• For Loop, While Loop, Do-While Loop

    In our previous post, you 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 alters 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 For Loop

    The common construction of a for loop is this:

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

    The for loop will need an initialized variable, then it will test a variable if it satisfies the condition, and enters the codes that will be executed. Lastly, after executing the line of codes, it will determine if the variable should be increment or decrement. Consider the example below.

    for loop sequence

    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. 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 was written in it. In our example, it will then execute our System.out. and will print “The value of is is: 0” because i holds a value of 0.
    4. Next, after executing the codes inside the bracket, it will then go to the increment or decrement definition of our loop. In our case, we have set i to be incremented by one (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 because step 1 will only be called once like i said earlier. 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 is not less than 10 because they are equal.

    The While Loop

    The while loop is almost the same on for loop but with just condition statement inside. It doesn’t have initialization or increment-decrement definition. A 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 return 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 create 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 is 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 (0+1).
    3. After executing all lines of codes inside, it will then go again to the condition statement and test if it satisfies the condtion. Since the value of is is 1, and it is less than 10, it will continue the loop and execute again the codes inside the loop. The process continue until such time that the condition returns false. If it returns false, it will end the while loop.

    The Do-While Loop

    The 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 continue the loop.
    3. The loop will continue until the condition returns false.

    Remember that in while loop, the condition will be tested first while in do-while loop, the statements or codes inside the bracket will be execute first before testing the condition.

    Next in our tutorial is how to terminate a loop.

    Related Post