210 likes | 242 Vues
while loops. while ( <expression> ) { <statements> } -------------------------- while ( <expression> ) <simple statement> ;. 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++; }
E N D
while loops while ( <expression> ){ <statements>} -------------------------- while ( <expression> ) <simple statement> ;
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 ( <expression> )
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 User Input Predefined Process Input/Output Preparation Display Output Decision Connector Connector (off page) Terminator
Start Flowcharting - Sample scanf amount nHalves = 0 Given some amount of money, amount, how many half dollars would be returned? myAmt = amount myAmt < 0.50 myAmt = myAmt - 0.50 nHalves = nHalves + 1 printf nHalves Finish
Expand to all coins - page 1 A Start nH = 0 scanf amount myAmt < 0.50 myAmt = amount A myAmt = myAmt - 0.50 nH = nH+ 1 B
B C nQ = 0 nN = 0 myAmt < 0.25 myAmt < 0.05 myAmt = myAmt - 0.25 myAmt = myAmt - 0.05 nQ = nQ + 1 nN = nN + 1 nD = 0 nP = 0 myAmt < 0.10 myAmt < 0.01 myAmt = myAmt - 0.10 myAmt = myAmt - 0.01 nD = nD + 1 nP = nP + 1 C D
D printf nH,nQ,nD,nN,nP Finish
Expand to all coins - page 1 A Start nH = 0; vH=0.50 scanf amount myAmt < vH myAmt = amount A myAmt = myAmt - vH nH = nH+ 1 B
B C nQ = 0; vQ=0.25 nN = 0; vN=0.05 myAmt < vQ myAmt < vN myAmt = myAmt - vQ myAmt = myAmt - vN nQ = nQ + 1 nN = nN + 1 nD = 0; vD=0.10 nP = 0; vP=0.01 myAmt < vD myAmt < vP myAmt = myAmt - vD myAmt = myAmt - vP nD = nD + 1 nP = nP + 1 C D
D printf nH,nQ,nD,nN,nP Finish
B General case: nC=number coins Output vC=value of coin Input myAmt=amt left Input/Output nQ = 0; vQ=0.25 myAmt < vQ myAmt = myAmt - vQ nQ = nQ + 1 function: change nC = 0; vC=input nD = 0; vD=0.10 myAmt < vC myAmt < vD myAmt = myAmt - vC myAmt = myAmt - vD nC = nC + 1 nD = nD + 1 return C
Start General case: nC=number coins Output vC=value of coin Input myAmt=amt left Input/Output scanf amount myAmt = amount change(nH,vH,myAmt) function:change (addr nC, val vC, addr Amt) change(nQ,vQ,myAmt) change(nD,vD,myAmt) nC = 0; vC=input myAmt < vC change(nN,vN,myAmt) change(nP,vP,myAmt) myAmt = myAmt - vC printf nH,nQ,nD,nN,nP nC = nC + 1 return Finish
Start scanf amount General case: nC=number coins Output vC=value of coin Input myAmt=amt left Input/Output myAmt = amount myAmt < 0 function:change (addr nC, val vC, addr Amt) change(nH,vH,myAmt) change(nQ,vQ,myAmt) change(nD,vD,myAmt) change(nN,vN,myAmt) change(nP,vP,myAmt) nC = 0; vC=input myAmt < vC printf nH,nQ,nD,nN,nP myAmt = myAmt - vC scanf amount nC = nC + 1 myAmt = amount return Finish