comp 110 more loops luv kohli september 15, 2008 mwf 2-2:50 pm sitterson 014 1
DESCRIPTION
Questions? 3TRANSCRIPT
![Page 1: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/1.jpg)
COMP 110COMP 110More loopsMore loops
Luv KohliSeptember 15, 2008
MWF 2-2:50 pmSitterson 014
1
![Page 2: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/2.jpg)
AnnouncementsAnnouncementsFacebook Tech Talk tonight, 6pm, SN011
Google info session, 6pm, SN014, tomorrow night
2
![Page 3: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/3.jpg)
Questions?Questions?
3
![Page 4: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/4.jpg)
Today in COMP 110Today in COMP 110do-while loops
for loops
![Page 5: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/5.jpg)
Review: loopsReview: loopsOften you need to repeat an action in a
programStart
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 6: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/6.jpg)
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
![Page 7: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/7.jpg)
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
![Page 8: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/8.jpg)
Review: Review: whilewhile loop loop
start;while (not enough
sandwiches){ make a sandwich;}distribute sandwiches;
Start
Enough sandwiches?
Distribute sandwiches
NoYesMake
sandwich
![Page 9: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/9.jpg)
Review: Review: whilewhile loop syntax loop syntax
while (boolean expression){ statements;}
![Page 10: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/10.jpg)
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
![Page 11: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/11.jpg)
whilewhile loop loop
EvaluateBoolean expression
Execute Body
End loop
truefalse
n=1
n <= 10?
End
Output n
No
n=n+1
Yes
![Page 12: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/12.jpg)
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
![Page 13: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/13.jpg)
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
![Page 14: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/14.jpg)
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
![Page 15: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/15.jpg)
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
![Page 16: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/16.jpg)
dodo--whilewhile loop syntax loop syntax
do{ statements; // loop body} while (boolean expression);
If Boolean expression is true, repeat loop bodyOtherwise, exit loop
![Page 17: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/17.jpg)
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!
![Page 18: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/18.jpg)
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);
![Page 19: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/19.jpg)
Infinite loopInfinite loop
int n = 1;do{ System.out.println(n); // n = n + 1;} while (n <= 10);
![Page 20: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/20.jpg)
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
![Page 21: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/21.jpg)
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
![Page 22: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/22.jpg)
forfor loop loop
22
EvaluateBoolean
expression
Execute BodyEnd loop
true false
Execute Initializing
actions
Execute Update actions
![Page 23: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/23.jpg)
forfor loop syntax loop syntax
for (initializing actions; Boolean expression; update actions){ statements; // loop body}
![Page 24: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/24.jpg)
Using a Using a forfor loop loop
int n;
for (n = 1; n <= 10; n++){ System.out.println(n);}
![Page 25: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/25.jpg)
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;}
![Page 26: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/26.jpg)
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);}
![Page 27: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/27.jpg)
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);}
![Page 28: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/28.jpg)
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
![Page 29: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/29.jpg)
Infinite loopInfinite loop
int n;
for (n = 1; n <= 10; n = 0){ System.out.println(n);}
![Page 30: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/30.jpg)
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
![Page 31: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1](https://reader036.vdocuments.us/reader036/viewer/2022062600/5a4d1b1f7f8b9ab059994c34/html5/thumbnails/31.jpg)
WednesdayWednesdaySpend some more time with loops
31