Presentation is loading. Please wait.

Presentation is loading. Please wait.

BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.

Similar presentations


Presentation on theme: "BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In."— Presentation transcript:

1 BACS 287 Programming Logic 3

2 BACS 287 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

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

4 BACS 287 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

5 BACS 287 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

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

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

8 BACS 287 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

9 BACS 287 Comparison of Iteration Structures 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

10 BACS 287 How Do Iteration Structures Work? 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

11 BACS 287 Comparison of Iteration Structures 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?

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

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

14 BACS 287 Iteration Solution 1 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

15 BACS 287 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.

16 BACS 287 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

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

18 BACS 287 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

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

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

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

22 BACS 287 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


Download ppt "BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In."

Similar presentations


Ads by Google