SlideServe
Menu
Préférences
© SlideServe · v2026

Mastering While Loops in Programming

jeffreyttaylor avatar
21 diapositives · 267 Views · Jan 07, 2020

Learn how to use while and do-while loops effectively through examples and comparisons. Ideal for beginners.

Mastering While Loops in Programming - slide 1
01 / 21Mastering While Loops in Programming
1 / 17

Télécharger la présentation

Mastering While Loops in Programming
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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