If statement 对多个if条件使用单个else语句
我正在为一个arduino项目编码,我遇到了这个问题,有人能帮我吗If statement 对多个if条件使用单个else语句,if-statement,arduino,If Statement,Arduino,我正在为一个arduino项目编码,我遇到了这个问题,有人能帮我吗 if(condition1){ //my codes here } if(condition2){ //my codes here } if(condition3){ //my codes here } ...... if(condition100){ //my codes here } else{ my codes here } 我想检查所有的if条件,如果条件为true,则执行代码,并且仅当if条件均为true时才运行el
if(condition1){
//my codes here
}
if(condition2){
//my codes here
}
if(condition3){
//my codes here
}
......
if(condition100){
//my codes here
}
else{
my codes here
}
我想检查所有的if条件,如果条件为true,则执行代码,并且仅当if条件均为true时才运行else语句。
请注意,我不能使用else if
,因为我想检查所有if条件,如果没有条件为真,我想运行else
如果条件互不依赖您可以使用在任何
If
s中设置的布尔标志
bool noPathTaken = true;
if ( condition1 ) {
noPathTaken = false;
// ...
}
if ( condition2 ) {
noPathTaken = false;
// ...
}
// ...
if ( noPathTaken ) { // this would be your "else"
// ...
}
必须在每次测试中设置所有其他标志
bool elseFlag = 1;
if(condition1){
elseFlag = 0;
my codes here
}
if(condition2){
elseFlag = 0;
my codes here
}
if(condition3){
elseFlag = 0;
my codes here
}
......
if(condition100){
elseFlag = 0;
my codes here
}
if (elseFlag) {
my codes here
}
因为
else
只绑定到前面的测试操作中,这里是if(条件100){ /C++>p/>这个Python、C++和java是如何关联的?你可以有一个初始设置为false的局部布尔值,并且在满足条件时变为真。如果可以检查这个布尔值是否为真,则可以替换其他变量。根据你拥有的if块的数量。是的,决定你想要支持哪种语言…在这里适用吗?尽管它仍然是C++23的建议。