while loops

21
while loops while ( <expression> ) { <statements> } -------------------------- while ( <expression> ) <simple statement> ;

description

while loops. while ( ) { } -------------------------- while ( ) ;. while loops - example. x = 7; while ( x < 10 ) { printf("%d",x); x++; } OUTPUT:. while loops - example. x = 7; while ( x < 3 ) { printf("%d",x); x++; } - PowerPoint PPT Presentation

Transcript of while loops

Page 1: while loops

while loopswhile ( <expression> ){

<statements>}

--------------------------

while ( <expression> )<simple statement> ;

Page 2: while loops

while loops - examplex = 7;while ( x < 10 ){

printf("%d",x);x++;

}

OUTPUT:

Page 3: while loops

while loops - examplex = 7;while ( x < 3 ){

printf("%d",x);x++;

}

OUTPUT:

Page 4: while loops

do-while loopsdo {

statements} while ( <expression> )

Page 5: while loops

do-while loops - examplex = 7;do {

printf("%d",x);x++;

} while ( x < 10 )

OUTPUT:

Page 6: while loops

do-while loops - examplex = 7;do {

printf("%d",x);x++;

} while ( x < 3 )

OUTPUT:

Page 7: while loops

comparison while vs do-whilex = 7;do {

printf("%d",x);x++;

} while ( x < 10 )

OUTPUT:

x = 7;while ( x < 10 ) {

printf("%d",x);x++;

}

OUTPUT:

Page 8: while loops

comparison while vs do-whilex = 7;do {

printf("%d",x);x++;

} while ( x < 3 )

OUTPUT:

x = 7;while ( x < 3 ) {

printf("%d",x);x++;

}

OUTPUT:

Page 9: while loops

Ideal use for while: when you don't know how many times to loop

OUTPUT:

Page 10: while loops

Change problem - while example

Statement of problem:

Given any amount of change under $2.00, determine and print out the minimum number of coins required to make that amount of change. Available coins are Halves, Quarters, Dimes, Nickels, and Pennies.

Page 11: while loops

Flowcharting

Process

Predefined Process

Preparation

Decision

User Input

Input/Output

Display Output

Terminator

Connector

Connector (off page)

Page 12: while loops

Flowcharting - Sample

myAmt = amount

myAmt < 0.50

scanf amount

Start

printf nHalves

Given some amount of money, amount, how many half dollars would be returned?

myAmt = myAmt - 0.50

nHalves = nHalves + 1

nHalves = 0

Finish

Page 13: while loops

Expand to all coins - page 1

myAmt < 0.50

myAmt = myAmt - 0.50

nH = nH+ 1

myAmt = amount

scanf amount

Start nH = 0

A

A

B

Page 14: while loops

myAmt < 0.25

myAmt = myAmt - 0.25

nQ = nQ + 1

nQ = 0

C

myAmt < 0.10

myAmt = myAmt - 0.10

nD = nD + 1

nD = 0

myAmt < 0.05

myAmt = myAmt - 0.05

nN = nN + 1

nN = 0

C

myAmt < 0.01

myAmt = myAmt - 0.01

nP = nP + 1

nP = 0

B

D

Page 15: while loops

D

printf nH,nQ,nD,nN,nP

Finish

Page 16: while loops

Expand to all coins - page 1

myAmt < vH

myAmt = myAmt - vH

nH = nH+ 1

myAmt = amount

scanf amount

Start nH = 0; vH=0.50

A

A

B

Page 17: while loops

myAmt < vQ

myAmt = myAmt - vQ

nQ = nQ + 1

nQ = 0; vQ=0.25

C

myAmt < vD

myAmt = myAmt - vD

nD = nD + 1

nD = 0; vD=0.10

myAmt < vN

myAmt = myAmt - vN

nN = nN + 1

nN = 0; vN=0.05

C

myAmt < vP

myAmt = myAmt - vP

nP = nP + 1

nP = 0; vP=0.01

B

D

Page 18: while loops

D

printf nH,nQ,nD,nN,nP

Finish

Page 19: while loops

myAmt < vQ

myAmt = myAmt - vQ

nQ = nQ + 1

nQ = 0; vQ=0.25

C

myAmt < vD

myAmt = myAmt - vD

nD = nD + 1

nD = 0; vD=0.10

B

myAmt < vC

myAmt = myAmt - vC

nC = nC + 1

nC = 0; vC=input

General case:

nC=number coins OutputvC=value of coin InputmyAmt=amt left Input/Output

function: change

return

Page 20: while loops

myAmt < vC

myAmt = myAmt - vC

nC = nC + 1

nC = 0; vC=input

General case:

nC=number coins OutputvC=value of coin InputmyAmt=amt left Input/Output

function:change(addr nC, val vC, addr Amt)

return

myAmt = amount

scanf amount

Start

change(nH,vH,myAmt)

change(nQ,vQ,myAmt)

change(nD,vD,myAmt)

change(nN,vN,myAmt)

change(nP,vP,myAmt)

printf nH,nQ,nD,nN,nP

Finish

Page 21: while loops

myAmt < vC

myAmt = myAmt - vC

nC = nC + 1

nC = 0; vC=input

General case:

nC=number coins OutputvC=value of coin InputmyAmt=amt left Input/Output

function:change(addr nC, val vC, addr Amt)

return

myAmt = amount

scanf amount

Start

change(nH,vH,myAmt)change(nQ,vQ,myAmt)change(nD,vD,myAmt)change(nN,vN,myAmt)change(nP,vP,myAmt)

printf nH,nQ,nD,nN,nP

Finish

myAmt < 0

myAmt = amount

scanf amount