Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop.

Similar presentations


Presentation on theme: "Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop."— Presentation transcript:

1 Nested For Loops

2 First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop body }while(condition); for(integer initialization; condition; iteration) { // loop body }while(condition);

3 while(condition) { while(condition) { // loop body } do { do { // loop body }while(condition); for(integer initialization; condition; iteration) { for(integer initialization; condition; iteration) { // loop body }

4 Example 1 int n = 10; while(n == 10) { while(n > 0) { n--; }

5 Example 2 int n = 10; do { do { n--; }while(n > 0); }while(n == 10);

6 Example 3 for(int k = 0; k < 10; k++) { for(int j = 0; j < 10; j++) { System.out.println(“Hello World”); }

7 Example 3 continued for(int k = 0; k < 10; k++)// scope of variable k is within this entire loop { for(int j = 0; j < 10; j++) // j is only within this loop { System.out.println(“Hello World”); } System.out.println(j); // you cannot do this, j is “dead” }

8 Once a loop begins and encounters another loop, the inner loop now executes until it finishes before the outer loop continues. for(int k = 0; k < 10; k++) { for(int j = 0; j < 10; j++) { System.out.println(“Hello World”); } // Loop in red finishes 10 times before the outer loop can // continue.

9 What’s the output? int j = 0; while(j < 5) { for(int s = 0; s < 5; s++) { System.out.println(“Hello”); } System.out.println(“World”); j++; }

10 What’s the output? int j = 0; do { System.out.println(“World”); for(int s = 0; s < 5; s++) { System.out.println(“Hello”); } j++; }while(j < 10);

11 What’s the output? for(int k = 0; k < 5; k++) { for(int j = 0; j < 5; j++) { System.out.println(“Hello World”); } System.out.println(“Hello World”); }

12 Exercise 1 Print “Hello World” 50 times using two nested for loops. Let outer loop run 10 times and inner loop run 5 times.

13 Exercise 2 Ask for a user input of a number between 1-10. Enter a while loop that will loop exactly the number of times of the input. Inside this while loop, enter a number a for loop that will display “Hello world” 5 times.

14 Exercise 3 Create this Rectangle: 10101010101010101010 It’s a 10 x 5 that prints 10 consistently

15 Exercise 4 Create the triangle 12345678910 123456789 12345678 1234567 123456 12345 1234 123 12 1

16 Exercise 5 Create the Pyramid 1 12 123 1234 12345 123456 Keep going until you reach 10.


Download ppt "Nested For Loops. First, the rules to these loops do not change. We are sticking one loop inside another. while(condition) { // loop body } do { // loop."

Similar presentations


Ads by Google