Thread: Logical Operators

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    10

    Logical Operators

    Do the ||, &&, ! operators have a specific order in which the compiler reads them? If so what is it?

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    I believe they have the same precedence, so the compiler will read them left to right.
    Any one else know for sure.

  3. #3
    Zzido
    Guest

    Thumbs up

    The answeres in my C book somwhere but you shouldn't really need it. Just keep everything separated into brackets then you can be sure of whats being evaluated when.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    The "!" (NOT) operator has higher precedence than the logical AND and OR operators. AND and OR have the same precedence and are evaluated left to right, (although some would argue that && is higher than ||).

    Whatever, it is poor programming practice to rely on operator precedence.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    liberal use of parentheses and proper coding style means that you can all but forget about operator precedence.This is a feature of c++ that causes more problems than it fixes.
    for instance this looks good....
    while ('\n' != *dest++ = *source++);
    but i would rather see this anyday....
    while (1)
    {
    *dest=*source;
    dest++;
    source++;
    if(*(dest-1)=='\n') break;
    }
    both do exactly the same thing.But to me and many others it is much more obvious what is going on in the second example and with use of parenthesis you do not need worry about operator precedence.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The best precedence advice I can give is this.
    Multiplication and division come before addition and subtraction. Everything else should be in parentheses.

    Btw: && || evaluate left to right, set up your tests so that the most critical test is on the left and evaluation will stop if it returns false.

    -Prelude
    My best code is written with the delete key.

  7. #7
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> while ('\n' != *dest++ = *source++);

    Exactly SC. In some places, these incredibly terse, "efficient" versions are applauded. These people have obviously never been in industry! As for efficiency, I would imagine any half decent compiler would reduce the two examples you gave to the same object!

    Readability and maintainability is the key concepts.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Codeblocks and logical operators
    By fhbwghads in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2008, 05:22 AM
  3. logical operators
    By Marlon in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 02:55 AM
  4. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 04:33 PM
  5. logical operators
    By sballew in forum C Programming
    Replies: 4
    Last Post: 09-04-2001, 06:24 PM