java while 循环详解
while 循环用于重复执行一段代码,直到指定条件变为假。 若事先不知道循环次数,while 循环是理想的选择。和 for 循环不同,while 循环结构中不进行循环的初始化和迭代条件的更新。
while 循环也被称为前测试循环(入口控制循环)。如果条件是真实的,则执行循环;否则,跳过循环,执行后续代码。
示例:打印前十个偶数
public class Calculation { public static void main(String[] args) { int i = 0; System.out.println("Printing the list of first 10 even numbers "); while (i < 10) { System.out.println(i * 2); i++; } } }
输出:
Printing the list of first 10 even numbers 0 2 4 6 8 10 12 14 16 18
以上是循环的详细内容。请关注图灵教育的其他相关文章!
