Java While Loop
A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution of the statement.
Example:
public class Myclass { public static void main(String[] args) { int num = 9; boolean divisibleBy7 = false; while (!divisibleBy7) { System.out.println(num); if (num % 7 == 0) divisibleBy7 = true; --num; } } }
Output
9 8 7
Since the while loop evaluates its conditional expression at the top of the loop, the body of the loop will not execute even once if the condition is false to begin with.
