Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate.

Similar presentations


Presentation on theme: "1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate."— Presentation transcript:

1 1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate after 10 iterations or terminate when the user types NO) Careful! If there is no terminating condition, then your program will never finish

2 2 Loops Pre-Test Loop check condition if false, exit the loop if true, execute statements, iterate: check condition if false, exit the loop if true, execute statements, iterate: etc. The block of statements may not be executed at all (if condition is immediately false) The condition must be updated is( condition )true? { block of statements; } yes no star t finish initialize condition

3 3 Loops Pre-Test Loop count-driven : uses a "counter" to determine how many times it iterates Example: every time the loop iterates, a counter variable is incremented by one. When the counter reaches a specific value, the condition becomes false and the loop terminates. event-driven : uses an "event" to determine how many times it iterates Example: at each iteration the user is asked whether to continue. As long as the user types "yes" the loop iterates. When the user types "no" the condition becomes false and the loop terminates.

4 4 for loops Pre-test, mainly count-driven Syntax: Example: for (init; condition; update) { statements; } /* Frog lifetime*/ int days; for (days =155; days > 0; days--) { work_all_day(); sleep_all_night(); } die_quietly();

5 5 while loops Pre-test, mainly event-driven Syntax: Example (event): while (condition) { statements; } /* Frog Feeding */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); clamp_mouth(); swallow_fly(); }

6 6 while loops Pre-test, mainly event-driven Syntax: Example (counter): while (condition) { statements; } /* Frog lifetime*/ int days; days = 155; /* initialize */ while ( days > 0 ) { /* condition */ work_all_day(); sleep_all_night(); days--; /* update */ } die_quietly();

7 7 Loops Post-Test Loop execute statements check condition if false, exit the loop if true, iterate: execute statements check condition if false, exit the loop if true, iterate: etc. The block of statements is always executed at least once The condition must be updated is( condition )true? { block of statements; } yes no finish star t initialize condition

8 8 Loops Post-test loops The block of statements is ALWAYS executed at least once! Often used for data validation (e.g. if the user types a wrong selection, keep asking for a correct one) A post-test loop may be count- or event-driven.

9 9 do-while loop Post-test, mainly event driven Syntax: Example: do { statements; } while (condition) /* Frog mating*/ do { have_mate = look_for_lady_frog(); } while ( have_mate == FALSE )

10 10 Loops In some cases, we may need to break put of a loop prematurely. To do that, we use a break statement. Example: int days; float food,fat;... for( days = 155; days > 0; days--) { work_all_day(); if ( food+fat < 0.01) break; sleep_all_night(); } die_quietly();

11 11 Loops In some cases, we may want to only execute part of the body during an iteration. To do that, we use a continue statement. Example: /* Frog feeding v2.0 */ while ( am_hungry() == TRUE && see_fly() == TRUE ) { flick_tongue(); if (!caught_fly()) continue; clamp_mouth(); swallow_fly(); }

12 12 break vs. continue vs. return continue it is used in loops it means : skip the remaining statements in the loop body and iterate again break it is used in loops and switch statements it means : skip the remaining statements in this block, break out of the block. When used in loops, it causes the loop to terminate return it may be used anywhere in a function. it means : terminate the function.


Download ppt "1 Loops Loops repeat (iterate) a block of statements for a number of times. A terminating condition tells a loop when to stop iterating (e.g. terminate."

Similar presentations


Ads by Google