什么是第一个“ for”?参数为空? [英] What does it mean when the first "for" parameter is blank?

查看:277
本文介绍了什么是第一个“ for”?参数为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览一些代码,并且看到了一些示例,其中省略了for循环的第一个元素。

I have been looking through some code and I have seen several examples where the first element of a for cycle is omitted.

示例:

for ( ; hole*2 <= currentSize; hole = child)

这是什么意思?

谢谢。

推荐答案

这只是意味着用户选择不将变量设置为其自己的起始值。

It just means that the user chose not to set a variable to their own starting value.

for(int i = 0; i < x; i++)

等价于。

int i = 0;
for( ; i < x; i++)

编辑(回复评论) :这些并不完全等效。变量i的范围不同。

EDIT (in response to comments): These aren't exactly equivalent. the scope of the variable i is different.

有时,后者用于拆分代码。如果在for循环本身中修改了索引变量,也可以舍弃第三条语句。

Sometimes the latter is used to break up the code. You can also drop out the third statement if your indexing variable is modified within the for loop itself...

int i = 0;
for(; i < x;)
{
...
i++
...
}

如果您放弃第二条语句,则将出现无限循环。

And if you drop out the second statement then you have an infinite loop.

for(;;)
{
runs indefinitely
}

这篇关于什么是第一个“ for”?参数为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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