Advertisement

Google Ad Slot: content-top

Java While Loop


while Loop:

The while loop executes as long as the specified condition evaluates to true. The condition is evaluated before the code block executes.


Syntax:

while (condition) { 
  // Code to execute 
}
Basic While Loop
int i=1;
while(i<=4){
System.out.print("Hi "+ i +" ");
i++;
}
System.out.println();
System.out.print("Bye"+i);
Try it yourself

Nested While Loop
int i = 1;
while(i<=4){
System.out.println("Hi"+ i);
int j=1;
while(j<=3) {
System.out.print("Hello "+j+" ");
j++;
}
i++;
}
System.out.println();
System.out.print("Bye"+i);
Try it yourself