C语言 for 循环括号内的两个分号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16113125/
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
Two semicolons inside a for-loop parentheses
提问by osse
I'm customising a code I found over the internet (it's the Adafruit Tweet Receipt). I cannot understand many parts of the code but the most perplexing to me is the for-loop with two semicolons inside the parentheses
我正在自定义我在互联网上找到的代码(它是Adafruit Tweet Receipt)。我无法理解代码的许多部分,但最令我困惑的是括号内有两个分号的 for 循环
boolean jsonParse(int depth, byte endChar) {
int c, i;
boolean readName = true;
for(;;) { //<---------
while(isspace(c = timedRead())); // Scan past whitespace
if(c < 0) return false; // Timeout
if(c == endChar) return true; // EOD
if(c == '{') { // Object follows
if(!jsonParse(depth + 1, '}')) return false;
if(!depth) return true; // End of file
if(depth == resultsDepth) { // End of object in results list
What does for(;;)mean? (It's an Arduino program so I guess it's in C).
什么为(;;)是什么意思?(这是一个 Arduino 程序,所以我猜它是用 C 编写的)。
回答by taocp
for(;;) {
}
functionally means
功能上的意思
while (true) {
}
It will probably break the loop/ return from loop based on some condition inside the loop body.
它可能会根据循环体内的某些条件中断循环/从循环返回。
The reason that for(;;)loops forever is because forhas three parts, each of which is optional. The first part initializes the loop; the second decides whether or not to continue the loop, and the third does something at the end of each iteration. It is full form, you would typically see something like this:
for(;;)永远循环的原因是因为它for包含三个部分,每个部分都是 optional。第一部分初始化循环;第二个决定是否继续循环,第三个在每次迭代结束时做一些事情。它是完整形式,您通常会看到如下内容:
for(i = 0; i < 10; i++)
If the first (initialization) or last (end-of-iteration) parts are missing, nothing is done in their place. If the middle (test) part is missing, then it acts as though truewere there in its place. So for(;;)is the same as for(;true;)', which (as shown above) is the same as while (true).
如果缺少第一个(初始化)或最后一个(迭代结束)部分,则不会在它们的位置执行任何操作。如果缺少中间(测试)部分,那么它就好像true在它的位置上一样。Sofor(;;)与for(;true;)'相同,后者(如上所示)与while (true).
回答by Fls'Zen
The forloop has 3 components, separated by semi-colons. The first component runs before the looping starts and is commonly used to initialize a variable. The second is a condition. The condition is checked at the beginning of each iteration, and if it evaluates to true, then the code in the loop runs. The third components is executed at the end of the loop, before another iteration (starting with condition check) begins, and is often used to increment a variable.
所述for环具有3个分量,由分号分隔。第一个组件在循环开始之前运行,通常用于初始化变量。二是有条件。在每次迭代开始时检查条件,如果计算结果为真,则循环中的代码运行。第三个组件在循环结束时执行,在另一个迭代(从条件检查开始)开始之前,通常用于增加变量。
In your case for(;;)means that it will loop forever since the condition is not present. The loop ends when the code returns or breaks.
在您的情况下for(;;)意味着它将永远循环,因为条件不存在。当代码returns 或breaks时循环结束。
回答by Travis J
Each clause of a forloop is optional. So when they are excluded, it still loops. forloops compile into whileloops.
for循环的每个子句都是可选的。所以当它们被排除时,它仍然循环。for循环编译成while循环。
The end result becomes a check to initialize any variables, which concludes after nothing happening since it is empty, a check to the boolean condition in the second clause, which is not present so the loop starts, and once the loop hits the end bracket, a check to see if there is any code to run before checking the boolean condition again.
最终结果变成了初始化任何变量的检查,它在没有发生任何事情后结束,因为它是空的,检查第二个子句中的布尔条件,它不存在所以循环开始,一旦循环碰到结束括号,在再次检查布尔条件之前检查是否有任何代码要运行。
In code it looks like:
在代码中它看起来像:
while(true){
}
回答by Suraj Jain
Here's What Wikipedia SaysAbout it
下面是维基百科说关于它
Use as infinite loops
This C-style for-loop is commonly the source of an infinite loop since the fundamental steps of iteration are completely in the control of the programmer. In fact, when infinite loops are intended, this type of for-loop can be used (with empty expressions), such as:
for (;;)//loop bodyThis style is used instead of infinite
while(1)loops to avoid a type conversion warning in some C/C++ compilers.Some programmers prefer the more succinctfor(;;)form over the semantically equivalent but more verbosewhile (true)form.
用作无限循环
这种 C 风格的 for 循环通常是无限循环的来源,因为迭代的基本步骤完全由程序员控制。事实上,当打算使用无限循环时,可以使用这种类型的 for 循环(带有空表达式),例如:
for (;;)//loop body
while(1)在某些 C/C++ 编译器中,使用这种风格代替无限循环以避免类型转换警告。一些程序员更喜欢更简洁的for(;;)形式,而不是语义等效但更冗长的while (true)形式。

