• iconJava Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

DOWhile:
'dowhile' 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
{
    ...
    ...
}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



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


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
    {
        ...
        ...
        
    }while(condition);
...
..
}while(condition);

In the above syntax :
Outer loop is Row
Inner loop is Column

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


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:
***
***
***
***
***
***

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


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:
*
**
***
****
*****
******

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


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:
******
*****
****
***
**
*