Thread: combining boolean conditions with parenthesis

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    31

    combining boolean conditions with parenthesis

    Hello,

    I was wondering whether the expected parenthesis hierarchy is respected when combining boolean conditions in a do-while loop.

    To be concrete, consider this:
    Code:
    do
    {
    ...
    }
    while ( A && ( B || C || D ) )
    Then, the loop terminates when either A is false or (B||C||D) is false, and for this last one to be false, then B and C and D have to be false.

    Am I right? If so, this works in both C and C++?

    Thanks for your help,

    mc61




    Thanks for your help,

    mc61

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It terminates if either A is false on it's own or B, C and D are all false.

    Note that A is tested first, and if it's FALSE, no other test has to be performed, and you can rely on this in the compiler (for example you can do
    Code:
    if (p != NULL && *p != 7)  ...
    and you can be sure that *p is never being accessed if p is NULL.

    And yes, it's the same in C and C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    Thanks matsp!

    mc61

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Novice needs help
    By ghaasemi in forum C++ Programming
    Replies: 9
    Last Post: 05-30-2009, 08:20 AM
  2. Casting boolean as string
    By doofusboy in forum C Programming
    Replies: 11
    Last Post: 11-10-2005, 12:24 PM
  3. Replies: 3
    Last Post: 11-11-2003, 03:44 AM
  4. Working with boolean...
    By CompiledMonkey in forum C Programming
    Replies: 4
    Last Post: 11-03-2003, 10:39 AM
  5. Use struct to create a boolean type
    By skyglin in forum C Programming
    Replies: 6
    Last Post: 06-18-2003, 08:21 PM