LOOPS CSI 121 Structured Programming Language Course teacher

  • Slides: 11
Download presentation
LOOPS CSI 121 – Structured Programming Language Course teacher: Minhajul Bashir minhajul@cse. uiu. ac.

LOOPS CSI 121 – Structured Programming Language Course teacher: Minhajul Bashir minhajul@cse. uiu. ac. bd 12/18/2021

WRITE A C PROGRAM TO FIND OUT THE SUM OF THE FIRST 100 NATURAL

WRITE A C PROGRAM TO FIND OUT THE SUM OF THE FIRST 100 NATURAL NUMBERS minhajul@cse. uiu. ac. bd 12/18/2021

Start sum = 0 i=1 No Yes sum = sum + i i=i+1 Print

Start sum = 0 i=1 No Yes sum = sum + i i=i+1 Print sum End minhajul@cse. uiu. ac. bd 12/18/2021 3

Start AN APPROACH THAT IS NOT USED TODAY sum = 0 i=1 sum =

Start AN APPROACH THAT IS NOT USED TODAY sum = 0 i=1 sum = 0; i = 1; No A label loop: Yes sum = sum + i i=i+1 if(i <= 100) { sum += i; Forcefully go to label i++; goto loop; Print sum } printf(“%dn”, sum); End minhajul@cse. uiu. ac. bd 12/18/2021 4

LOOPS • The goto approach is seldom used today, for better code readability •

LOOPS • The goto approach is seldom used today, for better code readability • Three loops in C • while loop • do … while loop • for loop minhajul@cse. uiu. ac. bd 12/18/2021

WHILE LOOP sum = 0; No label i = 1; sum = 0; i

WHILE LOOP sum = 0; No label i = 1; sum = 0; i = 1; loop: if(i <= 100) { while(i <= 100) { sum += i; i++; goto loop; } printf(“%dn”, sum); minhajul@cse. uiu. ac. bd } Starts the next iteration automatically printf(“%dn”, sum); 12/18/2021 6

WHILE LOOP scanf(“%d”, &n); sum = 0; • The statements in a while loop

WHILE LOOP scanf(“%d”, &n); sum = 0; • The statements in a while loop are executed if the condition is true i = 1; • Otherwise, the statements are NEVER executed while(i <= n) { • Example: the while code will not execute if the user inputs n=0 sum += i; i++; } minhajul@cse. uiu. ac. bd • If you want the code should execute at least once, use do … while 12/18/2021 7

DO … WHILE LOOP sum = 0; i = 1; while(i <= 100) {

DO … WHILE LOOP sum = 0; i = 1; while(i <= 100) { do { sum += i; i++; } } while(i <= 100); printf(“%dn”, sum); minhajul@cse. uiu. ac. bd 12/18/2021 8

DO … WHILE LOOP • Anything that can be done with a do …

DO … WHILE LOOP • Anything that can be done with a do … while loop, can also be done with a while loop • So is there actually a need for do … while? • Check out this link: https: //stackoverflow. com/questions/994905/is-there-ever-aneed-for-a-do-while-loop minhajul@cse. uiu. ac. bd 12/18/2021 9

FOR LOOP • for loop is a shortcut to while loop • Generally used

FOR LOOP • for loop is a shortcut to while loop • Generally used for loops with incremental operations • Like the example we have seen!!! minhajul@cse. uiu. ac. bd 12/18/2021 10

FOR LOOP sum = 0; Initialization sum = 0; Increment i = 1; while(i

FOR LOOP sum = 0; Initialization sum = 0; Increment i = 1; while(i <= 100) { for(i = 1; i <= 100; i++) { sum += i; i++; } Condition } printf(“%dn”, sum); minhajul@cse. uiu. ac. bd printf(“%dn”, sum); 12/18/2021 11