这个循环是如何工作的?我无法理解
How does this loop work? I am unable to understand it
for(int i=1;i<=n;){
f++;
if((i++==p) || (i++==p))
break;
}
示例 1:n=7,p=3,f=0
;所以f
的值应该是1吧?但它给出 f=2
作为输出
示例 2:n=7,p=4,f=0
;它给出的输出为 f=2
示例 3:n=7,p=5,f=0
;它给出的输出为 f=3
帮我理解一下。
案例 1 当 n=7,p=3,f=0
。让我们在执行 for 循环时查看不同变量的值。
迭代 1
for(int i=1;i<=n;) //here n = 7
{
f++; //here f becomes 1 that is now we have f = 1
if((i++==p) || (i++==p)) // variable i is incremented by 1 and becomes 2 because of the
//first i++. But note the old value of i(which is 1) is returned by i++. So although
//right now i = 2, its old value(which is 1) is compared with the value of variable
//p(which is 3). Since 1 and 3 are not equal the next (i++==p) is evaluated. And so
//again variable i is incremented and becomes 3. But here also the old value of
//i(which is 2) is used to compare with value of variable p(which is still 3). But since
//2 is not equal to 3 the control flow doesn't go inside the `if` and so `break` is not
//executed.
break;
}
迭代 2
for(int i=1;i<=n;) //here n = 7
{
f++; //here f becomes 2 that is now we have f = 2
if((i++==p) || (i++==p)) // Now i is incremented again because of the first i++ and
//becomes 4. But here also the old value of variable i(which is 3) is used to compare
//with value of variable p(which is 3) and since 3 = 3 the condition is satisfied
//and we go inside the `if` statement and `break` is executed. Note at this point
//the value of f is 2 .That is why you get f =2 as output.
break;
}
类似地,您可以计算案例 2,其中 n=7,p=4,f=0;
。
请注意,您can/should为此目的使用了调试器。
for(int i=1;i<=n;){
f++;
if((i++==p) || (i++==p))
break;
}
示例 1:n=7,p=3,f=0
;所以f
的值应该是1吧?但它给出 f=2
作为输出
示例 2:n=7,p=4,f=0
;它给出的输出为 f=2
示例 3:n=7,p=5,f=0
;它给出的输出为 f=3
帮我理解一下。
案例 1 当 n=7,p=3,f=0
。让我们在执行 for 循环时查看不同变量的值。
迭代 1
for(int i=1;i<=n;) //here n = 7
{
f++; //here f becomes 1 that is now we have f = 1
if((i++==p) || (i++==p)) // variable i is incremented by 1 and becomes 2 because of the
//first i++. But note the old value of i(which is 1) is returned by i++. So although
//right now i = 2, its old value(which is 1) is compared with the value of variable
//p(which is 3). Since 1 and 3 are not equal the next (i++==p) is evaluated. And so
//again variable i is incremented and becomes 3. But here also the old value of
//i(which is 2) is used to compare with value of variable p(which is still 3). But since
//2 is not equal to 3 the control flow doesn't go inside the `if` and so `break` is not
//executed.
break;
}
迭代 2
for(int i=1;i<=n;) //here n = 7
{
f++; //here f becomes 2 that is now we have f = 2
if((i++==p) || (i++==p)) // Now i is incremented again because of the first i++ and
//becomes 4. But here also the old value of variable i(which is 3) is used to compare
//with value of variable p(which is 3) and since 3 = 3 the condition is satisfied
//and we go inside the `if` statement and `break` is executed. Note at this point
//the value of f is 2 .That is why you get f =2 as output.
break;
}
类似地,您可以计算案例 2,其中 n=7,p=4,f=0;
。
请注意,您can/should为此目的使用了调试器。