Latest :

Java While Loop – Tutorial & Examples

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

Output:

Definition Of While Loop

This loop is used to execute a set of statements repeatedly as long as a condition is true.

Syntax:

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:

Execution Order:

Another Example Program For While Loop In Java:

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:

Output:

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:

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:

Output:

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:

Example Program For Infinite Loop:

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:

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:

 

Nested Loops

We can have a looping construct inside another looping construct. This kind of arrangement is known as nested looping.

Eg:

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.

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.

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.

x

Check Also

Convert String To Date In Java – JavaTutoring

Convert String to Date In Java – Here we cover the different ways to convert ...