Thread: Order of Operations help (no code)

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    158

    Order of Operations help (no code)

    Assume that x is 11, y is 6, and z is 1 at the beginning of each statement.


    w=x-- + y --*++z

    The answer is 23, but i cant figure out whats going on there. what is ++ and what --?


    2. !!(x*4)+x%y


    answer is 6, and again idk whats going on

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    The value of x-- is the value of x
    the value of y-- is the value of y
    the value of ++z is 1 more than the value of z

    so, in w = x-- + y -- * ++z; you can replace the expressions by their values
    w = 11 + 6 * 2
    w = 11 + 12
    w = 23

    ~~~~~~~~

    The logical negation operator (!) yields a value of 0 or 1

    !!(WHATEVER) is 0 or 1

    it is 0 if WHATEVER is 0; it is 1 otherwise

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    158
    Quote Originally Posted by qny View Post
    The value of x-- is the value of x
    the value of y-- is the value of y
    the value of ++z is 1 more than the value of z

    so, in w = x-- + y -- * ++z; you can replace the expressions by their values
    w = 11 + 6 * 2
    w = 11 + 12
    w = 23

    ~~~~~~~~

    The logical negation operator (!) yields a value of 0 or 1

    !!(WHATEVER) is 0 or 1

    it is 0 if WHATEVER is 0; it is 1 otherwise
    Thanks. I have just one more q

    w=x==y || x!=y && z>x.

    Again x=11, y =6, and z=1.

    Answer is 0 because x==y evaluates to false, and the second part of the statement is false as well. So false || false=0, correct?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    All the logical operators (&& (AND), || (OR), == (EQUALITY), etc) yield either 0 or 1.

    Your description of why the value is 0 is correct.
    But there may be short-circuit evaluation in different expressions. Just remember that not all sub-expressions need to be evaluated to get the final result

    The statement
    x = ((4 == 4) || (format_hard_disk()));
    will never call the function format_hard_disk() because its value is not needed to determine the final value to assign to x.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Code:
    w=x==y
    You know that the first equal sign,because it is single it is used for assignment and not for comparison.
    And yes false || false = false .Because of boolean algebra false is 0 and true is 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of operations question
    By GuitarNinja in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2009, 06:14 AM
  2. Order of operations question
    By nicomp in forum C++ Programming
    Replies: 9
    Last Post: 09-20-2006, 07:18 PM
  3. order of operations...
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-21-2004, 11:38 PM
  4. Order of Operations
    By C-Duddley in forum C Programming
    Replies: 3
    Last Post: 12-06-2002, 08:10 PM
  5. order of operations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 10-31-2002, 08:51 PM