Nested For Loops First the rules to these



















- Slides: 19
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 body }while(condition); for(integer initialization; condition; iteration) { // loop body }while(condition);
while(condition) { // loop body } } do { // loop body }while(condition); for(integer initialization; condition; iteration) { // loop body } }
Example 1 int n = 10; while(n == 10) { while(n > 0) { n--; } }
Example 2 int n = 10; do { n--; }while(n > 0); }while(n == 10);
Example 3 for(int k = 0; k < 10; k++) { for(int j = 0; j < 10; j++) { System. out. println(“Hello World”); } }
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” }
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. //
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++; }
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);
What’s the output? for(int k = 0; k < 5; k++) { for(int j = 0; j < 5; j++) { System. out. println(“Hello World”); }
Exercise 1 Print “Hello World” 50 times using two nested for loops. Let outer loop run 10 times and inner loop run 5 times.
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.
Exercise 3 Create this Rectangle: 10 10 10 10 10 10 10 10 It’s a 10 x 5 that prints 10 consistently 10 10 10 10 10
Exercise 4 -A Create the triangle 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 6 6 6 7 7 8 8 8 9 9 10
Exercise 4 -B Create the triangle 1 1 1 1 1 2 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 6 6 6 7 7 8 8 8 9 9 10
Exercise 5 Start a while loop and give me two options: 1. Keep going 2. Exit This loop breaks if I choose option 2. If I choose option 1, enter another loop that keeps generating random numbers until we have a number that is divisible by 7. Keep outputting this random number.
Exercise 6 Start a while loop and give me two options: 1. Keep going 2. Exit This loop breaks if I choose option 2. If I choose option 1, keep generating 3 random numbers until the sum of the three numbers add up to a multiple of 5. Display the 3 numbers and their sum every time you loop.
Exercise 7 Start a while loop and give me two options: 1. Keep going 2. Exit This loop breaks if I choose option 2. If I choose option 1, ask me to input and integer value of greater than 1000. Once that number is in, give me the prime factorization of that number. Every non prime number has a prime factorization. For example, the prime factorization of 100 is (2*2*5*5). If the number is a prime, let me know so.