if conditions executing

This is a discussion on if conditions executing within the C Programming forums, part of the General Programming Boards category; I have a simple and short question that I just cant find an answer for. If I code Code: int ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    36

    if conditions executing

    I have a simple and short question that I just cant find an answer for.

    If I code

    Code:
    int x = 1;
    int y = 2;
    int z = 3;
    if (x == y--)
    z+=3;
    and then output each variable....should

    1. y be decremented and print out as "1"
    2. the statement after the if statement be executed


    I ask because in my program, y does decrement (which seems weird to me because I feel like the condition is only check), and because the z variable is not increased. However I think this is because x is checked against y before it is decremented.

    Clarification would be great, thanks!

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    77
    If I recall correctly, -- is defined as a decrement operator. You can use that as a standalone function. Although, it's been a long time since I've coded in C...

    Therefore, if you check using

    Code:
    if (x == (y-1))
    (I'm a bit of a parenthesis ............. I never take any chances, though you could try it without the internal parentheses)

    That should do a proper check without actually decrementing y.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Yes it is a decrement operator. and if i check x against (y-1) it doesnt decrement y.

    However, I am confused why y-- is being executed since it is in an if condition. Is this a characteristic of c?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    Quote Originally Posted by ollie88r View Post
    Yes it is a decrement operator. and if i check x against (y-1) it doesnt decrement y.

    However, I am confused why y-- is being executed since it is in an if condition. Is this a characteristic of c?
    The compiler doesn't care where the decrement occurs - the effect is the same: the variable is decreased by one.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    36
    Ok

    thanks sebastiani

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Or to the point: the (parentheses) part of an if condition must always be evaluated, to see whether the condition is in fact true or false (putting short-circuiting aside for the moment). So any operation/function call/whatever inside that parentheses, you should expect to happen. If an operation/function call/whatever inside the true part, or the false part, then that may or may not happen depending on the condition.

    (Short circuiting means that in a condition such as, say, this || that, if "this" is true then that will not be executed, since it is irrelevant to evaluating the entire expression -- the expression must be true so why bother.)

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    77
    Oh, so for if (x == y-1), that's ok, because y-1 has to be executed for the == to be evaluated. That's a good tidbit. Thanks tabstop.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Molokai View Post
    Oh, so for if (x == y-1), that's ok, because y-1 has to be executed for the == to be evaluated. That's a good tidbit. Thanks tabstop.
    It has to be evaluated, yes. Notice the difference though: y-1 doesn't change y (it merely subtracts one from y and evaluates to that value); y-- does change y (in fact, it's sort of the opposite: the original, larger value of y is evaluated instead of the new value of y).

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by ollie88r View Post
    I have a simple and short question that I just cant find an answer for.

    If I code

    Code:
    int x = 1;
    int y = 2;
    int z = 3;
    if (x == y--)
    z+=3;
    and then output each variable....should

    1. y be decremented and print out as "1"
    2. the statement after the if statement be executed


    I ask because in my program, y does decrement (which seems weird to me because I feel like the condition is only check), and because the z variable is not increased. However I think this is because x is checked against y before it is decremented.

    Clarification would be great, thanks!
    if the increment ++ or decrement -- operator precedes the variable, the variable is modified before evaluation, if it comes after the variable is modified after evaluation. You should be able to figure out the rest on your own
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newbie question - if statements without conditions
    By c_h in forum C++ Programming
    Replies: 2
    Last Post: 07-18-2008, 10:42 AM
  2. Giving instruction to an executing C code
    By darkwalk in forum C Programming
    Replies: 2
    Last Post: 10-24-2006, 12:01 PM
  3. Replies: 40
    Last Post: 09-01-2006, 12:09 AM
  4. Executing a program
    By trancedeejay in forum C Programming
    Replies: 7
    Last Post: 03-06-2006, 07:55 AM
  5. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM

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