其条件为真在if语句 [英] which condition is true in an if statement

查看:176
本文介绍了其条件为真在if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,我有一个如果语句,例如

say I have an if statement as such

if(condition1 || condition2 || condition3)
{
 //do something
}

是否有可能找出其中的3个条件是真实的,当我们进入循环?

Is it possible to find out which of the 3 conditions was true when we enter the loop?

推荐答案

是的,你可以逐个检查每一个都用一样的东西:

Yes, you can check each one individually with something like:

if(condition1 || condition2 || condition3) {
    if (condition1) { doSomethingOne(); }
    if (condition2) { doSomethingTwo(); }
    if (condition3) { doSomethingThree(); }
    doSomethingCommon();
}



当然假设条件是不太可能在临时改变(如与线程,中断或存储器映射的I / O,例如)。

assuming of course that the conditions aren't likely to change in the interim (such as with threading, interrupts or memory-mapped I/O, for example).

这篇关于其条件为真在if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆