# Nesting loop statements

• Introduction

• A loop (while or for) statement can occur within another loop statement - this is called as nested loop

• Here, I just want to show you an example of the use of nested loops

We will see more examples later when we learn arrays and matrices.

• Review: Syntax of the while and for statements

• The syntax structure of the while   statement is as follows:

 ``` while ( Condition ) ONE-single-statement // While body ```

• The syntax structure of the for   statement is as follows:

 ``` for ( initialization ; Condition ; Update ) ONE-single-statement // for body ```

• Nesting while and for statements

• As you can smell what is coming, we can use another while   statement or for   statement statement as ONE-single-statement   in a loop statement

• When we nest for   statements, we must be careful NOT to use the SAME variable in the for   running index

In other words, NEVER do this:

 ``` for ( i = 1; i < 10; i++ ) { for ( i = 1; i < 20; i++ ) { .... Same variable (i) is used in both loops... } } ```

ALWAYS use different loop index variables, e.g.:

 ``` for ( i = 0; i < 10; i++ ) { for ( j = 0; j < 20; j++ ) { .... } } ```

• Illustrating the nested for statement

• The best way to see the effect of nesting loops is through an example.

• Consider the following for loop:

 ``` for ( j = 0; j < 10; j++) { ....(statements) } ```

• Let us ignore the statements that are executed by the for loop and focus on the loop index variable j

The following figure depicts the sequence of values that the variable j will assume when the for loop is executed:

• Now let us consider the following nested for loop

• The outer for loop is first started:

1. i = 0

2. Then the statement pertaining to the outer for is executed:

This statement is the inner for loop

(And we have just seen how this for loop is executed)

3. The following figure depict the exection of the first iteration of the outer for loop (for the case that i = 0):

•  ``` for ( i = 0; i < 2; i++) { for ( j = 0; j < 10; j++) { ....(statements) } } ```

• After the inner for loop finishes, the outer for loop continues and the index i is increased to i=1:

1. i = 1

2. Then the statement pertaining to the outer for is executed:

This statement is the inner for loop

(And we have just seen how this for loop is executed)

3. The following figure depict the exection of the second iteration of the outer for loop (for the case that i = 1):

• After the inner for loop finishes, the outer for loop continues and the index i is increased to i=2:

1. i = 2

2. Then the statement pertaining to the outer for is executed:

This statement is the inner for loop

(And we have just seen how this for loop is executed)

3. The following figure depict the exection of the second iteration of the outer for loop (for the case that i = 2):

• After the inner for loop finishes, the outer for loop continues and the index i is increased to i=3.

The loop condition of the outer for is not false and the outer for loop terminates - the statement pertaining to this for loop will no longer be executed (which means that the inner for loop will not be run !)

• The following figure depicts the execution of the entire nested for loop:

 ``` for ( i = 0; i < 2; i++) { for ( j = 0; j < 10; j++) { ....(statements) } } ```

• Example of nested for statement

• Problem statement:

 Write a program that reads in an integer number N and print all prime numbers that is less than or equal to N

• We already have a program that can determine is a number n is prime (see: click here )

 ``` boolean isPrime; int f, n; isPrime = true; // Assume that n is prime... for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } if ( isPrime ) System.out.println( n + " is a prime number"); ```

• All we need to do is to use this program over and over again, for n = 2, 3, 4, ..., N

 ``` boolean isPrime; int f, n, N; for ( n = 2; n <= N; n++ ) { isPrime = true; // Assume that n is prime... for ( f = 2; f <= n - 1 ; f++ ) { if (n % f == 0) then isPrime = false; // Now we know n is NOT prime... } if ( isPrime ) System.out.println( n + " is a prime number"); } ```

• Example Program: (Demo above code)

• Nice "How To" in Book

• The "How To 7.1" on page 254 in your book is a nice write up on how to construct loops