# Subgoals

## Section7.12Worked Example: Nested Loops

### Subgoals for Evaluating a Loop.

1. Identify loop parts
1. Determine start condition
2. Determine update condition
3. Determine termination condition
4. Determine body that is repeated
2. Trace the loop
1. For every iteration of loop, write down values

### Subsection7.12.1

You can watch this video or read through the content below it.
Problem: Given the following code, what is the output?
int i = 0, j = 0, m = 8, n = 5;
while (i < n)
{
j = 0;
while (j < m)
{
System.out.print("*");
j++;
}
System.out.println();
i++;
}


### Subsection7.12.3SG2: Define and initialize variables

For now, we begin with the variables that control the outer loop.
Start:
i = 0, n = 5

End:
i >= 5


### Subsection7.12.4SG3: Trace the loop

Think of an analog clock:
Second hand goes completely around before minute hand moves
Just like inner loop:
Inner loop must finish before outer loop increments once
Looking at ONLY the outer loop: <NL> is a new line
Now Repeat for inner loop

### Subsection7.12.6SG2: Define and initialize variables

Start:
j = 0, m = 8

End:
j >= 8


### Subsection7.12.7SG3: Trace the loop

Looking at Only the inner loop:
For each iteration of the inner loop, a single star is added to the current output on the same line. For each iteration of the outer loop, the current line is ended and a new line begins. The output looks like:
Note that outer loop goes 5 times,
Inner loop goes 8 times (for each outer loop)