c++ - 如果选择上面的 if 语句,else-is 会解析吗?

标签 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;
}

假设这是一个无限循环,这个循环会不会从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/

相关文章:

c# - Razor ASP.NET : Show data from 'if' inside 'foreach' . 'break;' 不工作

javascript - 使用 && 而不是 if 是否有效?

javascript - jquery if 语句无法正常运行

python - 不满足条件时将 "?"添加到字符串

javascript - 最有可能的是如果功能不起作用

c++ - Visual C++ 不会创建 dll 文件并在 *.lib 处停止

c++ - 如何在 C++ 中将 16 位十六进制颜色转换为 RGB888 值

java - Hadoop build Failing in Windows : zconf. h 从 native.sln 中丢失?

c++ - 使 : *** No targets specified and no makefile found. 停止。 [Ubuntu]

C++ - 从/dev/urandom 中提取随机数