# if block with logical or

• 06-30-2010, 02:40 PM
Ranu
if block with logical or
I want to know whether it is possible to to know the particular case for which the if block is true?Like
if(a>b || b>c || c>d)
{
then which case is true?
}
a,b,c,d are arbitrary numbers.
• 06-30-2010, 02:59 PM
PhHein
No, that is not possible, without evaluating very expression on it's own.
• 06-30-2010, 03:03 PM
arun9683
No its not possible to identify which condition became true.

The work around is have nested if blocks
• 06-30-2010, 03:15 PM
JosAH
Quote:

Originally Posted by Ranu
I want to know whether it is possible to to know the particular case for which the if block is true?Like
if(a>b || b>c || c>d)
{
then which case is true?
}
a,b,c,d are arbitrary numbers.

It's a bit ugly but you could do this:

Code:

```boolean alb= false; // a > b condition boolean blc= false; // b > c condition boolean cld= false; // c > d condition if|((alb= a > b) || (blc= b > c) || (cld= c > d)   // use alb and blc and cld to check which is true```
Note that if, say, blc was true cld doesn't reflect the truth value of c > d.

kind regards,

Jos
• 06-30-2010, 04:23 PM
Tolls
That if statement you put up essentially says "if any of these are true then...".

If you need to know which of them is true then presumably you need to know that in order to do some processing, so you would need an "if...else if...else".

ETA: Just realised the "if...else if...else" has a similar problem to Jos's in that it will only handle the first it encounters. But anyway, you get the idea hopefully.
• 06-30-2010, 04:31 PM
PhHein
Just don't use short circuiting and Jos' approach:
Code:

`if((alb= a > b) | (blc= b > c) | (cld= c > d))`
• 07-01-2010, 09:11 AM
Ranu
Yeah,I went for separate if blocks.Thanks all