Repetition in C: Loops
1
Repetition: Loops • Loop statements repeat (jargon: ‘iterate on’) on’ a {block of statements;} for as many times as you wish, • Until a terminating condition tells a loop when and where to stop repeating those statements. • No terminating condition? Your program will never get finished! (that is usually a bug) 2
Loops: 3 x 2 Kinds 1) Pre-Test Loop: “Ask First” Keep going? YES if cond is true. Run statement(s), repeat…
start is(cond)true?
no
yes {
block of statements;
}
( see the loop? )
finish 3
Loops: 3 x 2 Kinds 1) Pre-Test Loop: “Ask First” Two kinds of condition: cond 1a) Event-driven Event 1b) Count-driven Count
start is(cond)true?
no
yes {
block of statements;
}
finish 4
Loops: 3 x 2 Kinds 2) Post-Test Loop: “Ask Last” Run statement(s). Keep going? YES if cond is true. repeat…
start {
block of statements;
} is(cond)true? yes
( see the loop? )
no
finish 5
Loops: 3 x 2 Kinds 2) Post-Test Loop: “Ask Last” Two kinds of condition: cond 2a) Event-driven Event 2b) Count-driven Count
start {
block of statements;
} is(cond)true? yes
no
finish 6
Loops: 3 x 2 Kinds 3) Interrupted Loop: “Ask Anytime”
start
{ block of statements; } Run some statement(s), Keep going? is(cond)true? no yes YES if cond is true. { block of statements; } Run some statement(s) repeat… ( see the loop? ) finish
BAD IDEA! AVOID THIS!!
7
Loops: 3 x 2 Kinds 3) Interrupted Loop: “Ask Anytime” Two kinds of condition: cond 3a) Event-driven Event 3b) Count-driven Count
start { block of statements; } is(cond)true? no yes { block of statements; }
BAD IDEA! AVOID THIS!!
finish 8
C Looping Summary • Just 3 kinds of loops (for ANY language) pre-test (runs 0, 1 or more times) post-test (runs 1,2, or more times) interrupted (runs 0.7? 1.3? or more? Bad idea. )
• C: Just 3 kinds of loop statements, statements BUT (of course) they don’t match the 3 kinds of loops! for(init; cond; step){stmts;} while(cond) {stmts;} do{stmts;} while(cond)
pre-test pre-test post-test 9
Pre-test Loops in C: while : event-driven while (cond) {stmts;}
start is(cond)true?
/* Frog Feeding I*/ moveTo(air); open_eyes(); while(TRUE==see_fly()) { flick_tongue(); clamp_mouth(); }
/* /* /*
init init cond
*/ */ */
no
yes {
block of statements;
}
/*action*/ finish 10
Pre-test Loops in C: while : count-driven while (cond) {stmts;}
is(cond)true?
/* Frog lifetime*/ int days; days = 155; /* while(days > 0) /* { work_all_day(); sleep_all_night(); days--; /* } die_quietly();
start no
yes init */ cond */
step */
{
block of statements;
}
finish 11
Pre-test Loops in C: for : event-driven start
for(init; } init cond; cond step){stmts; ){ /* Frog lifetime*/ int days, wet; /*for( init; cond; step)*/ for(days=155,wet=1; 1==wet && days>0; days--;) { work_all_day(); sleep_all_night(); wet = jumpInWater(); } die_quietly(wet);
is(cond)true?
no
yes {
block of statements;
}
finish 12
Pre-test Loops in C: for : count-driven start
for(init; } init cond; cond step){stmts; ){ /* Frog lifetime*/ int days;
is(cond)true? yes {
/*for( init; cond; step)*/ for(days=155; days>0; days--) { work_all_day(); sleep_all_night(); } die_quietly();
no
block of statements;
}
finish 13
Post-Test Loops in C: do{}while() : event or count
do{stmts; }while(cond ) do{ }while(
/*Frog Feeding II*/ do { chew_and_mash(); swallow(); } while (mouth_empty()==FALSE;)
start {
block of statements;
} is(cond)true? yes
no
finish 14
Interrupted Loops in C: break keyword 3) Interrupted Loop: (avoid this!) start float energy; ... while(TRUE) { drink_water(); if(energy <= 2.384) break; jump_10_meters(); }
{ block of statements; } is(cond)true? no yes { block of statements; }
finish 15
Interrupted Loops in C: break keyword 3) Interrupted Loop: (sometimes MUST do it) (rare!) 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();
start { block of statements; } is(cond)true? no yes { block of statements; }
finish 16
Summarize: Loops in C • Three kinds of loop-making statements in C, – – –
while(cond) {stmts;} do{stmts;} while(cond); for(init; cond; step){stmts;}
• Different kinds are best for different tasks, so choose loops wisely. Don’t use ‘for’ loops for everything! 17
while(cond){stmts;}; • Pre-test, Event-driven -test the cond expression -if TRUE, run {stmts;} and start again. -else STOP. • Messy for count-driven loops; use ‘for’. 18
do{stmts;}while(cond;) • Post-Test, OK for Event- or Count-driven; -run {stmts;} statement, -test cond; -If TRUE, start again; else STOP. • Count-driven? Try to re-arrange problem into pre-test form, then use ‘for’ statement. 19
for(init; cond; step){stmts;} • Pre-Test, Count-driven run init; (statement); test cond; (statement); if true, run stmts; else STOP run step expression repeat… 20
Nested Loops Nesting: A loop is a complete statement. int i,j; ... i = 0; for (j=0; j<3; j++) { printf(“( %d,%d )”, i, j); }
Output: > 0,0
0,1
0,2
>
. 21
Nested Loops Nesting: Place one loop inside another inner loop is one statement within the outer loop printf(“\n”); for (i=0; i<4; i++) { for (j=0; j<3; j++) { printf(“( %d,%d )”, i, j); } printf(“\n”); }
Output: > 0,0 1,0 2,0 3,0
0,1 1,1 2,1 3,1
0,2 1,2 2,2 3,2
>
22
Nested Loops: Notation • Inner loop is one statement within the outer loop • C notation permits very ‘compact’ notation, BUT • Hard to read, may hide errors printf(“\n”); for (i=0; i<4; i++){ for (j=0; j<3; j++) printf(“( %d,%d )”, i, j); printf(“\n”); }
BAD IDEA! Output: > 0,0 1,0 2,0 3,0 >
0,1 1,1 2,1 3,1
0,2 1,2 2,2 3,2 23
Nested Loops: Notation • Inner loop is one statement within the outer loop • C notation permits very ‘compact’ notation, BUT • Hard to read, may hide errors printf(“\n”); for (i=0; i<4; i++){ for (j=0; j<3; j++) printf(“( %d,%d )”, i, j); printf(“\n”); }
If you put semicolon here, what happens?
BAD IDEA! Output: > 0,0 1,0 2,0 3,0 >
0,1 1,1 2,1 3,1
0,2 1,2 2,2 3,2 24
Nested Loops: Notation • Inner loop is one statement within the outer loop • C notation permits very ‘compact’ notation, BUT • Hard to read, may hide errors printf(“\n”); for (i=0; i<4; i++){ for (j=0; j<3; j++); printf(“( %d,%d )”, i, j); printf(“\n”); }
The ‘j’ loop does NOTHING!
BAD IDEA! Output: > 0,3 1,3 2,3 3,3 >
25
Nested Loops: Notation • Inner loop is one statement within the outer loop • C notation permits very ‘compact’ notation, BUT • Hard to read, may hide errors printf(“\n”); for (i=0; i<4; i++){ for (j=0; j<3; j++) printf(“( %d,%d )”, i, j); printf(“\n”); }
If you skip the curly braces here what happens?
BAD IDEA! Output: > 0,0 1,0 2,0 3,0 >
0,1 1,1 2,1 3,1
0,2 1,2 2,2 3,2 26
Nested Loops: Notation • Inner loop is one statement within the outer loop • C notation permits very ‘compact’ notation, BUT • Hard to read, may hide errors
BAD IDEA!
printf(“\n”); The 2nd printf(“\n”); is not for (i=0; i<4; i++) for (j=0; j<3; j++) inside the ‘i’ loop statement! printf(“( %d,%d )”, i, j); printf(“\n”); Output: > 0,0 0,1 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2 27 >
Pre-Test Loops: while==for • Can you turn a for loop into a while loop? • Yes!
for(init; cond; step) { statements; }
≈
init; while(cond) { statements; step; } 28
Bug Warnings • Infinite loops if cond is never true • Floating-point data in a FOR statement
29