Thread: Short question concerning 'if'

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    15

    Short question concerning 'if'

    Take a look at this code:

    int number = 9;
    if ( number++ == 10)
    cout << ”True” ;
    else
    cout << ”False";

    Nevermind the bad formatting, does this code result in "true" or "false"?
    To me, it is obvious that 'number++==10' is true if 'number' initially has the value '9'. The thing is, I just did a test for an internet C++ course that I am attending, which gave us this code. When I chose that the result would be "true", I got a message stating that "false" was the right answer.

    Have I missed something, or is it the test that was faulty?
    The only thing I can think of is the position of '++', and that 'number++' makes the addition to number after the sentence in which it is written. But I do not know if that is the case.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    False, since you are using the post-increment operator ++

    Try:
    Code:
    int number = 9;
    if (++number == 10)
        cout << "True";
    else
        cout << "False";
    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
    Jul 2007
    Posts
    15
    Quote Originally Posted by laserlight View Post
    False, since you are using the post-increment operator ++

    Try:
    Code:
    int number = 9;
    if (++number == 10)
        cout << "True";
    else
        cout << "False";
    Thanks for your fast reply.
    Can I ask you, if '++number' makes the change to number instantly, when does the post-increment ++ take effect?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also immediately, but the value of the expression is the old one.

    ++var: var is incremented, the value of the expression is the new value of var.
    var++: var is incremented, the value of the expression is the old value of var.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    15
    Quote Originally Posted by CornedBee View Post
    Also immediately, but the value of the expression is the old one.

    ++var: var is incremented, the value of the expression is the new value of var.
    var++: var is incremented, the value of the expression is the old value of var.
    I see, both take effect immediately but the value of the expressions are different?
    That explains it. ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. int and unsignd short
    By a0161 in forum C Programming
    Replies: 2
    Last Post: 11-11-2008, 04:14 AM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Simple Short Class Question
    By Travis Dane in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2002, 07:12 PM
  4. traversal of tree (short question)
    By mackol in forum C Programming
    Replies: 5
    Last Post: 11-25-2002, 08:41 AM
  5. Replies: 1
    Last Post: 01-23-2002, 03:34 AM