do while loop in Java

You must have read the while loop in a recent post. Now we will discuss the do while loop in Java, do while java example, and also see the syntax of do while loop in java. Most of us already know about the do while loop, here we will see the execution of example of do while loop in java.
It is like a while loop, It also allows code to be executed repeatedly until a condition is satisfied. The only difference that it checks for the condition after the execution of the body of the loop. It is also known as Exit Control Loop. Here we will discuss how to use do while loop in Java.

do
{
   // Body of loop
} While(condition);

Working of do while loop in Java?

First, the statements inside the loop execute and then the condition gets evaluated.
If the condition returns true then the control gets transferred to the “do” and it again executes the statement that presented in the loop body.
If the condition returns false then control flow directly it jumps to out of the loop and it executes the next statement after the loop.

NOTE: The do-while loop executes the code at least one time because it checks the condition after execution of the body.

Flowchart of do while loop in Java
class ExampleDoWhileLoop 
{
    public static void main(String args[])
    {
         int i=1;
         do	
         {
              System.out.println(i);
              i++;
         }while(i <=10);
    }
}

Output:
1
2
3
4
5
6
7
8
9
10

Infinitive do while: If the condition of loop will never false. Then it’s infinite do while loop.

public class ExampleOfInfiniteDOWhileLoop
 {  
	public static void main(String[] args)
	{  
            int i = 0;
	    do  
	    {
	    	 System.out.println(i);
	    	 i++;
	    } while(i  > 0);
	 }  
}  

It is an infinite loop. Because this is because the condition is i > 0 which would always be true as we are incrementing the value of i inside body of the loop.

Another example:

public class ExampleOfInfiniteDoWhileLoop
 {  
	public static void main(String[] args)
	{    
             int i = 0;
	     do
	     {
	         System.out.println(i);
                 i++;
	     } while(true);
	}  
}  

1. Quiz, Read the below code and do answer.

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

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

public class ExampleDoWhileLoop 
{
    public static void main(String args[])
    {
         int i = 1;
         do    
         {
        	 System.out.println(i);
        	 i++;
         }
         while(i < 1);
    }
}

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

public class ExampleDoWhileLoop 
{
    public static void main(String args[])
    {
         int i = 1;
         do    
         {
        	 System.out.println(i);
        	 i = i+2;
         }
         while(i < 1);
    }
}

Click on anyone to know the answer.

Leave a Comment