0

say I have an if statement as such

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

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

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
asdasdasdasdqwe
  • 321
  • 1
  • 8
  • 20

7 Answers7

3

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

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

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).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

No. You'll have to do something like:

if(condition1 || condition2 || condition3)

{

if (condition1) {
}

if (condition2) {
}

if (condition3) {
}

//do something

}

David Harris
  • 2,332
  • 1
  • 13
  • 25
1

It is possible to find out which of the conditions was true by querying each of them using another if, effectively rendering the first if useless.

cli_hlt
  • 7,072
  • 2
  • 26
  • 22
1

A simple method.

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

Or if you know that only one of the conditions is going to be true, consider using a switch.

IsisCode
  • 2,490
  • 18
  • 20
0

Before you call the if statement, you can call:

System.out.println(condition1);
System.out.println(condition2);
System.out.println(condition3);

to find out which of the conditions was true. If you would like to make the program behave differently according to the condition you will need to put that code in a separate if statement.

Bartvbl
  • 2,878
  • 3
  • 28
  • 45
0

No. However you can achieve by: i. Using seperate if else within the 3 or conditions or ii. break the three or conditions in separate pairs to find out matching value

Panshul
  • 956
  • 1
  • 10
  • 25
-1

You have the short circuit operators. || and &&.

So say for instance you have the condition,

if( x && y || z)

If x && y doesnt evaluate to true, then y and z are never compared. However if X and Y are true, then it will test y or z. In this case your true value comes from the fact that x and y is true, and y or z is true.

mortdeus
  • 197
  • 2
  • Also if you need to check the value, why not test them before hand with your IO function. std::cout << "Condition 1 is: << condition1 << "Condition 2 is: << condition2 << ect... – mortdeus Feb 01 '12 at 20:29
  • Something seems fishy here. For (`x && y || z`): If x and y are true then z is never checked; if x or y is false, then z is checked. – Austin Salonen Feb 01 '12 at 20:42
  • The logic is if the first argument is And, and it evaluates to false. Then there is a possibility that the second condition could also be false. However if its true, it doesnt need to check that condition. However the opposite is true if you swap Or and And comparisons. If Or is false, then And is never going to be a possible true statement therefore the check isnt necessary. However if Or is true, then its possible to either get a true or false in the second check. Its all about optimization. – mortdeus Feb 02 '12 at 21:18