What is While Loop?
while loop repeats code while condition is true.
Syntax
while(condition){
code;
}
Example
int i = 1;
while(i <= 5){
System.out.println(i);
i++;
}
do while Loop
int i = 1;
do{
System.out.println(i);
i++;
}while(i <= 5);
Difference
- while checks condition first
- do while runs at least once