Thread: precedence questions

  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
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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, 06: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, 12:05 PM