Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: whiledo while, or for. For example, the inner loop can be a while loop while the outer loop can be a for loop. Of course, they can be the same kind of loops, too.

Both, Java and Python allow to use one loop inside another loop. Following section shows a few examples to illustrate the concept.

Nested while loops

The following program uses a nested for loop to find the prime numbers from 2 to 100:

Code Block
languagejava
titleJava nested loops
// prime numbers from 2 to 100
int i = 2;
while(i < 100){
	int j = 2;
    while(j <= (i / j)){
        if (i % j == 0) break;
        j = j + 1;// or j++;
    }   
    if (j > i/j) System.out.println(i + " is prime");
    i = i + 1; // or i++;    
}
System.out.println("Good bye!");
Code Block
languagepy
titlePython nested loops
# prime numbers from 2 to 100

i = 2
while(i < 100):
   j = 2
   while(j <= (i/j)):
      if not(i%j): break
      j = j + 1
   if (j > i/j) : print i, " is prime"
   i = i + 1

print "Good bye!"

Nested for loops

You can often use for-loops to make code easier to read. The following code with nested for-loops prints...

Code Block
languagejava
titleStrings in Java nested loops
// Print the below statement 3 times 
for (int number = 0; number < 3; number++) {
    System.out.println("-------------------------------------------");
    System.out.println("I am outer loop iteration " + number);
    // inner loop
    for (int another_number = 0; another_number < 5; another_number++) {
        System.out.println("****************************");
        System.out.println("I am inner loop iteration " + another_number);
    }
} 
Code Block
Strings in Python
languagepy
titlePython nested loops
# Print the below statement 3 times
for number in range(3) :  
    print("-------------------------------------------")
    print("I am outer loop iteration "+str(number))
    # Inner loop
    for another_number in range(5):  
        print("****************************")
        print("I am inner loop iteration "+str(another_number))
		break

You will find out that the control enters the first for loop and the value of the variable number is initialized as 0. The first print statement is printed, and then control enters the second for loop, where the value of the variable another_number is initialized to 0. The first print statement in the second for loop is printed once.

Now, the control returns to the inner for loop once again and the value of another_number is again initialized to the next integer followed by printing the statement inside the println() function.

The aforementioned process continues until the control has traversed until another_number reaches value 4, and then the control returns back to the outermost loop, initializes the variable number to the next integer, prints the statement inside the println() function, visits the inner loop and then repeats all of the above steps until the value of variable number reaches 2;

This journey of the control traveling from the outermost loop, traversing of the inner loop and then back again to the outer for loop continues until the control has covered the entire range, which is 3 times in your case.