Download presentation
Presentation is loading. Please wait.
Published byMary Barrett Modified over 9 years ago
1
do - while while: Execute the body of the loop at least once
False statements; True condition ? do - while while: False statements; True condition ? 142 K -1
2
do-while statement do { /*play the game*/ game(); /*again?*/
printf(“Another game (yes=1,no=0)? ”); scanf(“%i”,&answer); if (answer==1) go_on = TRUE; else go_on = FALSE; } while(go_on); #define constants TRUE is 1 FALSE is 0 don’t forget condition Play the game at least once 142 K -2
3
for loops for(initialization; condition; update expression) {
statement1; statement2; ... } Don’t forget update is written at the front of the loop, but is executed at the end Do not write "," Flow Chart initialization True condition ? statement1; statement2; update False 142 K -3
4
Counting in for loops /* print n asterisks */
for(count=1; count<=n; count=count+1) printf(“*”); initialization update condition don’t need { and } if only 1 statement What is printed? ************** n times Could also do: /* print n asterisks */ for(count=0; count<n; count=count+1) printf(“*”); start at 0 How would you print? ***** 142 K -4
5
for(row=1; row<=nrows; row = row+1) {
***** to print use: repeat 5 times printf(“*”); repeat 3 times Nested for loops for(row=1; row<=nrows; row = row+1) { } printf(“\n”); for(col=1;col<=ncols;col=col+1) printf(“*”); inner loop: print one row of ncols * outer loop: once a line of ncols * is printed, print a newline All of this nrows times ***** Get with nrows=3 and ncols=5 142 K - 5
6
Another 2D figure How would you print * ** *** ? **** *****
Note that the first row contains 1 star, the second 2 stars, the third 3 stars... Need only one input: the number of rows, nrows for(row=1; row<=nrows; row = row+1) { for(col=1; col<=row; col=col+1) printf(“*”); printf(“\n”); } 142 K -6
7
row at position row is made of row spaces
And what about ***** **** *** ** * ? Take row number 4: made of 3(=4-1) spaces 2(=5-(4-1)) * number the rows 0,1,2,... row at position row is made of row spaces nrows - row * (in our example above, nrows is 5) for(row=0; row<nrows; row = row+1) { for(col=1;col<=row;col=col+1) printf(“ ”); for(col=1;col<=nrows-row;col=col+1) printf(“*”); printf(“\n”); } 142 K -7
8
To simplify use a function
Given an integer n and a character, print the character n times. void repeat_char(int n, char symbol) { int i; for (i=0; i<n; i=i+1) printf(“%c”,symbol); } Then for (row=0; row<nrows; row=row+1) { repeat_char(row,‘ ’); repeat_char(nrows-row,‘*’); printf(“\n”); } 142 K -8
9
Another Example Printing a multiplication table: Print this 1 1 2 3 2
1 2 3 2*3 How? use two nested loops _ outer loop: Select the row to print for (row=1;row<=nrows;row=row+1) { printf(“\n”); } _ inner loop: Print the row for(col=1;col<=ncols;col=col+1) printf(“%4i”,row*col); 142 K -9
10
Loop Pitfalls sum = 0; while (sum < 10); sum = sum + 2;
Get a never ending loop What is sum? sum = 0; for (i=0; i<=10; i=i+1); sum = sum + i; i is 11 thus sum is 11 What is sum? sum = 0; for(i=1; i!=10; i=i+2) sum = sum + i; Get a never ending loop i is never 10 What is sum? 142 K -10
11
Loop Pitfalls double x; for (x=0.0; x<10.0; x=x+0.2)
printf(“%.16f\n”,x); Get (MSVC 6.0): Expect: ... ... do not use doubles as counters in loops 142 K -11
12
Better: int i; double x; for (i=0; i<50; i=i+1) {
x = (double)i/5.0; printf(“%.16f\n”,x); } ... 142 K -12
13
The continue statement
Directly go back to the update for(init; condition; update) { statements; if(cond) continue; more statements; } if cond is true, the rest of the loop body is skipped. What happens here? for(i=1; i<=10; i=i+1) { if (i%2==0) continue; printf(“The square of %i is %i\n”, i,i*i); } print the squares of the odd numbers between 1 and 10. However, use continue carefully and only if it clarifies your program. (not the case for the example above!). Can always be avoided. 142 K -13
14
The break statement Use break in a loop to exit the loop
example: print all of the prime numbers between 3 and 100 for(i=3; i<=100; i=i+1) { for(j=2; j<i; j=j+1) if(i%j==0) break; if(j==i-1) printf(“%i is a prime\n”,i); } if i can be divided by j, exit the j loop and check the next integer in the i loop use carefully 142 K -14
15
Handy Shorthand In a loop, very often use: i=i+1 or i=i-1
Instead, can use the unary operators: ++ and -- post increment i++ means i=i+1 i-- means i=i-1 post decrement Do you see the pun in C++? Example: for(i=1; i<=10; i++) { ... } Note: Also have /=, *=, +=, -=, %= e.g. x += a; means x=x+a; 142 K -15
16
Warning Also have pre-increment and pre-decrement ++i and --i
What is the difference with i++ and i-- ? None if used in isolation But NOT the same in an expression or an assignment. post: the variable is updated after being used pre: the variable is updated before being used int i=0, j=0, a, b; a = i++; /* a is 0 and i is 1 */ b = ++j; /* b is 1 and j is 1 */ USE ++ and -- in isolation do not write x = (y+1)/(3*(x-1)); as x = ++y/(3*(--x)); y is also updated! (before the expression is evaluated) 142 K -16
17
while, do-while, or for? Between do-while and while when we want
to execute the loop at least once Initialization Test Update for while in () after for prior to the loop in the loop body in () after while Better when know the number of iterations Better when don’t know number of iterations 142 K -17
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.