Do While Loops
In Java there is another type of loop called a Do While
loop, it is very similar to a while
loop but has a requirement that it is executed at least once. Let’s have a look at the syntax:
Do While Loop
Scanner scanner = new Scanner(System.in);do { System.out.print("Input: "); input = scanner.next().toLowerCase(); System.out.println(input)}while (!input.equals("quit"));
while
vs do while
Section titled “while vs do while”while
loops - check the condition first prior to executingdo while
loops - check the condition last and execute at least once (even if the condition is false).
Note: while
loops are the more common practice do while
loops have some use cases however the general preference is to implement while
loops where required.