while loops
description
Transcript of while loops
![Page 1: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/1.jpg)
while loopswhile ( <expression> ){
<statements>}
--------------------------
while ( <expression> )<simple statement> ;
![Page 2: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/2.jpg)
while loops - examplex = 7;while ( x < 10 ){
printf("%d",x);x++;
}
OUTPUT:
![Page 3: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/3.jpg)
while loops - examplex = 7;while ( x < 3 ){
printf("%d",x);x++;
}
OUTPUT:
![Page 4: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/4.jpg)
do-while loopsdo {
statements} while ( <expression> )
![Page 5: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/5.jpg)
do-while loops - examplex = 7;do {
printf("%d",x);x++;
} while ( x < 10 )
OUTPUT:
![Page 6: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/6.jpg)
do-while loops - examplex = 7;do {
printf("%d",x);x++;
} while ( x < 3 )
OUTPUT:
![Page 7: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/9.jpg)
Ideal use for while: when you don't know how many times to loop
OUTPUT:
![Page 10: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/11.jpg)
Flowcharting
Process
Predefined Process
Preparation
Decision
User Input
Input/Output
Display Output
Terminator
Connector
Connector (off page)
![Page 12: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/15.jpg)
D
printf nH,nQ,nD,nN,nP
Finish
![Page 16: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/18.jpg)
D
printf nH,nQ,nD,nN,nP
Finish
![Page 19: while loops](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022070419/56815a9a550346895dc8171f/html5/thumbnails/21.jpg)
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