Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS201 - Repetition loops.

Similar presentations


Presentation on theme: "CS201 - Repetition loops."— Presentation transcript:

1 CS201 - Repetition loops

2 Types Of Loops Counting Loop (while, for)
Sentinel-Controlled Loop (while, for) Endfile-Controlled Loop (while, for) Input Validation Loop (do-while) General Conditional Loop (while, for)

3 While Loop (same as Java)
while (condition) { Do Something } When condition is true, Do Something and return to while. When condition is false, skip and exit loop. i = 0; while ( i<10 ) { printf(“%d “, i); i=i+1;}

4 1 2 3 4 5 6 7 8 9

5 For Loop (almost like Java)
No internal type statement allowed! for (initialization; test; update) { Do Something } for(i=0; i<10; i=i+1) { printf(“%d\n”,i); } (same output) for (int j=0; j<10; j=j+1) { … } NOT ALLOWED IN C !

6 Interpretation First the initialization expression(s) is(are) executed. Then the test expression(s) is(are) evaluated. If true, statements are executed, update expression(s) is(are) executed, and test is evaluated again. If false, loop exits. Last expression controls multiple expressions.

7 What Prints? int i=0, j=0; for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2) printf(“%d %d\n”, i,j);

8 What Prints? int i=0, j=0; for (i=0, j=-2; i<8, j<2; i=i+1, j=j+2) printf(“%d %d\n”, i,j); 0 -2 1 0

9 Do While Loop (just like Java)
Do {something} while ( expression); Always does “something” at least once! Test expression. If true, return and to something again. If false, exit loop at that point.

10 What prints? int i=0; do { i=i+3; printf(“%d\n”, i);
} while (i<12);

11 What prints? int i=1; do { i=i+3; printf(“%d\n”, i);
} while (i<12); 4 7 10 13

12 Compound Assignment Like Java. x += 3; is the same as x = x + 3;
In general: var op = expression; Is the same as var = var op expression; += -= *= /= %=

13 Increment and Decrement Operators
Like Java Prefix and postfix versions ++i i++ i++; is the same as i = i + 1; j--; is the same as j = j – 1; However k=++i; is different than k=i++;

14 Prefix increment What prints? int i=0, j=0; printf(“%d\n”, i++);
j = i++; printf(“%d\n,j);

15 Prefix increment What prints? 2 int i=0, j=0; printf(“%d\n”, i++);
2 int i=0, j=0; printf(“%d\n”, i++); printf(%d\n”,++i); j = i++; printf(“%d\n,j); What’s the value of i here?

16 Prefix increment What prints? 2 int i=0, j=0; printf(“%d\n”, i++);
2 int i=0, j=0; printf(“%d\n”, i++); printf(%d\n”,++i); j = i++; printf(“%d\n,j); What’s the value of i here? 3

17 Sentinel-Controlled Loops
Loops that run as long as needed. Signal the end of the loop with a special value (negative for ages, etc.) Get a line of data While the sentinel value has not been encountered Process the data line. Get another line of data.

18 What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; }
printf(“%d\n”, sum); /* input = */

19 What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; }
printf(“%d\n”, sum); /* input = */ 16

20 What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; }
printf(“%d\n”, sum); /* input = -9 */

21 What prints? int i, sum=0; scanf(“%d”,&i); while(i!=-9) { sum+=i; }
printf(“%d\n”, sum); /* input = -9 */

22 EOF Many library functions return helpful data as the value of the function? For example, scanf returns the number of successful conversions. File functions can return a value equal to EOF (a special defined variable). You can send this to scanf with ctrl-D (linux)

23 What prints? int i, sum=0,status=0; status = scanf(“%d”,&i);
while(status!=EOF) { sum+=i; } printf(“%d\n”, sum); /* input = */ 2 4 6 ctl-d

24 What prints? int i, sum=0,status=0; status = scanf(“%d”,&i);
while(status!=EOF) { sum+=i; } printf(“%d\n”, sum); /* input = */ 2 4 6 ctl-d 12

25 Nested Loops Usually used to work with two dimensional arrays (later).
Simply put the definition for one loop in the body of another loop.

26 What Prints? int a=0, b=0, sum=0; for(a=0; a<6; a+=2)
for(b=0; b>4; b--) sum=sum+1; printf(“%d”,sum);

27 What Prints? int a=0, b=0, sum=0; for(a=0; a<6; a+=2)
for(b=0; b>-4; b--) sum=sum+1; printf(“%d”,sum);

28 Debugging Use a source level debugger as part of your IDE.
Use a command line debugger like gdb. Add printf statements to trace execution. (Be careful when removing the printf statements that you don’t introduce errors.)

29 ∞ Loops It’s hard to distinguish between a complex calculation loop and an infinite loop without debugging statements.


Download ppt "CS201 - Repetition loops."

Similar presentations


Ads by Google