1 / 17

142 K -1

do - while. statements;. do - while. True. condition ?. False. . while:. True. condition ?. statements;. False. Execute the body of the loop at least once. 142 K -1. do-while statement. #define constants TRUE is 1 FALSE is 0. don’t forget. condition.

webbmary
Download Presentation

142 K -1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. do - while statements; do - while True condition ? False  while: True condition ? statements; False Execute the body of the loop at least once 142 K -1

  2. do-while statement #define constants TRUE is 1 FALSE is 0 don’t forget condition Play the game at least once 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); 142 K -2

  3. for loops update is written at the front of the loop, but is executed at the end Don’t forget initialization True statement1; statement2; update condition ? False for(initialization; condition; update expression) { statement1; statement2; ... } Do not write "," Flow Chart 142 K -3

  4. Counting in for loops initialization update condition don’t need { and } if only 1 statement What is printed? ************** n times start at 0 ***** ***** ***** How would you print? /* print n asterisks */ for(count=1; count<=n; count=count+1) printf(“*”); Could also do: /* print n asterisks */ for(count=0; count<n; count=count+1) printf(“*”); 142 K -4

  5. ***** ***** ***** to print 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(“*”); outer loop: once a line of ncols * is printed, print a newline All of this nrows times inner loop: print one row of ncols * ***** ***** ***** Get with nrows=3 and ncols=5 use: repeat 5 times printf(“*”); 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. 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 Print this 1 2 3 1 2 3 2 4 6 3 6 9 1 2 3 2*3 for (row=1;row<=nrows;row=row+1) { printf(“\n”); } for(col=1;col<=ncols;col=col+1) printf(“%4i”,row*col); Printing a multiplication table: How? use two nested loops _ outer loop: Select the row to print _ inner loop: Print the row 142 K -9

  10. Loop Pitfalls Get a never ending loop i is 11 thus sum is 11 i is never 10 Get a never ending loop sum = 0; while (sum < 10); sum = sum + 2; What is sum? sum = 0; for (i=0; i<=10; i=i+1); sum = sum + i; What is sum? sum = 0; for(i=1; i!=10; i=i+2) sum = sum + i; What is sum? 142 K -10

  11. Loop Pitfalls Get (MSVC 6.0): 0.0000000000000000 0.2000000000000000 0.4000000000000000 0.6000000000000001 0.8000000000000000 1.0000000000000000 1.2000000000000000 1.3999999999999999 0.0000000000000000 0.2000000000000000 0.4000000000000000 0.6000000000000000 0.8000000000000000 1.0000000000000000 1.2000000000000000 1.4000000000000000 ... ... 9.1999999999999993 9.3999999999999986 9.5999999999999979 9.7999999999999972 9.9999999999999964 9.2000000000000000 9.4000000000000000 9.6000000000000000 9.8000000000000000 double x; for (x=0.0; x<10.0; x=x+0.2) printf(“%.16f\n”,x); Expect: 142 K -11 do not use doubles as counters in loops

  12. 0.0000000000000000 0.2000000000000000 0.4000000000000000 0.6000000000000000 0.8000000000000000 1.0000000000000000 1.2000000000000000 1.3999999999999999 ... 9.1999999999999993 9.4000000000000004 9.5999999999999996 9.8000000000000007 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 if cond is true, the rest of the loop body is skipped. Directly go back to the update print the squares of the odd numbers between 1 and 10. for(init; condition; update) { statements; if(cond) continue; more statements; } 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); } 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 if i can be divided by j, exit the j loop and check the next integer in the i loop use carefully 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); } } 142 K -14

  15. Handy Shorthand In a loop, very often use: or i=i+1 i=i-1 post increment post decrement Do you see the pun in C++? Example: for(i=1; i<=10; i++) { ... } Note: Also have /=, *=, +=, -=, %= x += a; means x=x+a; e.g. Instead, can use the unary operators: ++ and -- i++ means i=i+1 i-- means i=i-1 142 K -15

  16. Warning Also have pre-increment and pre-decrement ++i and --i None if used in isolation post: the variable is updated after being used the variable is updated before being used pre: USE ++ and -- in isolation y is also updated! (before the expression is evaluated) What is the difference with i++ and i-- ? But NOT the same in an expression or an assignment. 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 */ do not write x = (y+1)/(3*(x-1)); as x = ++y/(3*(--x)); 142 K -16

  17. while, do-while, or for? when we want to execute the loop at least once while for Initialization prior to the loop in () after for Test in () after for in () after while Update in the loop body in () after for Better when know the number of iterations Better when don’t know number of iterations Between do-whileandwhile 142 K -17

More Related