if-statement - 对多个 if 条件使用单个 else 语句
问题描述
我正在为一个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 条件,如果条件为真则执行代码,并且仅当 if 条件都不为真时才运行 else 语句。请注意,我不能使用else if
,因为我想检查所有 if 条件,如果没有一个为真,我想运行 else If 条件不相互依赖
解决方案
您可以使用在任何if
s 中设置的布尔标志。
bool noPathTaken = true;
if ( condition1 ) {
noPathTaken = false;
// ...
}
if ( condition2 ) {
noPathTaken = false;
// ...
}
// ...
if ( noPathTaken ) { // this would be your "else"
// ...
}
推荐阅读
- angular - 如何使用 Angular 处理错误主体
- c# - 需要时获得依赖?
- javascript - 按数据键从高到低对 JSON 对象进行排序 - Javascript
- javascript - 时间计数器 - 打字稿
- c# - 为什么 performanceCounter 显示超过 100 %
- javascript - 如何检查多个变量是否为真?
- reactjs - 如何在反应原生中使用道具从大父组件到子组件的方法?
- spring-boot - 如何在 Spring Boot Security 的另一个微服务中调用我的身份验证微服务
- mysql - MySQL 全文搜索:为什么 MATCH AGAINST 不使用表索引
- bash - 未加载库:/usr/local/opt/openssl/lib/libcrypto.1.0.0.dylib 在 Mac 上