Java While Loop
A block of code can be run by a loop as long as a predetermined condition is met.
Loops are useful because they eliminate errors, save time, and improve readability of programming.
A block of code is iterated through by the while loop as long as a particular condition is met:
while (condition) {
// code block to be executed
}
In the example below, if a variable (i) is less than 5, the loop’s code will continue to run repeatedly:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
Note: If you don’t increment the variable in the condition, the loop will never stop!
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.