Short question concerning 'if'

This is a discussion on Short question concerning 'if' within the C++ Programming forums, part of the General Programming Boards category; Take a look at this code: int number = 9; if ( number++ == 10) cout << ”True” ; else ...

  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
    22,265
    False, since you are using the post-increment operator ++

    Try:
    Code:
    int number = 9;
    if (++number == 10)
        cout << "True";
    else
        cout << "False";
    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
    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,893
    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, 04: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

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