While Loop In Java – Executing a set of statements repeatedly is known as looping. We have 3 types of looping constructs in Java. These looping statements are also known as iterative statements.
- While
- For Loop
- Do While
All the three are used primarily with same purpose and the difference is in their syntax. Because of the syntactical differences, their behavior may differ a little bit. We will see the differences soon. More than the behavior it is programmer’s choice about what looping constructor to use when.
Example For While Loop – While Loop Basics
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class WhileLoopBasics { public static void main(String args[]) { System.out.println("hello"); System.out.println("hello"); System.out.println("hello"); System.out.println("hello"); System.out.println("hello"); System.out.println("hello"); } } |
Output:
1 2 3 4 5 6 7 |
output: hello hello hello hello hello hello |
Definition Of While Loop
This loop is used to execute a set of statements repeatedly as long as a condition is true.
Syntax:
1 2 3 4 5 |
while(condition) { --------- --------- } |
When control comes to this loop, the condition in the header is evaluated to check whether it results in true or false. If the condition is true, the control enters the loop body and executes the statements in it.
After executing the statements, the control automatically comes up to the condition part and checks whether the condition results in true (the code in body might have changed value of some variable so that the conditions truth value may change).
If the condition results in true, then control enters the body again to execute the statements in it. Then the control goes to condition part again.
This process is continued as long as the condition results in true. When the condition results in false, the control goes to the statement immediately following the loop.
Example Program for Java While Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class WhileLoopBasics { public static void main(String args[]) { int a=10; while(a>=1) { System.out.println("hello"); a=a-1; // or a--; } } } |
Execution Order:
1 2 3 4 5 6 7 8 9 10 11 |
a=10--->(a>=1)-->(10>=1)--->(true)----> prints "hello"-->a----->a=9 a=9--->(a>=1)-->(9>=1)--->(true)----> prints "hello"-->a----->a=8 a=8--->(a>=1)-->(8>=1)--->(true)----> prints "hello"-->a----->a=7 a=7--->(a>=1)-->(7>=1)--->(true)----> prints "hello"-->a----->a=6 a=6--->(a>=1)-->(6>=1)--->(true)----> prints "hello"-->a----->a=5 a=5--->(a>=1)-->(5>=1)--->(true)----> prints "hello"-->a----->a=4 a=4--->(a>=1)-->(4>=1)--->(true)----> prints "hello"-->a----->a=3 a=3--->(a>=1)-->(3>=1)--->(true)----> prints "hello"-->a----->a=2 a=2--->(a>=1)-->(2>=1)--->(true)----> prints "hello"-->a----->a=1 a=1--->(a>=1)-->(1>=1)--->(true)----> prints "hello"-->a----->a=0 a=0--->(a>=10)-->(0>=10)--->(false)---->exit from while loop |
Another Example Program For While Loop In Java:
1 2 3 4 5 6 7 |
int a=1,b=3; while(a<=b) { System.out.println(“mouse”); a++; } System.out.println(“cat”); |
In the above example, by the time control comes to header part of the loop, a is 1 and b is 3 so the condition a<=b results in true.
So the control comes into loop body and prints “mouse”. Then value of a is incremented by 1 so it becomes 2. Then the control comes-up to the condition part automatically and checks its truth value.
As value of a is 2 now, the condition results in true again. So the body executes and prints “mouse” again. Then a becomes 3.
Then the control comes to condition part again which results in true. So the loop body is executed third time to print “mouse” and to increment a by 1.
This time a is 4 so the condition results in false (4<=3). As the condition is false, the control comes to the statement after the loop and prints “cat”.
Loop Control Variables
The truth value of the condition depends on one or more variables. Such variables are known as loop control variables.
In the above example, the condition in loop header depends on the variable a and b. A change in any of these two variables will affect the truth value of the condition. In this particular example, value of b is not changed at all in the loop. So a is the primary loop control variable here.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class WhileLoopBasics { public static void main(String args[]) { int i=0,j=5; while(i<=j) { System.out.println(i); i++; } } } |
Output:
1 2 3 4 5 6 |
0 1 2 3 4 5 |
Body With Single Statement
Similar to if/else block, the while loop block also do not need curly braces when there is only one statement. So the following two examples will give same effect.
Eg1:
1 2 3 4 5 6 7 |
while(a++ < b) statementA; Eg2: while(a++ < b) { statementA; } |
Generally the loop body is executed N number of times and the condition is checked for N+1 number of times. For the first N times, the condition results in true and the (N+1)th time the condition results in false.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 |
class WhileLoopBasics { public static void main(String args[]) { int i=0; while(i<=5) System.out.println(i++); } } |
Output:
1 2 3 4 5 6 |
0 1 2 3 4 5 |
Infinite Loops
Sometimes we write a looping construct as if the condition results in true always. Such loops are known as infinite loops or indefinite loops.
Example:
1 2 3 4 5 6 7 8 9 10 |
while(true) { StatementA; StatementB; } Eg2: while(2<4) { StatementA; StatementB; } |
Example Program For Infinite Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
class WhileLoopBasics { public static void main(String args[]) { int a,b; while(true) System.out.println("InfiniteLoop...."); while(2==2) System.out.println("InfiniteLoop...."); a=2,b=2; while(a==b) System.out.println("InfiniteLoop...."); a=2,b=2; while(a<=b) System.out.println("InfiniteLoop...."); a=2,b=2; while(a>=b) System.out.println("InfiniteLoop...."); a=1,b=2; while(a<b) System.out.println("InfiniteLoop...."); a=2,b=1; while(a>b) System.out.println("InfiniteLoop...."); } } |
This kind of loops are used when we don’t know exactly when to come out of the loop. At middle of the loop body, we may need to come out depending on a resultant value after an expression is evaluated or a user entered value met a condition. When such a situation arises we can come out using a statement like ‘break’ or ‘return’.
Executing Part Of The Body In An Iteration
If we want to skip remaining statements of a loop in the current iteration (round), then we can use ‘continue’ statement at middle of a loop body. Then the control automatically goes to the next iteration (condition part). The next time (in the next iteration) all the statements of the loop body may get executed.
Example:
1 2 3 4 5 6 7 8 9 |
while(condition) { ----- ----- if(somesituation) continue; ----- ----- } |
Only Boolean Values Are Accepted
The condition in the header should always result in a Boolean value (true or false). No other value is allowed (similar to condition in if header).
Example:
1 2 3 4 |
while(3<7) is valid while(9) is not valid while(a>b) is valid while(4+8) is not valid while(true) is valid while(1) is not valid |
Nested Loops
We can have a looping construct inside another looping construct. This kind of arrangement is known as nested looping.
Eg:
1 2 3 4 5 6 7 |
while(condition1) { while(condition2) { } } |
There is no limit on depth of nesting. We can have any other loop inside a while loop. In fact any loop can have any other loop as part of its nesting. Not only a loop, any other construct (if, switch, etc) can be placed in a loop.
Initialization, Condition and Incrementation
Generally a loop execution depends on initial value of loop control variable, condition of loop and incrementation/decrementation of loop control variable.
The following loop executes for 10 times.
1 2 3 4 |
int a=1;while(a<=10) { a++; } |
Dummy Statement As Loop Body
If we put a semi-colon at the end of a while expression, that semi-colon becomes body of the loop. In that case anything after the semi-colon becomes out of the loop.
1 2 3 |
int a=10; while(a++ < 13) ; System.out.println(a); |
In the above example we have 3 statements. int a=10; is before the loop and System.out.println(a); is after the loop.
The loop statement ends in its line itself. The semi-colon in the line (2nd line) is body of the loop. So as long as the condition (a++ < 13) is true the dummy statement (semi-colon) is executed.
When condition results in false, the control comes out to print a value. So the printing statement is executed only once with that code. When it is printed, it prints 14.
Hope you like this tutorial, In case if you have any doubts do leave a comment here.