Java While Loop
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
Do not forget to increase the variable used in the condition, otherwise the loop will never end!
Nested while Loop
class project1 {
public static void main(String a[])
{
int i =0;
while( i <=5)
{
System.out.println("hi");
int j=1;
while (j<3) {
System.out.println("good");
j++;
}
i++;
}
System.out.println(i);
}
}
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.
int i = 0;
do {
System.out.println(i);
i++;
while (i < 5);