You are on page 1of 29

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
start
1) Pre-Test Loop:
“Ask First” no
is(cond)true?
Keep going?
yes
YES if cond is true. {
Run statement(s), block of statements;
}
repeat…

( see the loop? ) finish


3
Loops: 3 x 2 Kinds
start
1) Pre-Test Loop:
“Ask First” no
is(cond)true?
Two kinds of
condition: yes
cond
{
block of statements;
1a) Event-driven
Event }
1b) Count-driven
Count
finish
4
Loops: 3 x 2 Kinds
start
2) Post-Test Loop:
“Ask Last”
{
Run statement(s). block of statements;
Keep going? }

YES if cond is true. no


is(cond)true?
repeat… yes

( see the loop? ) finish


5
Loops: 3 x 2 Kinds
start
2) Post-Test Loop:
“Ask Last”
Two kinds of {
condition: block of statements;
cond
}

2a) Event-driven
Event no
is(cond)true?
2b) Count-driven
Count yes

finish
6
Loops: 3 x 2 Kinds
start
3) Interrupted Loop:
“Ask Anytime”
Run some statement(s), { block of statements; }

Keep going? is(cond)true? no


YES if cond is true. yes
Run some statement(s) { block of statements; }

repeat…
( see the loop? )
finish
BAD IDEA! AVOID THIS!!
7
Loops: 3 x 2 Kinds
start
3) Interrupted Loop:
“Ask Anytime”
Two kinds of { block of statements; }
condition:
cond is(cond)true? no
yes
3a) Event-driven
Event { block of statements; }

3b) Count-driven
Count

finish
BAD IDEA! AVOID THIS!!
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;} pre-test
while(cond) {stmts;} pre-test
do{stmts;} while(cond) post-test
9
Pre-test Loops in C:
while : event-driven
while (cond) {stmts;} start

no
/* Frog Feeding I*/ is(cond)true?
yes
moveTo(air); /* init */ {
open_eyes(); /* init */ block of statements;
while(TRUE==see_fly()) /* cond */ }
{
flick_tongue(); /*action*/
clamp_mouth();
finish
}

10
Pre-test Loops in C:
while : count-driven
while (cond) {stmts;} start

no
/* Frog lifetime*/ is(cond)true?
int days;
yes
days = 155; /* init */ {
while(days > 0) /* cond */ block of statements;
{ }
work_all_day();
sleep_all_night();
days--; /* step */
finish
}
die_quietly();
11
Pre-test Loops in C:
for : event-driven
for(init;
init cond;
cond step){stmts;
){ } start

no
/* Frog lifetime*/ is(cond)true?
int days, wet;
yes
/*for( init; cond; step)*/ {
for(days=155,wet=1;
block of statements;
1==wet && days>0; days--;)
}
{
work_all_day();
sleep_all_night();
wet = jumpInWater(); finish
}
die_quietly(wet);
12
Pre-test Loops in C:
for : count-driven
start
for(init;
init cond;
cond step){stmts;
){ }
no
/* Frog lifetime*/ is(cond)true?
int days; yes
{
/*for( init; cond; step)*/ block of statements;
for(days=155; days>0; days--) }
{
work_all_day();
sleep_all_night(); finish
}
die_quietly();
13
Post-Test Loops in C:
do{}while() : event or
count
do{stmts;
do{ }while(cond
}while( ) start

{
/*Frog Feeding II*/ block of statements;
do }
{
chew_and_mash(); no
is(cond)true?
swallow(); yes
}
while (mouth_empty()==FALSE;) finish

14
Interrupted Loops in C:
break keyword
3) Interrupted Loop: (avoid this!)
start

float energy;
{ block of statements; }
...
while(TRUE) is(cond)true? no
{ yes
{ block of statements; }
drink_water();
if(energy <= 2.384) break;
jump_10_meters();
finish
}

15
Interrupted Loops in C:
break keyword
3) Interrupted Loop: (sometimes MUST do it) (rare!)
start
int days;
float food,fat;
{ block of statements; }
for(days=155; days>0; days--)
{ is(cond)true? no
work_all_day(); yes
if( food+fat < 0.01) break; { block of statements; }
sleep_all_night();
}
die_quietly();
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;
...
Output:

i = 0; >
for (j=0; j<3; j++) 0,0 0,1 0,2
{ >
printf(“( %d,%d )”, i, j);
}

21
Nested Loops
Nesting: Place one loop inside another
inner loop is one statement within the outer loop

printf(“\n”);
Output:
for (i=0; i<4; i++)
{ >
for (j=0; j<3; j++) 0,0 0,1 0,2
{ 1,0 1,1 1,2
printf(“( %d,%d )”, i, j); 2,0 2,1 2,2
} 3,0 3,1 3,2
printf(“\n”); >
}

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 BAD IDEA!
printf(“\n”); Output:
for (i=0; i<4; i++){
for (j=0; j<3; j++) >
printf(“( %d,%d )”, i, j); 0,0 0,1 0,2
printf(“\n”); }
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 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 BAD IDEA!
printf(“\n”); Output:
for (i=0; i<4; i++){
for (j=0; j<3; j++) >
printf(“( %d,%d )”, i, j); 0,0 0,1 0,2
printf(“\n”); }
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
If you put semicolon here, >
what happens? 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 BAD IDEA!
printf(“\n”); Output:
for (i=0; i<4; i++){
for (j=0; j<3; j++); >
printf(“( %d,%d )”, i, j); 0,3
printf(“\n”); }
1,3
2,3
3,3
The ‘j’ loop does NOTHING! >
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 BAD IDEA!
printf(“\n”); Output:
for (i=0; i<4; i++){
for (j=0; j<3; j++) >
printf(“( %d,%d )”, i, j); 0,0 0,1 0,2
printf(“\n”); }
1,0 1,1 1,2
2,0 2,1 2,2
3,0 3,1 3,2
If you skip the curly braces here >
what happens? 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”);
for (i=0; i<4; i++) The 2nd printf(“\n”); is not
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!

init;
for(init; cond; step) while(cond)
{
statements; ≈ {
statements;
} step;
}
28
Bug Warnings
• Infinite loops if cond is never true

• Floating-point data in a FOR statement

29

You might also like