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.

Printable View

- 06-30-2010, 02:40 PMRanuif 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 PMPhHein
No, that is not possible, without evaluating very expression on it's own.

- 06-30-2010, 03:03 PMarun9683
No its not possible to identify which condition became true.

The work around is have nested if blocks - 06-30-2010, 03:15 PMJosAH
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

kind regards,

Jos - 06-30-2010, 04:23 PMTolls
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 PMPhHein
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 AMRanu
Yeah,I went for separate if blocks.Thanks all