0% found this document useful (0 votes)
203 views10 pages

Lecture 10 Nested For

Nested for loops allow loops to be placed inside one another, creating a loop of loops. The inner loop must use a different variable name than the outer loop. Common bugs include using the wrong loop variable name. Well commented code explains the conceptual purpose of loops rather than syntax details.

Uploaded by

Munir Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views10 pages

Lecture 10 Nested For

Nested for loops allow loops to be placed inside one another, creating a loop of loops. The inner loop must use a different variable name than the outer loop. Common bugs include using the wrong loop variable name. Well commented code explains the conceptual purpose of loops rather than syntax details.

Uploaded by

Munir Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Nested for Loops

Based on slides for Building Java Programs by Reges/Stepp, found at


http://faculty.washington.edu/stepp/book/
Nested for loops
A for loop can contain any kind of statement in its body,
including another for loop.
The inner loop must have a different name for its loop counter
variable so that it will not conflict with the outer loop.
nested loop: Loops placed inside one another, creating a
loop of loops.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
printf(%s\n,seven");
}
}
Output:
seven
seven
seven
seven
seven
seven

CS305j Introduction to Computing Nested For Loops 2


More nested for loops
All of the statements in the outer loop's body are
executed 5 times.
The inner loop runs 10 times for each of those 5 times,
for a total of 50 numbers printed.

for (int i = 1; i <= 5; i++) {


for (int j = 1; j <= 10; j++) {
printf(%d , i * j);
}
printf(\n); // to end the line
}
Output:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50

CS305j Introduction to Computing Nested For Loops 3


Nested for loop exercise
What is the output of the following nested for
loop?
for (int row = 1; row <= 4; row++) {
for (int col = 1; col <= 5; col++)
{
printf("*");
}
printf(\n);
}

Output?

CS305j Introduction to Computing Nested For Loops 4


Nested for loop exercise
What is the output of the following nested for
loop?
for (int row = 1; row <= 6; row++) {
for (int col = 1; col <= row; col++) {
printf("*");
}
print(\n);
}

Output:
*
**
***
****
*****
******

CS305j Introduction to Computing Nested For Loops 5


Nested for loop exercise
What is the output of the following nested
for loop?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
printf(%d,i);
}
printf(\n);
}

Output?

CS305j Introduction to Computing Nested For Loops 6


Nested for loop exercise
Create a nested for loop to produce the
following output.

....1
...22
..333
.4444
55555

CS305j Introduction to Computing Nested For Loops 7


Nested for loop exercise
A for loop can have more than one loop nested in it.
What is the output of the following nested for loops?
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= (5 - i); j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
printf(%d,i);
}
printf(\n);
}
Answer:
1
22
333
4444
55555

CS305j Introduction to Computing Nested For Loops 8


Common nested loop bugs
It is a common bug to accidentally type the wrong loop
counter variable, which can lead to incorrect behavior.
What is the output of the following nested loops?
for (int i = 1; i <= 10; i++) {
for (int j = 1; i <= 5; j++) {
printf(%d,j);
}
printf(\n);
}
What is the output of the following nested loops?
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; i++) {
printf(%d,j);
}
printf(\n);
}

CS305j Introduction to Computing Nested For Loops 9


How to comment: for loops
Place a comment on complex loops explaining what they do from a
conceptual standpoint, not the mechanics of the syntax.
Bad:
// This loop repeats 10 times, with i from 1 to 10.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) { // loop goes 5 times
printf(%d,j); // print the j
}
printf(\n);
}
Better:
// Prints 12345 ten times on ten separate lines.
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; j++) {
printf(%d,j);
}
printf(\n); // end the line of output
}

CS305j Introduction to Computing Nested For Loops 10

You might also like