Download presentation
Presentation is loading. Please wait.
Published byAdelia Crawford Modified over 9 years ago
1
while loops while ( ) { } -------------------------- while ( ) ;
2
while loops - example x = 7; while ( x < 10 ) { printf("%d",x); x++; } OUTPUT:
3
while loops - example x = 7; while ( x < 3 ) { printf("%d",x); x++; } OUTPUT:
4
do-while loops do { statements } while ( )
5
do-while loops - example x = 7; do { printf("%d",x); x++; } while ( x < 10 ) OUTPUT:
6
do-while loops - example x = 7; do { printf("%d",x); x++; } while ( x < 3 ) OUTPUT:
7
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:
8
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:
9
Ideal use for while: when you don't know how many times to loop OUTPUT:
10
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.
11
Flowcharting Process Predefined Process Preparation Decision User Input Input/Output Display Output Terminator Connector Connector (off page)
12
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
13
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
14
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
15
D printf nH,nQ,nD,nN,nP Finish
16
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
17
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
18
D printf nH,nQ,nD,nN,nP Finish
19
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
20
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
21
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.