在这组语句中:
if(robot1Count < 12) {
robot1Count++;
}
else if(robot1Count < 24) {
robot1Count++;
}
else if(robot1Count < 36) {
robot1Count++;
}
else if(robot1Count < 48) {
robot1Count++;
}
else {
robot1Count = 0;
}
假设这是一个无限循环,这个循环会不会从0遍历到48,变成0。我想知道的是,如果第一个 block 被执行,后面的 block 会被忽略吗?或者我应该将第二个更改为 else if(robot1Count < 24 && robot1Count >= 12) 吗?或者这无关紧要?
最佳答案
The thing I'm wondering is if the first block is executed, would all the following blocks be ignored?
是的,它们都会被忽略。条件甚至不会被评估。但是你知道,你可以自己测试一下!
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;
}
然后您可以看到哪些消息被打印到控制台,然后您就会知道并感觉到发生了什么!
关于c++ - 如果选择上面的 if 语句,else-is 会解析吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10000703/