Do while loop is called an exit controlled loop and While loop is called an entry controlled loop explain.
In : BSc IT Subject : Structure Programming Methodology - JavaThe do-while loop is called an exit-controlled loop, and the while loop is called an entry-controlled loop — this is because of when they check the condition.
###while loop (Entry-controlled):
- Checks the condition before entering the loop.
- If the condition is false, the loop body will not run at all.
Example:
int i = 1;
while (i <= 3) {
System.out.println(i);
i++;
}
###do-while loop (Exit-controlled):
- Executes the loop body first, then checks the condition at the end.
- So, the loop body runs at least once, even if the condition is false.
Example:
int j = 5;
do {
System.out.println(j);
j++;
} while (j <= 3);
In Simple Words:
- Use `while` when you want to check before looping.
- Use `do-while` when you want the loop to run at least once, no matter what.