1 / 21

while loops

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++; }

Download Presentation

while loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. while loops while ( <expression> ){ <statements>} -------------------------- while ( <expression> ) <simple statement> ;

  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 ( <expression> )

  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 User Input Predefined Process Input/Output Preparation Display Output Decision Connector Connector (off page) Terminator

  12. 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

  13. 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

  14. 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

  15. D printf nH,nQ,nD,nN,nP Finish

  16. 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

  17. 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

  18. D printf nH,nQ,nD,nN,nP Finish

  19. 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

  20. 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

  21. 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

More Related