0% found this document useful (0 votes)
0 views15 pages

(Lesson 5) Nested Loops

The document provides an overview of nested loops in programming, explaining their structure and usage. It includes multiple examples demonstrating how to create patterns using nested loops, including user input for rows and columns, and variations such as printing 'X' and 'O' based on column indices. Additionally, it covers generating a multiplication table using nested loops.

Uploaded by

sskx55nchb
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)
0 views15 pages

(Lesson 5) Nested Loops

The document provides an overview of nested loops in programming, explaining their structure and usage. It includes multiple examples demonstrating how to create patterns using nested loops, including user input for rows and columns, and variations such as printing 'X' and 'O' based on column indices. Additionally, it covers generating a multiplication table using nested loops.

Uploaded by

sskx55nchb
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/ 15

Nested Loops

Dr. Ali Allam Introduction to Programming (BIS227E)


Nested Loops: Overview

 A loop can contain any kind of statement(s) inside its body, including
another loop. One loop inside another is called a nested loop.
 The counter variable of the inner loop should have a different name,
so that it will not conflict with the outer loop.
 A nested loop has the following syntax, as an example:
In this example, the inner loop is
repeated 5 times for each of the10
rounds of the outer loop.
Example (1): A Simple Pattern

 In the shown example, the first thing to do is public class Prog1


ask yourself, how many rows and columns do {
public static void main(String[] args)
you have? {
* * * * * * * * * * for (int r=1; r<=6; r++)
* * * * * * * * * * {
* * * * * * * * * * for (int c=1; c<=10; c++)
{
* * * * * * * * * *
System.out.print(" * ");
* * * * * * * * * * }
* * * * * * * * * * System.out.println();
}
 You have 6 rows and 10 columns. So, the outer loop }
counts from 1 to 6, and the inner loop counts from 1 }
to 10.
Example (1) explained
 The outer loop controls the number of rows, and the
inner loop controls the number of columns.
for (int r=1; r<=6; r++)
{  This means that for each of the 6 rows, there are 10
for (int c=1; c<=10; c++) columns.
{
System.out.print(" * ");  At each column, an asterisk (*) is printed. All 10
} asterisks are printed beside each other.
System.out.println();
}
 After the inner loop finishes its 10 rounds, the
program then makes a new line.
 The outer loop now starts its 2nd round, and again
the inner loop is repeated 10 times in the same
exact way, and so on.
Example (2): More interaction (input)

 How about asking the user to enter the number of rows and the
number of columns for the pattern…

Output Console
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
Example (2): More interaction (input)
import javax.swing.*;
public class Prog2
{
public static void main(String[] args)
{
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter the numnber of rows:"));
int y = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of columns:"));
for (int r=1; r<=x; r++)
{
for (int c=1; c<=y; c++)
System.out.print(" * ");
System.out.println();
}
}
}
Can you display
the output in a
message box?
Try it ☺
Example (3): Switching (X and O)

 In this example, if the column index is even, the program will print an
“X” letter. If it is odd, it will print an “O”.
for (int r=1; r<=4; r++)
{
for (int c=1; c<=6; c++) Output Console
{
if(c%2==0) O X O X O X
Prog3.java
System.out.print(" X "); O X O X O X
else O X O X O X
System.out.print(" O "); O X O X O X
}
System.out.println();
}
Example (4): A changing number of columns

In this example, the number of columns is different at each row (i.e. you don’t
have a fixed number of columns at each row). So, on the 1st row you need to
make one column, at the 2nd row you need two columns, and so on.
Prog4.java Output Console
for (int r=1; r<=6; r++) X
{ X X
for (int c=1; c<=r; c++) X X X
System.out.print(" X ");
X X X X
System.out.println();
X X X X X
} X X X X X X
Now, let’s work a little bit with numbers!
Nested Loops: More examples

 Keep in mind that nested loops work in a two-dimensional pattern


(i.e. rows and columns).
 Now, let’s try using nested loops with numbers …
 Printing out a two-dimensional pattern of numbers
 Printing out the multiplication table
 Printing out the prime numbers
Example (5): Pattern of numbers
 In this example, the program prints the column’s index (column number).
public class Prog5
{
public static void main(String[] args)
{ Output Console
for (int r=1; r<=5; r++)
{ 123456789
for (int c=1; c<=9; c++) 123456789
System.out.print(c); 123456789
123456789
System.out.println();
123456789
}
}
}
Example (6): Pattern of numbers
 In this example, if the row is even, the program will print the column’s index. Otherwise,
if the row is odd, the program prints asterisks.
for (int r=1; r<=5; r++)
{
for (int c=1; c<=9; c++)
{ Output Console
if(r%2==0) XXXXXXXXX
System.out.print(c); 123456789
else
XXXXXXXXX
System.out.print("X");
} 123456789
System.out.println(); XXXXXXXXX
}
Example (7): Multiplication Table

 The user is asked to enter the number of rows and columns, and
accordingly the program prints the times table of those numbers.

Output Console
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
Example (7): Multiplication Table
import javax.swing.*;
public class Prog7
{
public static void main(String[] args)
{
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of rows: "));
int y = Integer.parseInt(JOptionPane.showInputDialog("Enter the number of columns:"));
for (int r=1; r<=x; r++)
{
for (int c=1; c<=y; c++)
System.out.print(r*c + "\t");
System.out.println();
}
}
}

You might also like