1
Unit 11
Nested Loops
2
What Can Go Inside?
• What kind of code can we put in the body of a loop?
• ANYTHING…even other loops
while (condition) for( init; condition; update)
{ {
// What can go here? // What can go here?
} }
False init
condition False
condition
True What code can we put True
while Block in the body of a For Block
loop?
Statements Statements
Following Update
statements
Following
3
Nested Loops
• Loops can contain other loops in their body
False
cond1
while (cond1) { True
// code1
while(cond2) {
code1
// code 2 False
} cond2
// code3
}
True
code2
code3
Following
statements
4
Nested Loop Sequencing
• Key Idea: The inner loop runs in its entirety for each
iteration of the outer loop
Cond1: T T F False
cond1
while (cond1) { 1 9 15
// code1 2 10 True
Cond2: T T F
while(cond2) { 3 5 7 11 13 code1
Cond2: T F // code 2 12
4 6 False
}
// code3 8 14 cond2
} True
16 code2
code3
Following
statements
5
Nested Loops Example 1
• When you write loops int main()
{
consider what the body int secret, guess;
char again = 'y';
of each loop means in // outer loop
while(again == 'y')
an abstract sense { // Choose secret num. 0-19
secret = rand() % 20;
– The body of the outer guess = -1;
// inner loop
loop represents 1 game while(guess != secret)
1 game
(and we repeat that {
1 turn
cout << "Enter guess: ";
over and over) cin >> guess;
}
– The body of the inner cout << "Win!" << endl;
cout << "Play again (y/n): ";
loop represents 1 turn cin >> again;
(and we repeat turn }
return 0;
after turn) }
6
Nested Loops Example 2
• Key idea: Perform all int main()
{
iterations of the inner loop for(int i=0; i < 2; i++){
for(int j=0; j < 3; j++){
before starting the next cout << i << " " << j << endl;
}
iteration of the outer loop }
}
– Said another way: The inner
loop executes completely for i j
each single iteration of the
outer loop
• Trace through the execution
of this code and show what
will be printed
7
Nested Loops Example 3
• Trace through the execution int main()
{
of this code and show what int x = 0;
cin >> x;
will be printed if the user while( x%2 == 0 ){
for(int i=x; i >= 0; i -= 2){
types in: 8 4 7 6 cout << i << " ";
}
cout << endl;
cin >> x;
}
cout << "Done" << endl;
return 0;
}
Program Output:
8
break Statement with Nested Loops
• break will only exit the inner- char again = 'y';
while(again == 'y' )
most loop, not all the nested {
/* Give the user 10 turns
loops. but stop if guess right */
• This can be exactly what you int i, guess, secretNum = /*..*/
for(i=0; i < 10; i++)
want in some cases {
cin >> guess;
• In other cases, you may want to if(guess == secretNum){
break;
break out of all loops, but }
realize a single 'break' }
if( i == 10 )
statement cannot do that. cout << "You lose!" << endl;
else
– Instead must change a variable so cout << "You win!" << endl;
that the outer loop condition will
cin >> again;
fail }
9
Tips
• Nested loops often help us represent and
process multi-dimensional data
– 2 loops allow us to process data that corresponds
to 2 dimension (i.e. rows/columns)
– 3 loops allow us to process data that corresponds
to 3 dimensions (i.e. rows/columns/planes)
10
More Practice
• cpp/nestedloops/rectangle
• cpp/nestedloops/flag
• cpp/nestedloops/etox-range
• cpp/nestedloops/sphere
11
SOLUTIONS
12
Nested Loops Example 2
• Trace through the execution int main()
{
of this code and show what for(int i=0; i < 2; i++){
for(int j=0; j < 3; j++){
will be printed cout << i << " " << j << endl;
}
}
}
i j
0 0
1
Program Output: 2
1 0
0 0
0 1
1
0 2 2
1 0
1 1
1 2
13
Nested Loops Example 3
• Trace through the execution int main()
{
of this code and show what int x = 0;
cin >> x;
will be printed if the user while(x%2 == 0){
for(int i=x; i >= 0; i -= 2){
types in: 8 4 7 6 cout << i << " ";
}
cout << endl;
cin >> x;
}
cout << "Done" << endl;
return 0;
}
Program Output:
8 6 4 2 0
4 2 0
Done