Nested Loops in Java

In this class, We discuss Nested Loops in Java.

The reader should have prior knowledge of loops in Java. Click Here.

The concept of nested loops is very important.

Most of the coding logic is written using nested loops.

In this class, we will understand the execution of nested loops.

In our next classes, we discuss a few practice examples.

The below diagram shows the example.

The loop statement was written inside a loop we call a nested loop.

For each execution of the outer loop, the inner loop will execute ten times.

The outer loop was executed ten times.

So the “hello” is displayed hundred times.

Example:

The below diagram shows an example.

The code will display “hello” hundred times and “how are you” ten times.

Note: The code inside the body of the outer loop should be examined carefully.

Three-level nested loops:

The below diagram shows the example.

The code will display “hello” thousand times.

The outer loop i = one the 2nd level loop j executes ten times.

For each 2nd level loop execution, the third level will execute ten times.

So Total, the display statement will execute thousand times.