for 循环是如何工作的,特别是 for(;;)? [英] How does a for loop work, specifically for(;;)?

查看:41
本文介绍了for 循环是如何工作的,特别是 for(;;)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看一些旧的公司代码,我发现了一个如下所示的 for 循环:

Looking through some old company code, I came across a for loop that looks like this:

for (;;) {
    //Some stuff
}

我试过谷歌但找不到任何答案.我是在编程课上睡着了还是这是一个不寻常的循环?

I tried Google but couldn't find any answers. Did I fall asleep in a programming class or is this an unusual loop?

推荐答案

java 中的一个 for 循环结构如下 -

A for loop in java has the following structure -

for (initialization statement; condition check; update)
    loop body;

如你所见,这里有四个语句 -

As you can see, there are four statements here -

  1. 初始化语句:该语句仅在第一次进入循环时执行一次.这是一个可选的声明,这意味着您可以选择将此字段保留为空白.它通常用于某些初始化目的.
  2. 条件检查:此语句可能是最重要的语句.它检查以验证某些表达式的计算结果是否为真.如果是,则循环执行继续.您可以选择将此字段保留为空,这将被评估为 true.
  3. 更新:此语句列表从左到右执行,通常用于递增/递减某个变量.
  4. loop body:循环体,会根据条件检查的真值反复执行.

基本上是这样执行的——首先,当第一次进入循环时,初始化语句被执行一次.然后执行条件检查以查看它的评估结果是否为真.如果是,则执行循环体,否则循环执行结束.之后,执行更新语句.接下来,再次执行条件检查,如果结果为真,则再次执行循环体,然后执行更新语句,然后再次执行条件检查......你明白了.

Basically this is how the execution follows - first, when the loop is entered for the first time, the initialization statement is executed once. Then the conditional check is executed to see if it evaluated to true. If it is, then the the loop body is executed, otherwise the loop execution is finished. After that, the Update statement(s) is(are) executed. Next, the conditional check is executed again, and if it evaluates to true, then again the loop body is executed, then update statement is executed, then again the conditional check....you get the picture.

现在关于您的 for( ; ; ) 语法.它没有初始化语句,因此不会执行任何操作.它的条件检查语句也是空的,这意味着它在循环体执行后评估为真.接下来,由于更新语句为空,因此什么都不执行.然后再次执行条件检查,再次评估为真,然后整个过程将再次重复.

Now about your for( ; ; ) syntax. It has no initialization statement, so nothing will be executed. Its conditional check statement is also empty, which means it evaluates to true after that the loop body is executed. Next, since the update statement is empty, nothing is executed. Then the conditional check is performed again which will again evaluates to true and then this whole process will again repeat.

所以你看,这基本上是一个没有初始化语句的无限循环,它的条件检查总是评估为真,并且没有更新语句.这相当于 -

So you see, this is basically an infinite loop which has no initialization statement, whose conditional check will always evaluates to true, and which has no update statement. This is equivalent to -

while(true)
{
    .....
}

这是 Java 中另一个流行的循环结构.

which is another popular loop construct in java.

当您使用这样的无限循环时,请务必注意中断条件,因为在大多数情况下,您不能让循环无限期地运行.要跳出这些类型的循环,您可以使用 break 语句.结构如下——

When you use an infinite loop like this, it's important pay attention to the breaking condition as in most cases you can't let a loop to run indefinitely. To break out of these kinds of loops, you can use the break statement. The structure is as follows -

if(some_condition_is_true)
    break;        // This will cause execution to break out of its nearest loop

if(some_condition_is_false)
    break;

这篇关于for 循环是如何工作的,特别是 for(;;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆