Module 8 Part 2 Nested Loops 1 Nested

  • Slides: 11
Download presentation
Module 8 – Part 2 Nested Loops 1

Module 8 – Part 2 Nested Loops 1

Nested Loops • Similar to nested selection statements, loops can contain other loop statements

Nested Loops • Similar to nested selection statements, loops can contain other loop statements • For each iteration of the outer loop, the inner loop iterates completely 2

Pseudocode – Nested for Loop Example Problem Statement: Write a program that uses nested

Pseudocode – Nested for Loop Example Problem Statement: Write a program that uses nested for loops to print 10 rows of stars, as shown in the picture to the right. 3

Pseudocode – Nested for Loop Example BEGIN MAIN CREATE max. Rows ← 10, row,

Pseudocode – Nested for Loop Example BEGIN MAIN CREATE max. Rows ← 10, row, star FOR (row ← 1, row < max. Rows, row ← row + 1) FOR (star ← 1, star <= row, star ← star + 1) PRINT “*” ENDinner. FOR PRINTLINE () ENDouter. FOR END MAIN Ps 4

Nested for Loop Example int max. Rows = 10; for (int row = 1;

Nested for Loop Example int max. Rows = 10; for (int row = 1; row <= max. Rows; row++) { for (int star = 1; star <= row; star++) { PRINT ("*"); } PRINTLINE(); } 5

Pseudocode – Nested for Loop Example #2 Problem Statement: Write a program that uses

Pseudocode – Nested for Loop Example #2 Problem Statement: Write a program that uses nested for loops to print the shape shown in the picture to the right. Ps 6

Pseudocode – Nested for Loop Example #2 BEGIN MAIN CREATE space = 10, row,

Pseudocode – Nested for Loop Example #2 BEGIN MAIN CREATE space = 10, row, s, c FOR (row = 0, row < = 17, row += 2) FOR (s = 1, s < space, s++) PRINT “ ” ENDFOR (c = 1, c < r, c++) PRINT “*” ENDFOR space-PRINTLINE () ENDFOR END MAIN Ps 7

In-class Problem Revisited Write the Pseudocode for a Calculator program (Add, Subtract, Multiply, Divide

In-class Problem Revisited Write the Pseudocode for a Calculator program (Add, Subtract, Multiply, Divide and Exponents only) that uses a Switch statement to select the operation based on user input, nested loops with Sentinel values to allow the user to end an operation and return to the selection menu. 8

BEGIN MAIN CREATE operation = ‘z’ WHILE (operation != ‘Q’ OR ‘q’) PRINT (”Please

BEGIN MAIN CREATE operation = ‘z’ WHILE (operation != ‘Q’ OR ‘q’) PRINT (”Please select an operation (A, S, M, D, E or Q): READ operation SWITCH (operation) CASE ‘A’ OR ‘a’ DO PRINT (Number to add (0 to quit): ) total += number PRINT total WHILE (number <> 0) CASE ‘D’ OR ‘d’ DO PRINT (Number to divide (0 to quit): ) total /= number PRINT total WHILE (number <> 0) Concluded on next slide… Ps 9

CASE ‘E’ OR ‘e’ DO PRINT (Number to exponentiate (0 to quit): ) total

CASE ‘E’ OR ‘e’ DO PRINT (Number to exponentiate (0 to quit): ) total = total * total PRINT total WHILE (number <> 0) CASE ‘M’ OR ‘m’ DO PRINT (Number to multiply (0 to quit): ) total /= number PRINT total WHILE (number <> 0) ENDWHILE CASE ‘S’ OR ‘s’ DO PRINT (Number to subtract (0 to quit): ) total -= number PRINT total WHILE (number <> 0) ENDWHILE END MAIN Ps 10

End of Module 8 11

End of Module 8 11