如果循环 [英] if loop
问题描述
for(int i = 1; i< = 10; i ++)
cout<< i<<结束;
我预计如下:
1
2
3
4
5
6
7
8
9
10
11
说到最后一次迭代我是10.
然后i ++递增,但返回旧值10.
所以,条件再次为真。
现在,我在if的主体中是11.
显然,我错了。
后缀增量运算符在
构造中的功能是否不同?
for (int i = 1; i <= 10; i++)
cout << i << endl;
I expected the following:
1
2
3
4
5
6
7
8
9
10
11
When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.
Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
推荐答案
Roman T?ngi写道:
Roman T?ngi wrote:
for(int i = 1; i< = 10; i ++)
以上也可以写成:
for(int i = 1; i< 11; i ++)
cout<< i<< endl;
我期待以下:
1
2
3
4
5
7
8
10
当涉及到最后一次迭代时,我才是10.
然后i ++递增,但返回旧的价值10.
所以,条件再次成真。
现在,我的身体是否是11.
显然,我错了。
后缀增量运算符函数在
构造中的区别不同?
for (int i = 1; i <= 10; i++)
The above can also be written:
for(int i=1; i<11; i++)
cout << i << endl;
I expected the following:
1
2
3
4
5
6
7
8
9
10
11
When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.
Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
在这两种情况下,循环体都是在[1,10]中为i值执行的。
-
Ioannis Vranos
http://www23.brinkster.com/noicys
Roman T?ngi schrieb:
Roman T?ngi schrieb:
for(int i = 1 ; i< = 10; i ++)
cout<< i<< endl;
我期待以下:
1
2
3
4
5
7
8
10
当涉及到最后一次迭代时,我才是10.
然后i ++递增,但返回旧的价值10.
所以,条件再次成真。
现在,我的身体是否是11.
显然,我错了。
后缀增量运算符函数在
构造中有何不同?
for (int i = 1; i <= 10; i++)
cout << i << endl;
I expected the following:
1
2
3
4
5
6
7
8
9
10
11
When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.
Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
编号
如果你重新使用它可能会更清楚 - 将for循环写成while循环
(在语义上等效,除了循环范围
变量i):
int i = 1;
while(i< = 10)
{
cout<< i<<结束;
i ++;
}
HTH
Stefan
No.
Maybe it get''s clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):
int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}
HTH
Stefan
>
Roman T?ngi schrieb:
Roman T?ngi schrieb:
for(int i = 1; i< = 10; i ++)
cout<< i<< endl;
我期待以下:
1
2
3
4
5
7
8
10
当涉及到最后一次迭代时,我才是10.
然后i ++递增,但返回旧的价值10.
所以,条件再次成真。
现在,我的身体是否是11.
显然,我错了。
后缀增量运算符函数在
构造中有何不同?
for (int i = 1; i <= 10; i++)
cout << i << endl;
I expected the following:
1
2
3
4
5
6
7
8
9
10
11
When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.
Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
编号
如果你重新使用它可能会更清楚 - 将for循环写成while循环
(在语义上等效,除了循环范围
变量i):
int i = 1;
while(i< = 10)
{
cout<< i<<结束;
i ++;
}
HTH
Stefan
No.
Maybe it get''s clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):
int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}
HTH
Stefan
>
这篇关于如果循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!