order of multiple test expression?

This is a discussion on order of multiple test expression? within the C Programming forums, part of the General Programming Boards category; I don't want to reference outide my array, so is this test valid, or will the second expression try to ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    Question 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. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    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. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >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. #4
    Barjor
    Guest
    When you are using parenhesis(sp) the evalutaion starts with the innermost expresion.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    > 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. #6
    Registered User
    Join Date
    Feb 2002
    Posts
    18

    Smile

    Thanks all. Problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Embedded SQL Order By
    By cjohnman in forum C Programming
    Replies: 12
    Last Post: 04-15-2008, 03:45 PM
  2. test to see if multiple values are the same
    By squeaker in forum C++ Programming
    Replies: 3
    Last Post: 06-08-2006, 01:45 AM
  3. Header file include order
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 05-17-2006, 03:22 PM
  4. How do you order your game code?
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 02-05-2006, 05:26 PM
  5. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21