Nested Loops in Java22 Mar 2025 | 5 min read The English meaning of nested is inside another. It means nested loops are the loop statement inside another loop statement. In simple terms, the loop within a loop is called a nested loop. The inner loop runs fully before the outer loop moves to the next cycle. It is useful for handling repeated tasks, such as working with grids, printing patterns, or processing multi-layered data like schedules and tables. In this section, we will discuss the types of nested loops in Java. In Java, there are mainly following three types of nested loops:
Let’s discuss one by one. Nested for LoopA nested for loop is a for loop that is placed inside another for loop. The inner loop runs completely for each cycle of the outer loop before it moves to the next iteration. Syntax: Example of Nested for LoopFile Name: Main.java ExampleCompile and RunOutput: Outer: 1, Inner: 1 Outer: 1, Inner: 2 Outer: 1, Inner: 3 Outer: 2, Inner: 1 Outer: 2, Inner: 2 Outer: 2, Inner: 3 Outer: 3, Inner: 1 Outer: 3, Inner: 2 Outer: 3, Inner: 3 Outer: 4, Inner: 1 Outer: 4, Inner: 2 Outer: 4, Inner: 3 Use Cases of Nested for Loop
Nested while LoopA nested while loop is a while loop that is placed inside another while loop. The inner loop runs completely for each iteration of the outer loop before the outer loop moves to the next step. Syntax: Example of Nested while LoopFile Name: Main.java ExampleCompile and RunOutput: Outer: 1, Inner: 1 Outer: 1, Inner: 2 Outer: 2, Inner: 1 Outer: 2, Inner: 2 Outer: 3, Inner: 1 Outer: 3, Inner: 2 Use Cases of Nested while Loops
Nested do-while LoopA nested do-while loop is a do-while loop that is placed inside another do-while loop. The inner loop runs completely for each cycle of the outer loop before moving to the next iteration of the outer loop. Do-while loops, in contrast to while and for loops, ensure that the loop will run at least once, even in the case in which the condition is false. Syntax: Example of Nested while LoopFile Name: Main.java ExampleCompile and RunOutput: Outer: 1, Inner: 1 Outer: 1, Inner: 2 Outer: 2, Inner: 1 Outer: 2, Inner: 2 Outer: 3, Inner: 1 Outer: 3, Inner: 2 Use Cases of Nested do-while Loop
Hybrid Nested LoopsIt is not necessary that a loop must be nested inside its own type. We can put any type of loop inside a different type of loop. It is called a hybrid nested loop. For example, there may be while inside for loop, while inside do-while loop, for inside do-while loop, etc. Syntax: Example 1: while Loop Inside a for LoopFile Name: Main.java ExampleCompile and RunOutput: Outer: 1, Inner: 1 Outer: 1, Inner: 2 Outer: 2, Inner: 1 Outer: 2, Inner: 2 Outer: 3, Inner: 1 Outer: 3, Inner: 2 Example 2: do-while Loop Inside a while LoopFile Name: Main.java ExampleCompile and RunOutput: Outer: 1, Inner: 1 Outer: 1, Inner: 2 Outer: 2, Inner: 1 Outer: 2, Inner: 2 Outer: 3, Inner: 1 Outer: 3, Inner: 2 Use Cases of Hybrid-Nested Loop
Advantages of Nested Loops
Next TopicWhy Java is Secure |
The chromatic numbers are generally used in the coloring of graph nodes with some constraints. Chromatic Numbers in Java tell the minimum number of unique colors required to color all the nodes of a graph such that any two adjacent nodes do not have the same...
5 min read
Generic is used for creating Java code for graphs. Java's HashMap class is used to implement the Graph class. As we know, HashMap has a key and a value; in the graph, nodes are represented as keys, and their adjacency is listed as values. What is Generic? Generics...
9 min read
In object-oriented programming, communication between objects is a vital aspect of building complex systems. One of the key mechanisms for achieving this communication is message passing. In Java, message passing allows objects to interact with each other by invoking methods and passing data between them. In...
5 min read
The Find Triplets with Zero Sum problem involves identifying three numbers in an array that add up to zero. The challenge is common in coding interviews and helps improve understanding of array manipulation and sorting techniques. Efficient solutions often leverage sorting and two-pointer methods for optimal...
12 min read
In Java, graphical user interfaces (GUIs) play a crucial role in creating interactive applications. One of the key aspects of GUI programming is the layout manager that determines how components are arranged within a container. The border layout manager is one such layout manager that simplifies...
4 min read
In Java, an operator is a symbol that performs specified operations. In this section, we will discuss only the bitwise operator and its types with appropriate examples. Types of Bitwise Operators There are six types of bitwise operators in Java: Bitwise AND Bitwise Exclusive OR Bitwise Inclusive OR Bitwise Complement Bit Shift...
7 min read
The noneMatch() method in Java's Stream API is an essential function employed to evaluate whether none of the elements in a given stream satisfy a particular condition. It is particularly useful when we need to prove that none of the items in a collection match...
11 min read
In Java, the valueOf() method is a static method defined in many classes, primarily in the wrapper classes of primitive data types like Integer, Double, Boolean, etc. This method is used to create an object of the respective wrapper class from a string representation of a...
4 min read
The java.nio.DoubleBuffer has a mark() function. The current position of this DoubleBuffer is marked as the buffer's mark using the DoubleBuffer Class. Syntax: public DoubleBuffer mark() Return Value: The buffer's mark is set to the current position and the buffer is returned by this method. Example...
3 min read
One of the most important applications of computer vision is face detection which is applied in many fields like security systems, identification systems as well as image processing systems. Although there is numerous Java face detection software available, one which is worth mentioning is OpenCV, an...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India