简体   繁体   English

代码打印什么? C语言练习

[英]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;
}
  1. When j = 0 neither of the if statements return a value of 1, and thus h is incremented.j = 0两个 if 语句都不返回值 1,因此h递增。
  2. When 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) ('%' 是余数运算符)
  3. When j = 2 to j = 6 neither of the if statements return a value of 1, and thus h is incremented.j = 2j = 6两个 if 语句都不返回值 1,因此h递增。
  4. When 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不递增
  5. When j = 8 to j = 12 neither of the if statements return a value of 1, and thus h is incremented.j = 8j = 12两个 if 语句都不返回值 1,因此h递增。
  6. When 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不递增
  7. For 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM