Post on 18-Jan-2018
description
transcript
COMP 110COMP 110More loopsMore loops
Luv KohliSeptember 15, 2008
MWF 2-2:50 pmSitterson 014
1
AnnouncementsAnnouncementsFacebook Tech Talk tonight, 6pm, SN011
Google info session, 6pm, SN014, tomorrow night
2
Questions?Questions?
3
Today in COMP 110Today in COMP 110do-while loops
for loops
Review: loopsReview: loopsOften you need to repeat an action in a
programStart
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
Review: loopsReview: loops Loop: part of a program that
repeats
Body: statements being repeated
Iteration: each repetition of body
Stopping condition
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
Review: Review: Types of LoopsTypes of Loopswhile◦Safest choice◦Not always most elegant
do-while◦Loop iterates AT LEAST once
for◦Similar to while, but often more convenient
syntax
7
Review: Review: whilewhile loop loop
start;while (not enough
sandwiches){ make a sandwich;}distribute sandwiches;
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
Review: Review: whilewhile loop syntax loop syntax
while (boolean expression){ statements;}
Review: Using a Review: Using a whilewhile loop loop
int n = 1;while (n <= 10){ System.out.println(n); n = n + 1;}
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
whilewhile loop loop
EvaluateBoolean expression
Execute Body
End loop
truefalse
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
whilewhile loop loop
First, evaluate Boolean expression
If true, go into the body
If false, do not go into the body
n=20
n <= 10?
End
Output n
No
n=n+1
Yes
Types of LoopsTypes of Loopswhile◦Safest choice◦Not always most elegant
do-while◦Loop iterates AT LEAST once
for◦Similar to while, but often more convenient
syntax
13
dodo--whilewhile loop loop Similar to while loop
At least one iteration of the loop◦First execute the loop body◦Then evaluate the Boolean expression◦ If true, execute the body again◦ If false, quit the body
dodo--whilewhile loop loop
15
EvaluateBoolean expression
Execute Body End loop
true false
Execute Body
From outside the loop
EvaluateBoolean expression
End loop
false
Execute Body
From outside the loop
true
Equivalent
dodo--whilewhile loop syntax loop syntax
do{ statements; // loop body} while (boolean expression);
If Boolean expression is true, repeat loop bodyOtherwise, exit loop
Using a Using a dodo--whilewhile loop loop
int n = 1;do{ System.out.println(n); n = n + 1;} while (n <= 10);
Don’t forgetthe semicolon!
Using a Using a dodo--whilewhile loop loopWhat if n = 20?
int n = 20;do{ System.out.println(n); n = n + 1;} while (n <= 10);
Infinite loopInfinite loop
int n = 1;do{ System.out.println(n); // n = n + 1;} while (n <= 10);
Types of LoopsTypes of Loopswhile◦Safest choice◦Not always most elegant
do-while◦Loop iterates AT LEAST once
for◦Similar to while, but often more convenient
syntax
20
forfor loop loop Components of a for
loop:◦ Initializing actions
◦ Boolean expression
◦ Loop body
◦ Update actions
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
forfor loop loop
22
EvaluateBoolean
expression
Execute BodyEnd loop
true false
Execute Initializing
actions
Execute Update actions
forfor loop syntax loop syntax
for (initializing actions; Boolean expression; update actions){ statements; // loop body}
Using a Using a forfor loop loop
int n;
for (n = 1; n <= 10; n++){ System.out.println(n);}
whilewhile loops and loops and forfor loops loops
int n;
for (n = 1; n <= 10; n++){ System.out.println(n);}
int n = 1;while (n <= 10){ System.out.println(n); n = n + 1;}
forfor loop: initializing actions loop: initializing actionsOnly done ONCE at the beginning of the
loop, before the Boolean expression is tested
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
int n;
for (n = 1; n <= 10; n++){ System.out.println(n);}
forfor loop: update actions loop: update actionsExecuted at the end of every iteration◦ After the loop body, but before the next Boolean
expression evaluation
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
int n;
for (n = 1; n <= 10; n++){ System.out.println(n);}
Tracing a Tracing a forfor loop loopint n;
for (n = 1; n <= 3; n++){ System.out.println(n);}
123
n = ?n = 1n = 2n = 3n = 4
Execute initializing actionsEvaluate Boolean expressionExecute bodyExecute update actions
Infinite loopInfinite loop
int n;
for (n = 1; n <= 10; n = 0){ System.out.println(n);}
Types of LoopsTypes of Loopswhile◦Safest choice◦Not always most elegant
do-while◦Loop iterates AT LEAST once
for◦Similar to while, but often more convenient
syntax
30
WednesdayWednesdaySpend some more time with loops
31