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

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

问题描述

  for(;;) {b $ b // Some stuff 
}

找到任何答案。我在一个编程课上睡着了,或者这是一个不寻常的循环?

解决方案 循环在java中有以下结构:
$ b $ pre $ for(初始化语句;条件检查;更新)
循环体;

正如您所看到的,这里有四条语句 -


  1. 初始化语句:当第一次输入循环时,该语句只执行一次。这是一个可选的语句,这意味着你可以选择保持这个字段为空。它通常用于一些初始化目的。

  2. 有条件的检查:这个语句可能是最重要的。它检查以确定某些表达式是否为真。如果是,则循环执行继续。您可以选择将此字段保留为空,并将其评估为 true

  3. 更新:此语句列表从左到右正确的,通常用于递增/递减某些变量。
  4. 循环体:循环的主体,根据条件检查的真值一次又一次地执行。

基本上这就是执行的方式 - 首先,当循环第一次进入时,初始化语句被执行一次。然后执行条件检查以查看它是否评估为真。如果是,则执行循环体,否则循环执行结束。之后,Update语句被执行。接下来,再次执行条件检查,如果它的计算结果为true,那么循环体再次执行,然后更新语句被执行,然后再次条件检查....你得到的图片。

现在关于你的 for(;;)语法。它没有初始化语句,所以什么都不会执行。这是有条件的检查语句也是空的,所以这意味着它的计算结果为真。之后,循环体被执行。接下来,由于update语句是空的,所以不执行任何操作。然后再次执行条件检查,这将再次评估为真,然后这整个过程将再次重复。

所以,你看,这基本上是一个无限循环,没有初始化语句,其条件检查将始终评估为true,并且没有更新语句。这相当于 - $ / $>

  while(true)
{
.....

这是java中另一个流行的循环结构。

当你像这样使用一个无限循环时,重要的是要注意破坏条件,因为在大多数情况下你不能让循环无限期地运行。要打破这种循环,可以使用 break 语句。结构如下 - $ / b>

  if(some_condition_is_true)
break; //这会导致执行结果离开最近的循环

或者

  if(some_condition_is_false)
break;


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?

解决方案

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. Initialization statement: This statement is executed only once, when the loop is entered for the first time. This is an optional statement, meaning you can choose keep this field blank. It is generally used for some initialization purpose.
  2. Conditional check: This statement is probably the most important one. It checks to verify whether or not certain expression evaluates to true. If it is, then the loop execution continues. You can choose to keep this field empty, which will be evaluated to true.
  3. Update: This statement list is executed from left to right, typically used to increment/decrement some variable.
  4. loop body: The body of the loop, which will be executed again and again based on the conditional check's truth value.

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.

Now about your for( ; ; ) syntax. It has no initialization statement, so nothing will be executed. It's conditional check statement is also empty, so 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)
{
    .....
}

which is another popular loop construct in java.

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

or

if(some_condition_is_false)
    break;

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

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