While loop:
A while loop checks the condition before executing the loop body.
When to use while loop:
When you donβt know in advance how many times the loop will run
When the condition depends on user input or runtime valuesFor loop
A for loop is used when you know how many times the loop should run.
When to use for loop:
When the number of iterations is fixed or known-
do-while loop
A do-while loop in Java is a loop that executes the code at least once, even if the condition is false.
the condition is checked after the loop body runs.
Example-while loop:
public class LoopingStatement {public static void main(String[] args) {
int i=1;
while(i<=5)
{
System.out.println(i);
i++;
}}
}
Example-for loop:
public class LoopingStatement {public static void main(String[] args) {
for(int i=1; i<=10; i++) { if(i%2 == 0) { System.out.println("even numbers :"+ i); } else { System.out.println("odd numbers :"+ i); } }}
}
Example-do-while:
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)