1 / 30

APS105

APS105. Repeating. Repeating. Repeat a certain number of times Do 100 pushups! Repeat until done Do pushups until you are sore! In programming these are called “loops” “for” loops: counted, repeat until count is done “while” loops: repeat while a condition is true.

varana
Télécharger la présentation

APS105

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. APS105 Repeating

  2. Repeating • Repeat a certain number of times • Do 100 pushups! • Repeat until done • Do pushups until you are sore! • In programming these are called “loops” • “for” loops: counted, repeat until count is done • “while” loops: repeat while a condition is true loops are a very common part of programs!

  3. While Loops

  4. While Loops • General form: while (<expression>) <statement> • Effect: • Evaluate expression • If expression is true • execute statement • goto (1)

  5. While Loop Example1 • Read ints until zero is read, then print the sum .

  6. While Loop Example1 (curt version) • Read ints until zero is read, then print the sum .

  7. While Loop Example2 • Read ints until a negative num, then print the average .

  8. Do-While Loops • “while input number is non-zero, print it and input another number” • Matches a “while” loop nicely • Note: may print zero times if first num is zero • “input a number, print it, repeat if non-zero • Note: will print at least once • Matchs a “do-while” loop nicely • do-while: general form: do <statement> while (<expression>);

  9. Do-While Example • Input a number until between 1 and 10 .

  10. For Loops

  11. For Loops • General form: for (<expr1>; <expr2>; <expr3>) <statement> • Identical to this while loop arrangement: <expr1>; while (<expr2>) { <statement> <expr3> }

  12. For Loops (cont’d) • Typical use: for (<initialization>; <condition>; <update>) <statement> • <Initialization> • Executed once at the beginning • <condition> • Must be true (non-zero) to continue • <statement> • <update> • goto (2)

  13. For Loop Example • Print the squares of the numbers 1..10 .

  14. for variations: locally-declared counter variable • Example .

  15. for variations: empty initialization • Example: .

  16. for variations: empty update • Example: .

  17. for variations: empty condition • If condition expression is empty: • Assumed to be true • Example: .

  18. for variations: complex expr’s • Example: .

  19. Examples

  20. Which Kind of Loop to Use? • Use for: • If the number of repetitions is known • Use while: • If want to test condition before doing loop body • Use do-while: • If want to do loop body then test loop condition

  21. Loops with Condition AND Counting • Sum the first 100 squares (starting at 1), but stop if the sum passes 100000 .

  22. Nesting Loops: Example1 • Print a triangular pattern • Eg., if n is 5 then print: 1 12 123 1234 12345

  23. Nesting Loops: Example1 .

  24. Nesting Loops: Example2 • Print a triangular pattern of stars • Eg., if n is 5 then print: ***** **** *** ** *

  25. Nesting Loops: Example2 .

  26. Nesting Loops: Example3 • Print a triangular pattern of stars • Eg., if n is 5 then print: * ** *** **** *****

  27. Nesting Loops: Example2 .

  28. Another Loop Example • User inputs a digit (retry until a digit is input) • Then user inputs an integer • Print the number of occurrences of digit in int • Example User inputs digit 3 User inputs integer 342432 Program outputs: there are 2 occurrances of 3 in 342432

  29. Another Loop Example .

  30. Another Loop Example (cont’d) .

More Related