如果选择上面的if语句,还是会解决吗?

Por*_*ker 0 c++ if-statement

在这组陈述中:

if(robot1Count < 12) {
    robot1Count++;
}
else if(robot1Count < 24) {
    robot1Count++;
}
else if(robot1Count < 36) {
    robot1Count++;
}
else if(robot1Count < 48) {
    robot1Count++;
}
else {
    robot1Count = 0;
}
Run Code Online (Sandbox Code Playgroud)

想象一下,这是一个无限循环,这个循环是否会从0遍历到48,变为0.我想知道的是,如果第一个块被执行,是否会忽略以下所有块?或者我应该将第二个更改为else if(robot1Count <24 && robot1Count> = 12)?或者这没关系?

jas*_*son 7

我想知道的是,如果执行第一个块,是否会忽略以下所有块?

是的,他们都会被忽视.甚至不会评估这些条件.但是你知道,你可以亲自测试一下!

if(robot1Count < 12) {
    printf("< 12");
    robot1Count++;
}
else if(robot1Count < 24) {
    printf(">= 12 && < 24");
    robot1Count++;
}
else if(robot1Count < 36) {
    printf(">= 24 && < 36");
    robot1Count++;
}
else if(robot1Count < 48) {
    printf(">= 36 && < 48");
    robot1Count++;
}
else {
    printf(">= 48");
    robot1Count = 0;
}
Run Code Online (Sandbox Code Playgroud)

然后你可以看到哪些消息被打印到控制台然后你就会知道并感觉到发生了什么!