> java的while
循环:详细说明
while
循环提供了一种方法,只要指定条件保持真实,可以重复执行代码块。 与for
的循环不同,这是当前已知的迭代次数时的理想之选,最适合不预定重复数量的情况。 初始化,增量或减少操作在循环的结构之外处理。while
>
,则循环主体执行;否则,循环将终止,并且该程序继续以循环后的语句进行。
while
true
>
>让我们用Java程序来说明前十个偶数数字:
输出:
<code class="language-java">public class EvenNumbers { public static void main(String[] args) { int i = 0; System.out.println("Printing the list of first 10 even numbers \n"); while (i < 20) { // Condition: i must be less than 20 (to generate 10 even numbers) System.out.println(i); i += 2; // Increment i by 2 in each iteration } } }</code>
到0。循环继续持续,只要小于20。在循环内,打印了
的当前值(一个偶数),然后打印出来,然后<code>Printing the list of first 10 even numbers 0 2 4 6 8 10 12 14 16 18</code>
达到20。i
时,循环终止
以上是循环的详细内容。更多信息请关注PHP中文网其他相关文章!