Nested loop

Java nested-loop

What are Loops?

In the Article, we will Discuss about the Nested Loops of java.
In programming, loops are used to execute a block of code repeatedly for a specified number of times or until a condition is met. The idea behind a loop is to automate repetitive tasks and save time by executing the same code multiple times without having to write it out every time.

Java Loops:

Loops are a fundamental construct in programming and are used in almost every application. In Java, loops are used to execute a block of code repeatedly for a specified number of times or until a condition is met.

Types Of Loops in Java:

In Java, there are three types of loops:

Syntax:

for (initialization; condition; increment/decrement) {
   // code to be executed repeatedly
}

Syntax:

while (condition) {
   // code to be executed repeatedly
}

Syntax:

do {
   // code to be executed repeatedly
} while (condition);

Java Nested Loop:

A nested loop in Java is a loop inside another loop. It is a way of repeating a set of statements multiple times within another set of statements. Nested loops are often used when working with two-dimensional arrays or matrices.

Java Example Program Of Nested Loop in For Loop:

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

Explanation:

In this example, the outer loop starts with i = 1 and iterates up to i = 10. The inner loop starts with j = 1 and iterates up to j = 10. The System.out.print(i * j + ” “) statement is executed for each combination of i and j, resulting in a multiplication table being printed out. The System.out.println() statement is used to move to the next line after each row.

Nested loops can be useful in a variety of programming scenarios, such as when you need to iterate through a multi-dimensional array, generate all possible combinations of values, or perform a search over a set of data. However, it’s important to use them judiciously as they can easily result in inefficient code if used excessively.

Java Example Program Of Nested Loop in While Loop:

Run
public class NestedLoopExample {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 5) {
            int j = 1;
            while (j <= i) {
                System.out.print("*");
                j++;
            }
            System.out.println();
            i++;
        }
    }
}

Output:

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

Explanation:

In this example, we have a while loop that runs until i is greater than 5. Within this while loop, we have another while loop that prints out i number of asterisks on each line. The inner loop runs from j=1 to j=i, so the number of asterisks printed on each line increases by 1 each time.

Java Example Program Of Nested Loop in Do While Loop:

Run
public class NestedLoopExample {
    public static void main(String[] args) {
        int i = 1;
        do {
            int j = 1;
            do {
                System.out.print("*");
                j++;
            } while (j <= i);
            System.out.println();
            i++;
        } while (i <= 5);
    }
}

Output:

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

Explanation:

In this example, we have a do-while loop that runs until i is greater than 5. Within this do-while loop, we have another do-while loop that prints out i number of asterisks on each line. The inner loop runs from j=1 to j=i, so the number of asterisks printed on each line increases by 1 each time.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription