Java For Loop, While Loop, Do-While Loop

In our previous post, we learned what are the conditional statements and how to use If, Else If and Else statements. In this tutorial, you will learn what are the looping statements in Java such as the For loop, While loop and Do-While loop, and how they alter the behavior of your program.

Looping statements are also common in programming. These are used if you are executing the same line of codes repeatedly. In order to minimize the lines of codes, we use looping statements.

The Java For Loop

The common construction of a java for loop is this:

for(variable initialization; condition; code execution){
     //enter your code here that will be called repeatedly.
}

A Java For loop contains three parts inside the parenthesis. These are the initialization statement, the condition statement, and a code block that will be called for each end of loop.

The Initialization statements block is where you can put the initialization of variables which then you can use inside the loop block.

The Condition statement block should return a boolean which determines if it will continue to loop inside the loop block or exit the loop. Returning TRUE means that it will execute the code inside the block again while returning FALSE will exit the loop.

The Code Execution statements block is mostly coded to increase or decrease the variable that was initialized in the Initialization block. It can also call a method. Basically, the code inside this statement block will be called at the end of each loop.

Here’s an example of a Java For Loop. We added the numbers colored in Red to show you the execution order.

Steps of a for loop

  1. First, it will initialize a variable. In the example above, we have initialized a variable i to 0. This initialization will only take place once and will only be called once.
  2. Next, the loop will test the condition inside our condition block. If it returns true, it will continue, if not, it will break and end the loop. In our case, since variable i which is also equal to 0 is less than 10, it will return true and will continue our loop.
  3. The loop will then enters the bracket and execute whatever codes that were written inside. In our example, it will execute our System.out and will print “The value of i is: 0” because currently, i holds a value of 0.
  4. Next, after executing the codes inside the bracket, it will go to the code execution block of our loop. Here in step 4, we set i to be incremented by one using i++. Therefore the value of i will be equal to 1 (0+1).
  5. After incrementing our variable, it will continue the loop and go again to step 2. It will skip step 1 since it can only be called once. The process will continue and go to step 3 and step 4 and will repeat until such time that step 2 returns to false. In our example, the loop will end if our variable i reaches 10 because 10 < 10 will return false.

The Java While Loop

The Java While Loop is almost the same in For loop but with just a condition statement inside the parenthesis. It doesn’t have an initialization or code execution block. A Java While Loop statement construction is this:

while(condition){
     //codes to be executed if condition
     //is true.
}

The loop will always execute the codes inside the bracket if the condition always returns to true. For example:

     int i=0;
     while (i<10) {
          System.out.println("The value of i is: " + i);
          i++;
     }
  1. First, the loop will always test the condition. If it returns true, it will execute the codes inside it. If it's not, the while loop will end. In our above example, before we create a while loop, we have created a variable i which is equal to 0. In the condition statement i<10, since i which is 0 is less than 10, it will return true and execute the codes inside.
  2. Whatever codes that are inside the loop will be executed if the condition is true. In our example, our System.out will be executed and will print "The value of i is: 0". Next line of code is i++, which will increment the value of i making it 1.
  3. After executing all lines of codes inside, it will then go again to the condition statement and test if it satisfies the condition. Since the current value of i is 1, and it is less than 10, it will continue the loop and execute again the codes inside the loop. The process continues until such time that the condition returns false. If it returns false, it will end the while loop.

The Java Do-While Loop

The Java Do-While loop is almost the same in While Loop. While loop executes the code inside the bracket if the condition statement returns to true, but in the Do-While loop, the code inside the do statement will always be called. Consider the example below:

int i = 0;
do {
     System.out.println("The value of i is :" + i);
     i++;
} while (i<10);
  1. The statements inside do will always be called first. In our example above, we have a variable i which is initialized to 0. The program will then enters the do statement and print "The value of i is 0". This is the opposite of while loop because while loop test the condition first before executing statements while in do-while loop, the statements will be executed first before testing the conditions. After printing the value of i, it will increase the value of i by one (i++), then, it will go to the while statement.
  2. The while statement will then test the condition. In our example, i which is already equal to 1 will be tested if it is less than 10, i<10, and will result to true, thus continuing the loop.
  3. The loop will continue until the condition returns false.

Remember that in Java While Loop, the condition will be tested first while in Java Do-While Loop, the statements or codes inside the bracket will be executed first before testing the condition.

Next in our tutorial is how to terminate a loop.

Share this tutorial!