Nested while loop in Java programming language
- Home
- nested while loop
- Nested while loop in Java programming language
- On
- By
- 0 Comment
- Categories: nested while loop, Number pattern, pyramid triangle
Nested while loop in Java programming language
Nested while loop in Java programming language
We will learn this tutorial about Nested while loop in Java programming language
Nested while loop
When a while loop exists inside the body of another while loop, it is known as nested while loop in Java. Initially, the outer loop executes once and the afterwards inner loop begins to execute. Execution of the inner loop continues until the condition of the inner loop is satisfied(until the test expression is false).
Once the Condition of the inner loop is satisfied, the flow of control comes out to the outer loop for next iteration.
Declaration
Syntax
while(expression) { statement(s) while(expression) { statement(s) } }
Flow diagram for the nested while loop
Example for nested while loop in Java
Example
class nestedwhile{ public static void main(String args[]){ int i=1; while(i<=3){ System.out.println("\n"+i+" "+"outer loop executed only once\n"); int j=1; while(j<=4) { System.out.println(j+" "+"inner loop executed until to completion"); j++; } i++; } } }
When we execute the above program, it produces the following results
1 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion 2 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion 3 outer loop executed only once 1 inner loop executed until to completion 2 inner loop executed until to completion 3 inner loop executed until to completion 4 inner loop executed until to completion
Program 1
Print rectangular number pattern using nested while loop in Java
class nestedwhile{ public static void main(String args[]){ int i=1,j=1; while(i<=10) { while(j<=10) { System.out.print(j); j++; } i++; System.out.println(""); j=1; } } }
When we execute the above program, it produces the following results
12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910 12345678910
Program 2
Print rectangular star pattern using nested while loop in Java
class nestedwhile{ public static void main(String args[]){ int i=1,j=1; while(i<=10) { while(j<=10) { System.out.print("*"); j++; } i++; System.out.println(""); j=1; } } }
When we executed above program, produces the following result
********** ********** ********** ********** ********** ********** ********** ********** ********** **********
Program 3
Print triangle star pattern using nested while loop in Java
class nestedwhile{ public static void main(String args[]){ int i=1,j=1; while(i<=10) { while(j<=i) { System.out.print("*"); j++; } i++; System.out.println(""); j=1; } } }
When we execute above program, produces the following result
* ** *** **** ***** ****** ******* ******** ********* **********
Similar post
Nested for loop in C++ language
Nested while loop in C++ language
Nested for loop in Java language
Nested while loop in Java language
Suggested for you
Three dim Array in C++ language
Single dim Array in Java language
Two dim Array in Java language
Three dim Array in Java language
Single dim Array in C language