The do-while loop simply adds the commands or statements that you would like to execute in front of the condition in which the statements are executed. This is in contrary of the while loop which is structure in a way which we will have to key in the condition in which the loop is executed and then type in our statements for the loop. So what is the difference of having the statements of the loop before the condition? This, in fact, allows you to execute to the body of the loop at least once even though the condition may be false. As the program normally runs from top to bottom, the statements of the loop will be executed first regardless of the condition being true or false. After the loop ran once, then we will reach the condition of the loop, if the value if false, the loop will stop executing and if the condition is true, it will carry on executing the statements above.
Well, let's look at one simple example to give us a clearer idea of the do-while loop. The example we will print out 0 to 10 using the do-while loop.
Let's go through the code we have here. We have created a class "dowhileexample" within a Java project. Basically, since the aim of this simple code is to print out 0 to 10, we will first create an integer variable n which starts from 0. After that, we will type in the loop to print out 0 to 10 using the do-while structure.
The do-while loop will generally look like this:
do{
statements
}
while(condition);
In the example of our do-while loop, we type in "System.out.println(n);" to make sure zero is printed before we increase the value of n. Then, we include the statement "n++;" to increase the value of n by one each time the loop run.
After finishing up the statement for our loop, we will need to include our condition at the end of the loop to make sure it works properly. In this case, the condition of our loop should be "(n<11)" and this is located within the while statement as shown above. Some may ask why isn't it n<10, if that were to be the condition, when n is equal to 10, the statement would be false and we will not have 10 being printed out.
So now, we have our simple program pretty much done up and we are ready to test if it works well. So let's go ahead and click on the run button and see how it goes.
Mine shows this:
Here it is! If you can spot the difference, I have changed the condition within the while statement to "n<0" instead of "n<11" originally. So let's us now try running the program and see what result will we get.
When you try running the above program, you would get the above. However, if you were to write this simple program using a while loop, you will not get the 0 printed out. This is the primary difference between the do-while loop and the while loop. As I had mentioned earlier, the do-while loop would run the loop statements once before it were to stop or continue based on the condition. However, the while loop will execute the statement only if the condition were true. If the condition of the while loop is false, the loop statements will not be executed at all.
I guess, in generally, the do-while loop is used when you would like the statements of the loop to run at least once before deciding if the loop were to be carried on or stopped in your program. I have not met a case when I have to use this yet as I am just starting to program. However, I would show you a better example once I am able to find a case when the do-while loop is necessary for a program to be executed smoothly.
Until the next tutorial, I hope all of us have learned something be it good or bad. Taking small steps, moving forward towards our dream is not exactly a bad thing.
Enjoy your day.
No comments:
Post a Comment