Do While Loop Java

Do While Loop Java

Well, this is just another kind of loop, with the advantage that this is going to run at least once, even if the condition is false the first time. When we observed about while loop, or the for loop, they can be found in a situation when the loops don’t execute at all. Till now we have the idea of the other two loops, which is why learning about this loop should be very very simple for us.

Do While Loop Java

Again, if we read something like this – do something (this something is possibly a code), while this condition(possibly some condition, just like we have been using above) is true, it becomes pretty much easy to understand. Well, before we move to any implementation, let’s first understand the syntax of the do-while loop, and with an example, we will also try to visualize why the loop can execute at least once. Have a look at the syntax of the do-while loop –

//assume that the initialization was done in the code somewhere before, or just above the loop
do{
//some code here…
//iteration somewhere in the block here
}while (condition is true);

Well, that’s it. The above was the syntax of the do-while loop, which we need to follow. One thing to observe is that we are checking the condition after running the loop, and if the condition is true, we are again entering the loop. Now we got the answer to why this loop executes at least once, even if the condition is false the first time. The reason is that we are checking for the trueness of the condition after executing the code. If the condition becomes true, we are again going into the loop. This defines saying – do this while this condition is true.

Once the condition is false, we are out of the loop. This is quite simple, right? This means that we are first executing the code inside the loop, then checking whether or not is the condition true, and if the condition is true, we move to the loop again, and if the condition is false, we move out of the loop. Also, notice the semi-colon after a while. IT IS REQUIRED. Do not forget that.

So, even if the condition was false at the first execution only, the code had already been executed once and then checked for the trueness of the condition, and the condition being false, we move out of the do-while loop. This shows that the do-while loop is going to execute at least once, even if the condition is false the first time. Let’s have a sample implementation to understand the do-while loop –

public class Dowhileloop {
public static void main(String[] args) {
int i = 0;
do{
System.out.println(“This command just got executed!!”);
i++;
}while(i < 0);
}
}

So, if we try executing the above code, we find that the code inside the loop executed once, while the condition was false at the first time itself since the variable i is initialized to 0, and inside the loop, the value of variable i is becoming 1 through i++, so the condition (i<0) is never true, but still we could see the code inside the loop to execute once. If in case you need something like this, you have done while loop at your escape.

One such example if you wish to try is the pin verification system. We are put into the loop for some count of chances like we have 3 chances to put the pin correctly. If we put the correct pin, we are out of the loop, else, the program might be terminated there only, after the chances are over, saying that you have typed a wrong passkey or pin 3 times. This was just an example though but you can give it a try.

Or as another example, if you want to show some menu, like press 1 for this and press 2 for that and so on, then you might require to show that menu at least once, and then act in accordance with what the inputs are. So, this time as well, you might want to use the do… while loop.

Let’s have a sample program just like the other loops that we have seen so far, for demonstrating the do-while loop –

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

If we try running this program, we can find that this works just like the other loops. But the thing is that here, we check the condition after executing the code inside the loop, which results in at least one-time execution of the code if in case the condition is false at the first iteration itself.

Just like the other loops, the do-while loop can also encounter some conditions like a never-ending loop, but as we saw above, a never executing loop is not the case here, since it executes at least once!