Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views7 pages

Nested Loop

A nested loop is a loop that runs inside another loop, with the inner loop executing completely for each iteration of the outer loop. Common use cases include pattern printing, such as creating a diamond shape or other star patterns. Additionally, nested if statements allow for checking multiple conditions in a hierarchical manner, where one condition depends on another.

Uploaded by

zawars0005
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)
2 views7 pages

Nested Loop

A nested loop is a loop that runs inside another loop, with the inner loop executing completely for each iteration of the outer loop. Common use cases include pattern printing, such as creating a diamond shape or other star patterns. Additionally, nested if statements allow for checking multiple conditions in a hierarchical manner, where one condition depends on another.

Uploaded by

zawars0005
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/ 7

What Is a Nested Loop

A nested loop means one loop runs inside another loop. The inner loop runs completely for
each iteration of the outer loop.

Example in Java

java

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


for (int j = 1; j <= 2; j++) {
System.out.println("i = " + i + ", j = " + j);
}
}
Output:

i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2

How It Works
1. Outer loop starts with i = 1
o Inner loop runs: j = 1, j = 2
2. Outer loop moves to i = 2
o Inner loop runs again: j = 1, j = 2
3. Outer loop moves to i = 3
o Inner loop runs again: j = 1, j = 2

Common Use Case: Pattern Printing

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

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


System.out.print("* ");
}
System.out.println(); // Moves to next line

*
**
***
****

Formula for Total Iterations


If:

java
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
// code
}
Then total iterations = n × m

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


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

Concept
 Outer loop (i) runs 4 times: i = 1 to i = 4.
 Inner loop (j) depends on i. It prints * i times.
 System.out.println(); moves to the next line after inner loop.

i Inner loop (j from 1 to i) Output


1 j=1 *
2 j = 1, 2 **
3 j = 1, 2, 3 ***
4 j = 1, 2, 3, 4 ****

*
**
***
****

*
***
*****
*******
*********
*******
*****
***
*

public class DiamondPattern {


public static void main(String[] args) {
int n = 5; // height of upper half

// Upper half (including middle row)


for (int i = 1; i <= n; i++) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}

// Lower half (excluding middle row)


for (int i = n - 1; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= 2 * i - 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

How It Works:
 Upper half:
o Line 1: 4 spaces + 1 star
o Line 2: 3 spaces + 3 stars
o ...
o Line 5: 0 spaces + 9 stars
 Lower half:
o Reverses the process: 1 line fewer each time

nested if
What is a Nested if in Java?
A nested if means placing one if statement inside another if block. It lets you check more than
one condition, step by step.

if (condition1) {
if (condition2) {
// code runs if both condition1 and condition2 are true
}
}

int age = 20;


boolean hasID = true;

if (age >= 18) {


if (hasID) {
System.out.println("You are allowed to vote.");
}

You are allowed to vote.

How It Works:
1. Outer if (age >= 18) is checked first.
2. If true, then it goes inside to check if (hasID).
3. If both are true, it runs the print statement.

Why Use Nested if?


 You want one condition to depend on another.

Login Check Example:

String username = "rabia";


String password = "1234";

if (username.equals("rabia")) {

if (password.equals("1234")) {
System.out.println("Login Successful!");
} else {
System.out.println("Incorrect Password.");
}
} else {
System.out.println("User not found.");
}

You might also like