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++; } OUTPUT:
do-while loops do { statements } while ( )
do-while loops - example x = 7; do { printf("%d",x); x++; } while ( x < 10 ) OUTPUT:
do-while loops - example x = 7; do { printf("%d",x); x++; } while ( x < 3 ) OUTPUT:
comparison while vs do-while x = 7; do { printf("%d",x); x++; } while ( x < 10 ) OUTPUT: x = 7; while ( x < 10 ) { printf("%d",x); x++; } OUTPUT:
comparison while vs do-while x = 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 nHalves = nHalves + 1 nHalves = 0 Finish
Expand to all coins - page 1 myAmt < 0.50 myAmt = myAmt nH = nH+ 1 myAmt = amount scanf amount Start nH = 0 A A B
myAmt < 0.25 myAmt = myAmt nQ = nQ + 1 nQ = 0 C myAmt < 0.10 myAmt = myAmt nD = nD + 1 nD = 0 myAmt < 0.05 myAmt = myAmt nN = nN + 1 nN = 0 C myAmt < 0.01 myAmt = myAmt 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 coinsOutput vC=value of coinInput myAmt=amt leftInput/Output function: change return
myAmt < vC myAmt = myAmt - vC nC = nC + 1 nC = 0; vC=input General case: nC=number coinsOutput vC=value of coinInput myAmt=amt leftInput/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 coinsOutput vC=value of coinInput myAmt=amt leftInput/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