[英]What does the code print? Exercise in C
I am not sure why does this code print "h=13" and not "h=2".我不确定为什么此代码打印“h=13”而不是“h=2”。 Does anyone have an idea?
有没有人有想法?
#include <stdio.h>
int main() {
int j,h=1;
for(j=0;j<50;j++) {
if(j%6==1) continue;
h++;
if(j==7 || j==14 || j==21)
break;
}
printf("h=%d",h);
return 0;
}
j = 0
neither of the if statements return a value of 1, and thus h
is incremented.j = 0
两个 if 语句都不返回值 1,因此h
递增。j = 1
in (j % 6 == 1)
, 1 % 6 will give a remainder of 1 .j = 1
in (j % 6 == 1)
, 1 % 6 将给出 1 的余数。 The statement j % 6
is true and so, h is not incremented .j % 6
为真,因此 h不递增。 (the '%' is a Remainder Operator) j = 2
to j = 6
neither of the if statements return a value of 1, and thus h
is incremented.j = 2
到j = 6
两个 if 语句都不返回值 1,因此h
递增。j = 7
in (j % 6 == 1)
, 7 % 6 will give a remainder of 1 .j = 7
in (j % 6 == 1)
, 7 % 6 将给出 1 的余数。 The statement j % 6
is true and so, h is not incremented .j % 6
为真,因此 h不递增。j = 8
to j = 12
neither of the if statements return a value of 1, and thus h
is incremented.j = 8
到j = 12
两个 if 语句都不返回值 1,因此h
递增。j = 13
in (j % 6 == 1)
, 13 % 6 will give a remainder of 1 .j = 13
in (j % 6 == 1)
, 13 % 6 的余数为 1 。 The statement j % 6
is true and so, h is not incremented .j % 6
为真,因此 h不递增。j = 14
the statement j == 14 is
true and thus the break statement is executed.j = 14
,语句j == 14 is
真,因此执行 break 语句。 h will be incremented for : j
= 0, j
= 2 to j
= 6, j
= 8 to j
= 12, j
= 14 which is a total of 12 times. h 将增加:
j
= 0, j
= 2 到j
= 6, j
= 8 到j
= 12, j
= 14,总共 12 次。
Total of 12 + 1 ( h = 1
) = 13总计 12 + 1 (
h = 1
) = 13
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.