Opening Hours :7AM to 9PM
'while' is a keyword in java language which is used to execute one block of statements several number of times based on the condition.
while(condition) { Statement 1; Statement 2; .. .. }
In the above syntax if the condition is true the statements of while block is execute infinite times, if the condition is false while block terminated :
public class Main { public static void main(String args[]) { int x=1; while(x<=10) { System.out.println(x); x++; } } }
public class Main { public static void main(String args[]) { int x=10; while(x>=1) { System.out.println(x); x--; } } }
Nested while Loop :
One While loop within another while loop is knows as Nested while Loop
while(condition) { while(condition) { Statement 1; Statement 2; .. .. } Statement 3; Statement 4; .. .. }In the above syntax :
public class Main { public static void main(String args[]) { int x=1,y; while(x<=6) { y=1; while(y<=3) { System.out.print("*"); y++; } System.out.println(); x++; } } }
public class Main { public static void main(String args[]) { int x=1,y; while(x<=6) { y=1; while(y<=x) { System.out.print("*"); y++; } System.out.println(); x++; } } }
public class Main { public static void main(String args[]) { int x=1,y; while(x<=6) { y=6; while(y>=3) { System.out.print("*"); y--; } System.out.println(); x++; } } }