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
幫助我理解這一點。
uj5u.com熱心網友回復:
情況 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; .
請注意,您可以/應該為此目的使用除錯器。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/347924.html
