# Thread: order of multiple test expression?

1. ## order of multiple test expression?

I don't want to reference outide my array, so is this test valid, or will the second expression try to evaluate is the first is false?

if ((row+1 < maxrow) && (array[row+1][col] == EMPTY)

Also, for multiple tests, what is the order?
example:
if (((a && b) || (c && d)) || (e && f))

2. If you are using parentheses ( ), like you are, then the order will be excuted in the order from left to right...the order is the same for the bottom example....

(a && b) is checked, then
(c && d) is checked, then last
(e && f) is checked...

and if any of those are true, your following code segment will run...if you don't use parentheses, there is a certain order of precedence which C will evaluate all operators in...

3. >I don't want to reference outide my array, so is this test valid,
>or will the second expression try to evaluate is the first is false?
>
>if ((row+1 < maxrow) && (array[row+1][col] == EMPTY))

This test seems valid. Both expressions will be evaluated, it's not true that if the second expression will not be evaulated if the first one is false. Both will be evaluated and then the whole expression, the &&, will be evaluated.

By the way, you're using a two dimensional array, shouldn't you also check the column-range in order to avoid referencing outside the array? That would be just the same.

if ((row+1 < maxrow) && (col+1 < maxcol))

4. When you are using parenhesis(sp) the evalutaion starts with the innermost expresion.

5. > I don't want to reference outide my array, so is this test valid,
> or will the second expression try to evaluate is the first is false?
> if ((row+1 < maxrow) && (array[row+1][col] == EMPTY)
Looks good to me.

&& and || are special in that they operate in short-circuit mode.

Given
if ( a && b ) c;

if a evaluates to TRUE, b is evaluated, and if b is also TRUE, the following statements(c) are run

if a evaluates to FALSE, the whole expression will be FALSE anyway, so b is not even evaluated, and the following statements(c) are not executed.

> Both expressions will be evaluated, it's not true that if the second expression will not be evaulated if the first one is false.
This is incorrect - see above.

6. Thanks all. Problem solved.