Thread: Very elementary C++ question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I don't.
    Code:
    if (a <= x && x <= y) {}
    is perfectly readable as far as I am concerned . . . but if you don't think so, then by all means use parentheses.

    It's a common enough idiom.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by dwks View Post
    I don't.
    Code:
    if (a <= x && x <= y) {}
    is perfectly readable as far as I am concerned . . . but if you don't think so, then by all means use parentheses.

    It's a common enough idiom.
    Yes, that particular example is fine, but it you start getting into more complex expressions and start using different operators, you might run into problems, especially where precedence is concerned. So I choose to always be explicit with parentheses so I don't have to remember when it's safe not to use them...

    I've seen some code that did something like this:
    Code:
    if ( ptr = func() == NULL )
    and from the context it looks like the person who wrote it intended it to mean:
    Code:
    if ( (ptr = func()) == NULL )
    but because of precedence it actually meant:
    Code:
    if ( ptr = (func() == NULL) )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM