• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

do while loop :

Java Control Statements

'do while' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.

Syntax
do
{
    Statement 1;
    Statement 2;
    ..
    .. 

}while(condition);

In the above syntax if the condition is true the statements of do while block is execute infinite times, if the condition is false the statements of do while block is executed one time



MSK TechnologiesTrainings

Display 1 to 10 Values Using Do While Loop

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


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

Display 10 to 1 Values Using Do While Loop

public class Main
{
    public static void main(String args[])
    {
        int x=10;
        do
        {
            System.out.println(x);
            x--;
        }while(x>=1);
    }
}
                                

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

Nested For Do While In java


Nested Do While Loop :
One Do While loop within another Do While loop is knows as Nested Do While Loop

Syntax
do
{
    do
    {
        Statement 1;
        Statement 2;
        ..
        .. 
    }while(condition);
    
    Statement 3;
    Statement 4;
    ..
    .. 
}while(condition);
In the above syntax :
Outer loop is Row
Inner loop is Column

Key Points


Image
***
***
***
***
***
***

public class Main
{
    public static void main(String args[])
    {
        int x=1,y;
        do
        {
            y=1;
            do
            {
                System.out.print("*");
                y++;
            }while(y<=3);
            
            System.out.println();
            x++;
        }while(x<=6);
    
    }
}
                                

Output:
***
***
***
***
***
***

*
**
***
****
*****
******

                                   
public class Main
{
    public static void main(String args[])
    {
        int x=1,y;
        do
        {
            y=1;
            do
            {
                System.out.print("*");
                y++;
            }while(y<=x);
            
            System.out.println();
            x++;
        }while(x<=6);
    
    }
}
     
                                

Output:
*
**
***
****
*****
******

******
*****
****
***
**
*

public class Main
{
    public static void main(String args[])
    {
        int x=1,y;
        do
        {
            y=6;
            do
            {
                System.out.print("*");
                y--;
            }while(y>=x);
            
            System.out.println();
            x++;
        }while(x<=6);
    
    }
}
   
     
                                

Output:
******
*****
****
***
**
*