While Loops in Java – I




Hi! Welcome to the while loop tutorial.

Loop is one of the fundamental concept in programming. But, what is a loop?

In computer science, it is

“A single execution of a set of instructions that are to be repeated”.

What??

If you didn’t get it, just think that it is some technique used to avoid repetition of the same task. You will realise this soon with the examples.

How do we get the output “Hello World!“?

Now how can we print this “Hello World!” 10 times? Of course typing a copy of this same statement 10 times will do the work.

But this doesn’t look nice. Also, if we want to print this million times 🙁 Yeah it will be practically impossible to type the same thing again and again and again…even just copy and paste will not be a pleasant task at all.

Luckily, there is an easy way to do this with a single print statement. Use a while Loop!

A while loop has the following structure.

while (<condition>)

{

            <statements>

}

or if a single statement is within the brackets, there’s no any need of brackets.

while (<condition>)

            <statement>

Now what happens here is while the <condition> is true, the <statement(s)> that belong to the while statement are going to run continuously.

If the condition becomes false, further execution of the <statement(s)> will stop and the computer will jump to the next set of codes that lie below the while block.

Here’s the code for writing the “Hello World!“10 times, now this time, using a while loop.

First, note that I have initialized a variable called update. This variable is going to update its value by adding 1 in each cycle of the while loop.

OK. Here’s the step by step process.

Step 1 :

Initializing  and assigning the value 0 to a variable called update.

Step 2 :

while loop is executing. It first checks whether update is less than 10. The condition is true as update = 0 and 0 <10. So the statements

run one after the other. First “Hello World!” is printed and update is updated by adding 1.

Step 3:

Now update is not 0 anymore and it’s 1. What happens next is the computer again goes back to the beginning of while statement and checks the condition. Now (1 <10) is also true and the block will run again.

These steps are repeated until the update becomes 10. At that time, when the computer checks the condition, (10 < 10) is false (10 is equal to 10. No less, no more 🙂 )

So, the while loop ends and the program will jump to the next set of codes out of the while block.

So,

is executed at the end. That’s it. You may test the code and see.

 Now ready to have an infinite loop? Sounds interesting but NOT a good idea 🙁

You can easily generate an infinite loop from the previous example.

Just remove the statement

and it will run forever…..! Check it. In order to stop the loop, push the “terminate” button in the console.

Can you understand what is happening there?

The condition is always true since the update variable won’t actually update its value.

So value of update will always be 0. In every-time when the computer checks the condition, (0 < 10) is true!! That loop will run continuously.

Or else, you can easily write an infinite loop by setting the <condition> to the keyword true.

while (true){

                                  <Almost anything will run infinitely>

                         }

Infinite loops are not used in real programming.

But this while(true) format loop is used often with the special keyword break.

It is used to break and exit the loop so that the loop won’t be infinite. You will see these things later in detail…

But remember, avoid infinite loops 🙂

OK. That’s all for now. Next tutorial is also about another cool example of a while loop.

You may go and check it as well in order to be more familiarize with this loop. Bye! Thanks for reading!