precedence questions

This is a discussion on precedence questions within the C++ Programming forums, part of the General Programming Boards category; true ? 1 : 2 + 3 // why is this 1? False = 0, true = 1 right? // ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    11

    Red face precedence questions

    true ? 1 : 2 + 3 // why is this 1? False = 0, true = 1 right? // Then why is this 1 .
    also
    24 / 6 * 2 // I got this as well.
    has value 8, not value 2; likewise, the weird-looking expression
    true ? false : true ? false : true // how? I don't understand the if true then false? false true?
    Last edited by elpedoloco; 10-01-2011 at 10:53 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,935
    Quote Originally Posted by elpedoloco
    true ? 1 : 2 + 3 // why is this 1? False = 0, true = 1 right? // Then why is this 1 .
    Uh, what did you think the result should be, and why?

    Quote Originally Posted by elpedoloco
    24 / 6 * 2 // I got this as well.
    has value 8, not value 2; likewise, the weird-looking expression
    24 / 6 == 4
    4 * 2 == 8

    Quote Originally Posted by elpedoloco
    true ? false : true ? false : true // how? I don't understand the if true then false? false true?
    That is okay, I don't understand it either because I do not remember offhand what are the rules, even though I checked not too long ago. I could check again, but code like this is just badly written. If parentheses were used for grouping, it would be better, but even then the use of nested conditional operator expressions is bad practice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    As to the math one, read this page on precedence. Note that multiplication and division are at the same precedence level, so we go to the third column for associativity and notice it's left-to-right, so the expression is evaluated as Laserlight described.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    The rules are as follows
    Code:
    type variable = condition ? a : b
    a will return if the condition returns true
    b will return if it is false

    example:

    Code:
    int never_true = 5>6 ? 999 : 10;
    never_true will ALWAYS be 10.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,436
    Quote Originally Posted by elpedoloco View Post
    true ? 1 : 2 + 3 // why is this 1? False = 0, true = 1 right? // Then why is this 1 .
    In C, zero is false, and anything else is true. The ?: operator has lower precedence than addition, so true ? 1 : 2 + 3 is equal to true ? 1 : (2+3).

    Others have already explained the ?: operator, so I'll leave this example at that.
    Quote Originally Posted by elpedoloco View Post
    24 / 6 * 2 // I got this as well.
    has value 8, not value 2;
    Primary school arithmetic is applicable here, and if you don't understand primary school arithmetic then you should be ashamed (unless you are about five years old).

    Multiplication and division have the same precedence so, if more than one is in an expression, they are done left to right. 24/6*2 is equal to (24/6)*2.

    I'll leave your last example alone. You can nut it out based on explanations others have already given of the :? operator.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About precedence of && and ||
    By noobiept in forum C Programming
    Replies: 11
    Last Post: 09-12-2010, 06:38 PM
  2. precedence question
    By nimitzhunter in forum C Programming
    Replies: 4
    Last Post: 08-10-2010, 11:57 PM
  3. need some help for precedence
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 12-15-2006, 05:19 PM
  4. precedence
    By modec in forum C Programming
    Replies: 3
    Last Post: 05-22-2003, 11:37 AM
  5. precedence
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-17-2002, 11:05 AM

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