# Thread: if block with logical or

1. Member
Join Date
Jun 2010
Posts
13
Rep Power
0

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

2. No, that is not possible, without evaluating very expression on it's own.

3. Member
Join Date
Jun 2010
Location
Bangalore,India
Posts
70
Rep Power
0
No its not possible to identify which condition became true.

The work around is have nested if blocks

4. 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:

Java 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

5. Moderator
Join Date
Apr 2009
Posts
13,132
Rep Power
23
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.

6. Just don't use short circuiting and Jos' approach:
Java Code:
`if((alb= a > b) | (blc= b > c) | (cld= c > d))`

7. Member
Join Date
Jun 2010
Posts
13
Rep Power
0
Yeah,I went for separate if blocks.Thanks all

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•