while循环是一个控制流语句,它允许代码根据给定的布尔条件重复执行。while循环可以被认为是重复的if语句。
句法 :
while(布尔条件)
{
循环语句...
}
流程图:

// Java program to illustrate while loop
class whileLoopDemo
{
public static void main(String args[])
{
int x = 1;
// Exit when x becomes greater than 4
while (x <= 4)
{
System.out.println("Value of x:" + x);
// Increment the value of x for
// next iteration
x++;
}
}
}
输出:
Value of x:1 Value of x:2 Value of x:3 Value of x:4