[英]For loop confusion in c program
Why the loop is running from 2 to 7? 为什么循环从2运行到7?
int i;
for(i=1;i<=6;printf("\n%d\n",i))
i++;
The output of this is 这个的输出是
2 3 4 5 6 7
but limit of i
is 6. 但是
i
限制是6。
The Syntax of a for
loop is for
循环的语法是
for (
clause-1;
expression-2;
expression-3)
statementfor (
clause-1;
expression-2;
expression-3)
语句
The execution is as below, quoting from C11
, chapter §6.8.5.3, ( emphasis mine ) 执行如下,引用
C11
,章节§6.8.5.3,( 强调我的 )
The expression
expression-2
is the controlling expression that is evaluated before each execution of the loop body .表达式
expression-2
是在每次执行循环体之前计算的控制表达式。 The expressionexpression-3
is evaluated as a void expression after each execution of the loop body.在每次执行循环体之后,
expression-3
被评估为void表达式。 [....][....]
Here, i++
is the body and printf("\\n%d\\n",i)
is the expression-3 . 这里,
i++
是body, printf("\\n%d\\n",i)
是表达式-3 。
So, the order of execution would be something like 所以,执行的顺序就像是
i = 1;
start loop
i < = 6 //==> TRUE
i++; //i == 2
printf // Will print 2 ///iteration 1 done
i < = 6 //==> TRUE
i++; //i == 3
printf // Will print 3 ///iteration 2 done
.
.
.
i < = 6 //==> TRUE
i++; //i == 6
printf // Will print 6 ///iteration 5 done
i < = 6 //==> TRUE
i++; //i == 7
printf // Will print 7 ///iteration 6 done
i < = 6 ==> FALSE
end loop.
A for
loop like 像
for
循环一样
for(i=1;i<=6;printf("\n%d\n",i))
i++;
is equivalent to 相当于
{
i = 1; // Initialization clause from for loop
while (i <= 6) // Condition clause from for loop
{
i++; // Body of for loop
printf("\n%d\n", i); // "Increment" clause from for loop
}
}
As you can see, the printf
is done after the variable i
is incremented, which of course means it will print the incremented value ( 2
to 7
). 如您所见,
printf
在变量i
递增后完成,这当然意味着它将打印递增的值( 2
到7
)。
The workings of the loop are equivalent to the now-obvious 循环的工作方式相当于现在显而易见的
int i;
for (i = 1; i <= 6; /*intentionally blank*/){
i++;
printf("\n%d\n", i);
}
as, conceptually, the 3rd expression in the for
loop is ran just before the closing brace of the loop body. 从概念上讲,
for
循环中的第3个表达式就在循环体的右大括号之前运行。
You have written the for loop in an unusual manner. 你以不寻常的方式编写了for循环。
The operation of a for loop is given below. 下面给出for循环的操作。
The initialization is done first. 首先完成初始化。
i=1
Then the expression is checked i<=6
然后检查表达式
i<=6
Then the body is carried out i++
然后身体进行
i++
Then the increment is carried out. 然后执行增量。 In your case this is
printf("\\n%d\\n",i)
在你的情况下这是
printf("\\n%d\\n",i)
Repeat Step 2 to 4, until step 2 is FALSE. 重复步骤2到4,直到步骤2为假。
In your case, you can see that the printf
will be done for i==7
first, and then the expression will be checked for i==7
. 在您的情况下,您可以看到
printf
将首先针对i==7
完成,然后将检查表达式i==7
。 After that the for loop will exit. 之后,for循环将退出。 Similarly the first print will be done only after one increment on
i
类似地,第一次打印仅在
i
增加一次后完成
So, first print will be for 2
and last will be for 7
因此,首先打印为
2
,最后打印为7
You've written the loop incorrectly - you've swapped the body of the loop with the incrementation code. 你已经错误地编写了循环 - 你已经用增量代码交换了循环体。 So after having done
i++
which is in the body of the loop, it does the printf
as the incrementation when that should be the other way around. 因此,在完成循环体中的
i++
之后,它会将printf
作为增量进行处理,而这应该是另一种方式。
Write the for
loop correctly as follows. 正确写入
for
循环如下。
int i;
for(i=1;i<=6;i++)
printf("\n%d\n",i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.