![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
“The process of repeatedly executing a block of statements is known as looping.”
If a loop continues forever, it is called as infinite loop. Just like the sun arises on the earth every day.
In looping, statements block of statement is executed until some conditions for the termination of the loop occurred.
While loop is the simplest looping structure. While loop is entry
controlled loop statement. That means the condition is given at the beginning of loop. If the given condition does not satisfy , the loop statements never get executed.
Syntax
while (test-condition) { // body of while loop //increment/decrement counter }
class WhileLoopEx { public static void main(String args[]) { int start = 1; int end = 10; while ( start <= end) { System.out.println(start); start ++; } } }
Output
Before entering into the loop, the value of start = 1 and value of end = 10
Iteration | Condition while (start <= end) | Result | start |
---|---|---|---|
Iteration 1 | while (1 <= 10) | true | 2 |
Iteration 2 | while (2 <= 10) | true | 3 |
Iteration 3 | while (3 <= 10) | true | 4 |
Iteration 4 | while (4 <= 10) | true | 5 |
Iteration 5 | while (5 <= 10) | true | 6 |
Iteration 6 | while (6 <= 10) | true | 7 |
Iteration 7 | while (7 <= 10) | true | 8 |
Iteration 8 | while (8 <= 10) | true | 9 |
Iteration 9 | while (9 <= 10) | true | 10 |
Iteration 10 | while (10 <= 10) | true | 11 |
Iteration 11 | while (11 <= 10) | false | 11 |
If we have only single statement in the body of loop,then we can write it without using curly braces.
Example
class WhileLoopEx { public static void main(String args[]) { int i=1,add=0; while(i<=5) add += i++; System.out.println("add = "+add); } }
Output
Below program shows the numbers between 1 to 5
class WhileLoopEx { public static void main(String args[]) { int a=0; while(a++ < 5) { System.out.println("a : "+a); } } }
Output
Below program checks the float condition and adds float value by 1.0
class WhileLoopEx { public static void main(String args[]) { float a = 1.0f; double add = 0; while(a<=5.5) { add = add +a ; a=a+0.5f; } System.out.println("addition : "+add); } }
Output
Below program doesn't give any output at all. due to ; after while. Compiler will keep rotating eternally.
class WhileLoopEx { public static void main(String args[]) { int a=0; while(a < 10); { a++; System.out.println("a : "+a); } System.out.println("final a : "+a); } }
Output
No output will be displayed.
Below program will start incrementing the value from 1 to infinite.
class WhileLoopEx { public static void main(String args[]) { int a=1; while(a > 0) { a++; System.out.println("a : "+a); } } }
Output