[英]What does “for (; --i >= 0; )” mean in C?
What does for (; --i >= 0; )
mean in C? for (; --i >= 0; )
在 C 中是什么意思?
How is the counter getting decremented and how is it different from for ( ; i >= 0; --i)
?计数器如何递减,它与
for ( ; i >= 0; --i)
有何不同?
They are very similar, but not the same!它们非常相似,但又不一样! First you have to understand how a
for
loop in C gets executed:首先,您必须了解 C 中的
for
循环是如何执行的:
As an example:举个例子:
1 2, 5 4
| | |
v v v
for(i = 0; i < 5; i++) {
// Do something <-- 3
}
As you can see 2, 3, 4, 5 is a loop until the condition is false.如您所见,2、3、4、5 是一个循环,直到条件为假为止。
Now you should clearly see how the for loop gets executed.现在您应该清楚地看到 for 循环是如何执行的。 The difference now is that in your first example:
现在的区别在于,在您的第一个示例中:
int i = 5;
for ( ; --i >= 0; )
printf("%d\n", i);
The output would be:输出将是:
4
3
2
1
0
Because after the first check of the condition (Point 2), it executes the code block of the for
statement and i
already gets decremented.因为在第一次检查条件后(第 2 点),它执行了
for
语句的代码块并且i
已经递减。
In your second example:在你的第二个例子中:
int i = 5;
for( ; i>=0; --i)
printf("%d\n", i);
The output would be:输出将是:
5 // See here the difference
4
3
2
1
0
Here you get the difference, because it gets decremented in Point 4, so the first time it runs with the value 5.在这里你得到了差异,因为它在第 4 点递减,所以它第一次运行时值为 5。
These constructs are formally equivalent to这些结构形式上等价于
while (--i >= 0)
{
Body;
}
and和
while (i >= 0)
{
Body;
--i;
}
Do you better see the difference?你能更好地看出区别吗?
In general, we can convert any for
loop or while
loop into a set of mostly linear statements with a goto
.通常,我们可以使用
goto
将任何for
循环或while
循环转换为一组主要为线性的语句。 Doing this may be helpful to compare the code.这样做可能有助于比较代码。
### for (; --i >= 0; ) { statements; }
; // Initializer statement
start:
bool condition = (--i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
; // Empty increment statement
goto start // Go to the top of the loop.
}
### for( ; i>=0; --i) { statements; }
; // Initializer statement
start:
bool condition = (i >= 0); // Conditional
if (condition) { // If true, we execute the body as so:
statements; // The statements inside the loop
--i; // Increment statement
goto start; // Go to the top of the loop.
}
In the first case, we decrement i
for the first time before each loop body.在第一种情况下,我们在每个循环体之前第一次递减
i
。 In the second case, we decrement i
after each loop body.在第二种情况下,我们在每个循环体之后递减
i
。
The easiest way to see that is to consider what happens when you enter this loop when i == 0
.最简单的方法是考虑当
i == 0
进入这个循环时会发生什么。 In the first case, after this block of code, you would have a resulting value of i == -1
.在第一种情况下,在此代码块之后,您将得到
i == -1
的结果值。 In the second case, i
wouldn't have changed (that is, i == 0
), because we never reached the increment statement.在第二种情况下,
i
不会改变(即i == 0
),因为我们从未到达 increment 语句。
for (; --i >= 0; )
works like this: for (; --i >= 0; )
工作方式如下:
Say if you have i as 10, it will decrement the i value and will compare 9 >= 0 and so on.假设您将 i 设为 10,它将递减 i 值并比较 9 >= 0 等等。
So the output would be like 9, 8... till 0
.所以输出会像
9, 8... till 0
。
While the loop for( ; i>=0;--i)
would first go to 10, then decrement the value and then it would check i>=0.虽然
for( ; i>=0;--i)
循环首先会到达 10,然后递减该值,然后它会检查 i>=0。 So the output would be 10, 9, 8... till 0
.所以输出将是
10, 9, 8... till 0
。
The first one decrements i
and then checks the condition.第一个递减
i
然后检查条件。
The second one checks the condition first and if true, decrements i
after the loop body has been executed.第二个首先检查条件,如果为真,则在循环体执行后递减
i
。
The three parameters ( initialization
, condition
and increment
) in for
are optional (but they still requires the semicolon ;
). for
中的三个参数( initialization
、 condition
和increment
)是可选的(但它们仍然需要分号;
)。 So, you can safely write for(;;)
and it's the same as while(1)
.因此,您可以安全地编写
for(;;)
并且它与while(1)
相同。 So, expressions like所以,表达式如
for(; x > y; x++) { ... }
for(;1;x++) { if(x > y) break;
for(;;) { if(x++ > y) break;
for(;x++ > y;) { ... }
Are valid and equivalent.有效且等效。 In this, the increment occurs in the conditional parameters, just like in your code example.
在这种情况下,增量发生在条件参数中,就像在您的代码示例中一样。
Well, it is the same as saying for( i=0 or some value; --i <= 0 ; do nothing){}
and --i
is an predecrement operator.嗯,这和说
for( i=0 or some value; --i <= 0 ; do nothing){}
是一样的, --i
是--i
减运算符。
That means when this piece of code, ie, --i
, is being read, the value of i
will decrease by 1
at the same moment.这意味着当这段代码,即
--i
正在被读取时, i
的值将同时减少1
。
There are three main components in a for
loop. for
循环中包含三个主要组件。
Initialization, condition, and afterthought.初始化、条件和事后思考。
Initialization happens once at the start of the entire statement.初始化在整个语句的开头发生一次。 Condition happens before every cycle.
条件发生在每个周期之前。 Afterthought comes after every cycle.
事后考虑在每个周期之后。 Consider
i
starts at 2:考虑
i
从 2 开始:
for (; --i >= 0; )
Initialization: i = 2
Condition: i = 1
Afterthought: i = 1
Condition: i = 0
Afterthought: i = 0
Condition: i = -1
Exit
In the other case在另一种情况下
for( ; i>=0; --i)
Initialization: i = 2
Condition: i = 2
Afterthought: i = 1
Condition: i = 1
Afterthought: i = 0
Condition: i = 0
Afterthought: i = -1
Condition: i = -1
Exit
You can see that the second version actually lasts one cycle longer!你可以看到第二个版本实际上持续了一个周期!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.