1 / 22

BACS 287

BACS 287. Programming Logic 3. Iteration Constructs. Iteration constructs are used when you want to execute a segment of code several times. In pseudocode there are 2 different iteration types: DO WHILE - Test at top, 0 or more loops DO UNTIL - Test at bottom, 1 or more loops.

moya
Download Presentation

BACS 287

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. BACS 287 Programming Logic 3 BACS 287

  2. Iteration Constructs • Iteration constructs are used when you want to execute a segment of code several times. • In pseudocode there are 2 different iteration types: • DO WHILE - Test at top, 0 or more loops • DO UNTIL - Test at bottom, 1 or more loops BACS 287

  3. Do While Structure DO WHILE condition action 1 ... action n ENDDO • This executes while the condition continues to be true BACS 287

  4. Do While Example 1 Get positive Value from user DOWHILE Value < 0 Print “Error, enter a positive value” Get positive Value from user ENDDO Print Value BACS 287

  5. Do While Example 2 sum = 0 cnt = 0 Display “Enter test score OR -999 to exit” Read input Do While input <> -999 sum = sum + input cnt = cnt + 1 Read input End Do avg = sum / cnt <-- why is this line a problem? Print avg BACS 287

  6. Do Until Structure DO UNTIL condition action 1 ... action n ENDDO • This executes as long as the condition is not met BACS 287

  7. Do Until Example 1 Read system password DOUNTIL user password correct Get password from user ENDDO ... BACS 287

  8. Do Until Example 2 num = 0 Read system password DOUNTIL user password correct OR num = 3 Get password from user num = num + 1 ENDDO If num = 3 then <-- why is this ‘IF’ a problem? print “Sorry - Too many tries” Exit Program EndIf BACS 287

  9. Do While Structure cnt = 1 Do While cnt < 11 Print cnt cnt = cnt + 1 End Do Do Until Structure cnt = 1 Do Until cnt > 10 Print cnt cnt = cnt + 1 End Do Comparison of Iteration Structures BACS 287

  10. Do While Structure cnt = 1 Top: If cnt > 10 then goto End EndIf Print cnt cnt = cnt + 1 goto Top End: Do Until Structure cnt = 1 Top: Print cnt cnt = cnt + 1 If cnt < 11 then goto Top EndIf How Do Iteration Structures Work? BACS 287

  11. Do While Structure Read input record Do While Not EOF Gross = Hours * Rate Print Gross Read input record End Do Print Summary What if the input file Do Until Structure Read input record Do Until EOF Gross = Hours * Rate Print Gross Read input record End Do Print Summary is empty? Comparison of Iteration Structures BACS 287

  12. Iteration and Selection Read input record Do While Not EOF If Balance < 0 then Print “Overdrawn Account” EndIf Read input Record EndDo BACS 287

  13. Iteration Problem 1 • Write pseudocode to print “HI MOM” 100 times. BACS 287

  14. cnt = 1 Do While cnt < 101 Print “HI MOM” cnt = cnt + 1 End Do cnt = 1 Do Until cnt > 100 Print “HI MOM” cnt = cnt + 1 End Do Iteration Solution 1 BACS 287

  15. Iteration Problem 2 • Write the pseudocode to play a simple “guess the secret number” game. The user has 10 tries to guess. No hints are given after each guess. BACS 287

  16. Iteration Solution 2 Generate random secret-number cnt = 0 Do Until Guess = secret-number OR cnt = 10 Print “Input your guess” Read Guess cnt = cnt + 1 End Do If Guess = secret-number then Print “Congratulations!! Endif BACS 287

  17. Iteration Problem 2a • Modify the previous problem to give the player hints of “Too high” or “Too low” during the game. The user still has 10 tries to guess. BACS 287

  18. Iteration Solution 2a Generate random secret-number cnt = 0 Do Until Guess = secret-number OR cnt = 10 Print “Input your guess” Read Guess If Guess < secret-number then Print “Too Low” Elseif Guess > secret-number then Print “Too High” EndIf cnt = cnt + 1 End Do If Guess = secret-number then Print “Congratulations!! Endif BACS 287

  19. Iteration Problem 3 • Sum the odd numbers between 1 and 100 and print the results. BACS 287

  20. Iteration Solution 3 sum = 0 cnt = 1 Do While cnt < 100 sum = sum + cnt cnt = cnt + 2 End Do Print sum BACS 287

  21. Iteration Problem 4 • Ask the user for a starting positive number. Continuing asking until they enter a valid number. Next, print the numbers from this starting number to 0. BACS 287

  22. Iteration Solution 4 Start Ask user for start-num DoWhile start-num <= 0 display error message Ask user for start-num End Do Do While start-num >= 0 Print start-num start-num = start-num - 1 End Do End BACS 287

More Related