CS110_03c_loops - EECS110: 3c Repetition in C: Loops Jack...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EECS110: 3c Repetition in C: Loops Jack Tumblin [email protected] . 1 Repetition: Loops Loop statements repeat (jargon: `iterate 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 finish! (usually a bug; not always...) 2 Loops: 3 Kinds 1) Pre-Test Loop: "Ask First" Keep going? YES if cond is true. Run statement(s), repeat... ( see the loop? ) start is(cond)true? yes { block of statements; } no finish 3 Loops: 3 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? no yes ( see the loop? ) finish 4 Loops: 3 Kinds 3) Interrupted Loop: "Ask Anytime" Run some statement(s), Keep going? YES if cond is true. Run some statement(s) repeat... start { block of statements; } is(cond)true? no yes { block of statements; } ( see the loop? ) BAD IDEA! AVOID THIS!! finish 5 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, BUT (of course) they statements don't match the 3 kinds of loops! for(init; cond; step){stmts;} pre-test while(cond) {stmts;} pre-test post-test do{stmts;} while(cond) break; interrupt the loop. continue; skip to next pass. 6 Pre-test Loops in C: while : event-driven while (cond) {stmts;} /* Frog Feeding I*/ moveTo(air); open_eyes(); while(TRUE==see_fly()) { flick_tongue(); clamp_mouth(); } // // // init init cond yes { block of statements; } start no is(cond)true? //action finish 7 Pre-test Loops in C: while : count-driven while (cond) {stmts;} /* Frog lifetime*/ int days; days = 155; // while(days > 0) // { work_all_day(); sleep_all_night(); days--; // } die_quietly(); init cond start no is(cond)true? yes { block of statements; } step finish 8 Pre-test Loops in C: for : event-driven for(init; cond; step){stmts;} init cond ){ // 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); start no is(cond)true? yes { block of statements; } finish 9 Pre-test Loops in C: for : count-driven for(init; cond; step){stmts;} init cond ){ // Frog lifetime int days; //for( init; cond; step) for(days=155; days>0; days--) { work_all_day(); sleep_all_night(); } die_quietly(); start no is(cond)true? yes { block of statements; } finish 10 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;) { block of statements; } is(cond)true? yes finish 11 start no 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? break; no { block of statements; } finish 12 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? break; yes { block of statements; } finish 13 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! 14 while(cond){stmts;}; Pre-test; best for event-driven loops -test the cond expression -if TRUE, run {stmts;} and start again. -else STOP. Messy for count-driven loops; `for' is neater. 15 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. 16 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... 17 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); } . 18 Output: > 0,0 > 0,1 0,2 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 19 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 > 20 0,1 1,1 2,1 3,1 0,2 1,2 2,2 3,2 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 > 21 If you put semicolon here, what happens? 0,1 1,1 2,1 3,1 0,2 1,2 2,2 3,2 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,3 1,3 2,3 3,3 > 22 The `j' loop does NOTHING! 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 > 23 If you skip the curly braces here what happens? 0,1 1,1 2,1 3,1 0,2 1,2 2,2 3,2 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"); Output: > 0,0 0,1 > BAD IDEA! The 2nd printf("\n"); is not inside the `i' loop statement! 0,2 1,0 1,1 1,2 2,0 2,1 2,2 3,0 3,1 3,2 24 Better Notation for Nested Loops Keep it OBVIOUS: OBVIOUS -- Curly-brackets for ALL for loops, -- Orderly indenting for ALL loops, like this: Output: printf("\n"); for (i=0; i<4; i++) { for (j=0; j<3; j++) { printf("( %d,%d )", i, j); } printf("\n"); } > 0,0 1,0 2,0 3,0 > 0,1 1,1 2,1 3,1 0,2 1,2 2,2 3,2 25 Pre-Test Loops: while==for Can you turn a for loop into a while loop? Yes! init; while(cond) { statements; step; } 26 for(init; cond; step) { statements; } Bug Warnings Infinite loops if cond is never true Floating-point data in a FOR statement may be unreliable... 27 ...
View Full Document

This note was uploaded on 10/05/2011 for the course COMPUTER S 110-1 taught by Professor Tumblin during the Spring '11 term at Northwestern.

Ask a homework question - tutors are online