While Loop in Java

While Loop in Java

Well, the while loop is another type of loop, and we can easily understand the related concept. In fact, we can understand it even better, if we say something like – while this condition is true, do something(this something is possibly a block of code). Well, as soon as the condition is false, we are out of the loop. It is pretty much simple and straightforward. Here also, we have to perform the basic things like initialization, checking the condition, and also performing some iteration operations, which leads to termination of the loop.

While Loop in Java

So, firstly we initialize, then we check the condition, if the condition is true, we move inside the loop. Once we are inside the loop, we execute all the repetitive code which is written, and along with that code, we also have to do the iteration here only, inside the loop, which can help us lead to the termination of the loop, otherwise, we will encounter a condition called as an infinite loop, which is sometimes dangerous, and sometimes necessary as well.

Well, let’s first have a look at the syntax of the while loop –
// initialization is done before somewhere in the program, or even just above the while loop.
while(condition is true){
//run the code here…
// also perform the operation, which leads to the termination condition.
}

That’s it. Every time we try to get inside the while loop, the condition is checked. If the condition is true, then only we can enter the loop. If the condition becomes false, we are out of the while loop. Again, here also, the basic things like initialization, condition checking (every time we want to enter the loop), and the iteration are to be done. In the while loop, we check the condition before entering the loop. If the condition is true, then only we can enter the while loop.

This is important to mention when we will move to the next loop. But before that, let’s have an example program for demonstrating the while loop.

public class Whileloop {
public static void main(String[] args) {
// lets initialize here –
int i = 0;
while(i<10){
// here is some code which we want to execute repeatedly
System.out.println(“The value of i is now: ” + i);
// here is the operation which leads us to termination of the while loop
i++;
}
System.out.println(“This is just to inform you that you are out of the while loop”);
}
}

Well, before this, you may have read something called an infinite loop. We now know that the Infinite loop is a condition, in which the loop is forever executing because the condition never gets false, and we know that the while loop executes till the condition is true, and the condition is forever true. This can be a nightmare sometimes, and sometimes necessary too.

If you wish to try the infinite loop, try removing or commenting on the iteration operation, which leads us to the termination of the loop. This is pretty straightforward if you remove the iteration operation, then the variable value will never reach the point where the condition becomes false and hence, the condition is always going to be true, resulting in an infinite loop.

Alternatively, you can make the initialization and the condition such that it never becomes false, like in the below example. Let’s have an example implementation of the while loop in a situation of a never-ending loop –

public class Whileloop {
public static void main(String[] args) {
int i = 100;
while(i>10){
System.out.println(“The value of i is now: ” + i);
i++;
}
}
}

So, if we try executing this example, we end up having an endless loop. We can again do something more interesting in this case if we want to have an infinite loop here. The idea is to use the true keyword directly into the while loop condition. All we are doing is checking the trueness of the condition, right? Now, instead of checking any condition or any such initialization, we can directly type in true instead of some condition. So, true is always going to be true, putting our code in an endless loop again. Here is an example program for demonstrating this thing –

public class Whileloop {
public static void main(String[] args) {
while(true){
System.out.println(“We are in an infinite loop “);
}
}
}

So, running this program also ends our code into a never-ending loop.

In the while loop as well, you might find the situation when the loop does not execute at all. We now know that such a situation occurs when the condition is never true. So, let’s quickly have an example for such a situation, in which the while loop never executes. Let’s consider this example implementation for understanding this condition in case of a while loop –

public class Whileloop {
public static void main(String[] args) {
int i = 10;
while(i<0){
System.out.println(“Are we even going to enter in this loop ever? “);
i++;
}
}
}

As you can see, the while loop didn’t even execute in the first place. So, we should be careful about the initializations, condition checking, and iterations that we are doing, so that we do not run into conditions like an infinite loop or the loop that never executes.