+ All Categories
Home > Documents > COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1.

COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1.

Date post: 18-Jan-2018
Category:
Upload: dayna-rice
View: 216 times
Download: 0 times
Share this document with a friend
Description:
Questions? 3
31
COMP 110 COMP 110 More loops More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1
Transcript
Page 1: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1.

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.

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.

Questions?Questions?

3

Page 4: COMP 110 More loops Luv Kohli September 15, 2008 MWF 2-2:50 pm Sitterson 014 1.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

WednesdayWednesdaySpend some more time with loops

31


Recommended