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

Is there a way to check which condition returned TRUE to --do something here--

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • 5
    either use different if blocks or assign value of condition to some variable and make condition ORing on it – jmj Mar 28 '14 at 22:13
  • I am restricting it to not to use different if block – user2195582 Mar 28 '14 at 22:15
  • so then do this: if(condition1||condition2||condition3){if(condition1){}if(condition2){}if(condition3){}}. That's really the only way. This is assuming of course that condition1,2,3 are booleans. If not declare them as such before the beginning of the if block for efficiency's sake – k_g Mar 28 '14 at 22:21
  • @k_g "I am restricting it to not to use different if block" – cheseaux Mar 28 '14 at 22:22
  • Sorry, I thought it meant another independent if block. – k_g Mar 28 '14 at 22:22

3 Answers3

1

You're not supposed to be concerned about which of this three conditions returned true, otherwise it would mean that you need to use nested if statements (but apparently you don't want that).

You could either print the three conditions in the if-statement, but I don't see the point, or use the debugger if it has something to do with debugging.

cheseaux
  • 5,187
  • 31
  • 50
0

In this case you have operators with same precedence and it should read as

if( condition1 || (condition2 || condition3 ) {

}

if condition1 is true then condition2 and condition3 won't be checked.

If you want to know which condition exactly evaluated to true you must do an if else statement

if( condition1 )
{
    // do something when cond1 is true
}
else if( condition2 )
{
   // do something when cond2 is true 
}
else if( condition3 )
{
   // do something when cond3 is true
}
user9349193413
  • 1,203
  • 2
  • 14
  • 26
0

You could make each condition a method, and then in each method registering what the condition results in before returning.

Johan Frick
  • 1,084
  • 1
  • 10
  • 18