Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
205 views

Nested Loops

The document discusses using for loops to print patterns, including nested for loops. It provides examples of nested loops that print multiplied numbers in a grid, as well as examples that print patterns of characters like asterisks and dots. The examples are used to demonstrate how to structure the nested loops to control the number of iterations of the inner and outer loops to produce the desired output patterns. Mistakes like using the wrong iteration variable in loop conditions that can result in infinite loops are also highlighted.

Uploaded by

sivaabhilash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views

Nested Loops

The document discusses using for loops to print patterns, including nested for loops. It provides examples of nested loops that print multiplied numbers in a grid, as well as examples that print patterns of characters like asterisks and dots. The examples are used to demonstrate how to structure the nested loops to control the number of iterations of the inner and outer loops to produce the desired output patterns. Mistakes like using the wrong iteration variable in loop conditions that can result in infinite loops are also highlighted.

Uploaded by

sivaabhilash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Printing with for Loops

To print a character multiple times, use a for loop.


for (int j = 1; j <= 4; j++) {
System.out.print(".");
// 4 dots
}
int i = ... ; // i is some positive int
for (int j = 1; j <= i; j++)
System.out.print(*); //output?

based on slides at buildingjavaprograms.com

Nested Loops

nested loop: A loop placed inside another loop.


for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print((i * j) + "\t");
}
System.out.println(); // to end the line
}

Output:
1
2
3
4

2
4
6
8

3
6
9
12

4
8
12
16

5
10
15
20

Statements in the outer loop's body are executed 4 times.


The inner loop prints 5 numbers each time it is run.

Nested for Loops Example


What is the output of the following nested for loops?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print("*");
}
System.out.println();
}

Nested Loops Example


What is the output of the following nested for loops?
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Write nested loops that display:
1
22
333
4444
55555
666666

Another Nested Loops Example

What nested for loops produce the following output?


inner loop - prints one line
....1
...2
..3
outer loop - loops 5 times since there
.4
are 5 lines
5

We must build multiple complex lines of output using:


an outer "vertical" loop for each of the lines
inner "horizontal" loop(s) for the patterns within each line

Outer and Inner Loops

First write the outer loop, from 1 to the number of lines.

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


...
}
Now look at the line contents. Each line has a pattern:
some dots - how many? How does number of dots relate to the line
number?
a number - how does it depend on line number?
....1
...2
..3
.4
5

Nested for Loops Exercise


What is the output of the following nested for loops?
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= 5 - line; j++)
{
System.out.print(".");
}
for (int k = 1; k <= line; k++) {
System.out.print(line);
}
System.out.println(); // new line
}

Solution
Answer:
....1
...22
..333
.4444
55555

Exercise
Modify the previous code to produce this output:
....1
...2.
..3..
.4...
5....

Solution
for (int line = 1; line <= 5; line++) {
for (int j = 1; j <= (5 - line); j++){
System.out.print(".");
}
System.out.print(line);
for (int j = 1; j <= (line - 1); j++) {
System.out.print(".");
}
System.out.println();
}

Common Mistakes

Both of the following sets of code produce infinite loops:

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


for (int j = 1; i <= 5; j++) {
System.out.print(j);
}
System.out.println();
}
for (int i = 1; i <= 10; i++) {
for (int j = 1; j <= 5; i++) {
System.out.print(j);
}
System.out.println();
}

You might also like