Java do-while loop with examples | Loops in Java | Control Statements

As we know that a Programming Language uses control statements to cause the flow of execution to advance and branch based on the changes to the state of the program. The Control statements like the for loop, while loop and do while loop have been very helpful in executing a block of statements a given number of times.

In the previous tutorial, we discussed about while loops in Java, and saw that if the conditional expression controlling the while loop is false. Then the loop body will not be executed even once. But in some cases, we might require to run the loop body once, even if the conditional expression is false in the first place. Then how will we achieve this. 

Fortunately like other programming languages, Java also provides us with a 3rd type of loop, known as the do-while loop. This do-while loop always executes the loop body at least once in the beginning even if the conditional expression is false. This is due to the fact that the condition checking is done at the end of the loop structure. Also note that the conditional expression must be a boolean expression, as followed in every loop type.

syntax : 

initialization
do{
// code block to be executed
increment/decrement
}while(condition);

Example :

int i=1;
do{
//statements
i++;
}while(i>2);

* Here you can notice that the whole loop structure is somewhat similar to that of while loop, only with a slight difference that the condition is checked at the last of the loop, whereas in while loop condition was checked at the starting. Due to this feature of do-while loop, it is also called as EXIT CONTROL LOOP.
Also notice the semicolon(;) at the end of the while(i>2), this is compulsory in case of do-while loop.

How do-while loop works !

In the syntax of do-while loop, mentioned above. The condition checking is done at the last of the loop, that means the first time the code will always be executed, whether the condition is false or true. The initialization is done outside the loop body and the increment/decrement operation is performed inside the loop body, in order to maintain the flow.

Example :

public class doWhileLoop1 {

public static void main(String[] args) {
int n=10;
do {
    System.out.println("Programmer Studios");
         n--;
}while(n>0);
}

}

OUTPUT
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios

Here the int variable n is initialized with 10. On entering the do-while loop body. It encounters the print statement, and prints "Programmer Studios", then comes the decrement part and here n becomes 9. and after that the condition is checked, whether n>0 or not. If the condition evaluates to be true, then the control redirects to the first statement of loop block again and all statements are executed again. This process continues until the conditional expression becomes false. When it becomes false, then the statement just after the loop body is executed.

do-while loop as an infinite loop

If we pass a boolean expression like true in the condition part, then the loop will become an infinite loop, which would keep generating the output until we manually terminate the program.

Example:

public class doWhileLoopAsAnInfiniteLoopExample1 {

public static void main(String[] args) {
int n=10;
do {
    System.out.println("Programmer Studios");
            n--;
}while(n<100);
}

}

OUTPUT
Programmer Studios
Programmer Studios
Programmer Studios
Programmer Studios
...
...
...

In this example, since the condition is going to be true always, then the decrement operation will have no effect on the program. It will keep decrementing n, but will not terminate the program.

public class doWhileLoopAsInfiniteLoopExample2 {  
public static void main(String[] args) {  
    while(true){  
        System.out.println("Programmer Studios");  
    }  
}  
}  

This would also cause an infinite loop, because the condition we gave in the while brackets says to continue executing the statements until the condition is true, and here the condition is never gonna change on its own. Thus executing the statements infinite number of times.

Simple problems using do-while loop

Program 1:  WAP to find the sum of n numbers.

public class sumOfNNumbers {

    public static void main(String[] args) {
        int n=5;
        int sum = 0;
        int i = 1; 
        do {
            sum += i;
            i++;
            n--;
        }while(n>0);
        System.out.println(sum);
    }

}

OUTPUT
15

Program 2:  WAP to print the table of a number N upto 10.

public class tableOfN {

    public static void main(String[] args) {
        int n=5;        
         int i=1;
            do
            {
                System.out.println(n +" * " + i +" = " + n*i);
                i++;
            }while(i<=10);
    }

}

OUTPUT
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post