C语言 没有花括号的 for 循环有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26289546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:27:14  来源:igfitidea点击:

What does a for loop without curly braces do?

cfor-loop

提问by Tenza

Hi so basically my question is what does a for loop without any curly braces around it do? So from what I know, that during an if-statement only the first line of the code is executed. So in a for loop how does it work? I don't really understand the concept of a loop without the braces and with the braces. I guess an explanation with a piece of code would help. This is in C by the way. Here's a code I've been looking at as a reference.

嗨,所以基本上我的问题是没有任何花括号的 for 循环有什么作用?因此,据我所知,在 if 语句期间,仅执行代码的第一行。那么在 for 循环中它是如何工作的呢?我真的不明白没有大括号和大括号的循环的概念。我想用一段代码来解释会有所帮助。顺便说一下,这是在 C 中。这是我一直在查看的代码作为参考。

int main(int argc, char* argv[])
{
  int i;
  int count = 0;
  for (i = 0; i < 5; i++)
    count++;
    printf("The value of count is: %d\n", count);

  return 0;
}

In this case, I see that there is no curly braces, so I am assuming that it will just keep iterating the first statement until i < 5 and once i is not less than 5 it doesn't do anything, but when I tested the code I get that it also ends up printing the printf statement. I thought that a loop without curly braces executed only the first line of code? Or am I missing something here.

在这种情况下,我看到没有大括号,所以我假设它会一直迭代第一个语句直到 i < 5 并且一旦 i 不小于 5 它什么都不做,但是当我测试代码我知道它最终也会打印 printf 语句。我以为没有花括号的循环只执行了第一行代码?或者我在这里遗漏了什么。

回答by Air

Without curly braces, only the first statement following the loop definition is considered to belong to the loop body.

没有花括号,只有循环定义之后的第一条语句被认为属于循环体。

Notice that in your example, printfis only called once. Though its indentation matches the previous line, that's a red herring – C doesn't care. What it says to me is that whoever wrote the code probably forgot the braces, and intendedthe printfstatement to be part of the loop body.

请注意,在您的示例中,printf仅调用一次。尽管它的缩进与前一行匹配,但这是一个红鲱鱼——C 不在乎。它对我说的是,编写代码的人可能忘记了大括号,并打算printf语句作为循环体的一部分。

The only time I would leave out the curly braces is when writing a one-line ifstatement:

我唯一一次省略大括号是在编写一行if语句时:

if (condition) statement;
do_something_else();

Here, there's no indentation to introduce ambiguity about whether the statement on the second line is actually supposed to belong to the body of the if. You would likely be more confident when reading this that it's working as intended.

在这里,没有缩进来引入关于第二行上的语句是否实际上应该属于if. 阅读本文时,您可能会更加确信它按预期工作。

回答by gmarintes

If the for-loop does not have braces, it will execute the next statement. The syntax is essentially

如果 for 循环没有大括号,它将执行下一条语句。语法本质上是

for (<initialization>;<condition>;<increment>) <statement>;

The "statement" part can be anything. It could be a simple count++; or it could be an 'if'/'if-else' statement, it could even be another for-loop without the braces!

“声明”部分可以是任何东西。它可以是一个简单的 count++;或者它可能是一个 'if'/'if-else' 语句,它甚至可能是另一个没有大括号的 for 循环!

So in this case, the code is similar to:

所以在这种情况下,代码类似于:

for (i = 0; i < 5; i++) {
    count++;
}
printf("The value of count is: %d\n", count);