Upload
adelia-crawford
View
232
Download
0
Embed Size (px)
Citation preview
while loopswhile ( <expression> ){
<statements>}
--------------------------
while ( <expression> )<simple statement> ;
while loops - examplex = 7;while ( x < 10 ){
printf("%d",x);x++;
}
OUTPUT:
while loops - examplex = 7;while ( x < 3 ){
printf("%d",x);x++;
}
OUTPUT:
do-while loopsdo {
statements} while ( <expression> )
do-while loops - examplex = 7;do {
printf("%d",x);x++;
} while ( x < 10 )
OUTPUT:
do-while loops - examplex = 7;do {
printf("%d",x);x++;
} while ( x < 3 )
OUTPUT:
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:
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:
Ideal use for while: when you don't know how many times to loop
OUTPUT:
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.
Flowcharting
Process
Predefined Process
Preparation
Decision
User Input
Input/Output
Display Output
Terminator
Connector
Connector (off page)
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
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
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
D
printf nH,nQ,nD,nN,nP
Finish
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
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
D
printf nH,nQ,nD,nN,nP
Finish
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
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 < 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