Opening Hours :7AM to 9PM
'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.
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
class Main
{
public static void main(String args[])
{
int x=1;
do
{
System.out.println(x);
x++;
}while(x<=10);
}
}
public class Main { public static void main(String args[]) { int x=10; do { System.out.println(x); x--; }while(x>=1); } }
Nested Do While Loop :
One Do While loop within another Do While loop is knows as Nested Do While Loop
do { do { Statement 1; Statement 2; .. .. }while(condition); Statement 3; Statement 4; .. .. }while(condition);In the above syntax :
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); } }
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); } }
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); } }