Post Increment Operator

I was told by my 'C' tutor that the post increment operator comes into action when the statement in which it has occoured has terminated that is after ';' symbol... I never found anything wrong with it but the Exercise given in my C++ book has caused me a problem...

[b]x=7;
x++==8||x==7;
/b]

The answer is false...

but it should be true as the post increment operator comes into action after the statement is terminated so the value of x remains 7 in the statement and the comparison x==7 should return TRUE...

I am really confused is the post increment operator in C++ follows different rules from that of 'C'??

My Sincere Thanks for giving your time,sharing the knowledge and helping me out...
closed account (zb0S216C)
There's two ways you can approach this:

1)
1
2
3
4
if( ( ( X++ ) == 8 ) || ( X == 7 ) )
{
    // ...
}


2)
1
2
3
4
if( ( ( X + 1 ) == 8 ) || ( X == 7 ) )
{
    // ...
}

The increment operator simply increases the left or right-hand operand by one. It's best to encase the increment operator with parentheses. This way, the left or right-hand operand is incremented before evaluation. This changes the operator precedence, I believe.

Wazzak
Last edited on
closed account (1vRz3TCk)
The only thing for certain is that by the sequence point (;) x will be incremented.

The compiler may lead to code that evaluates the first time (x++==8) and then increments it and then evaluates x again (x==7).

Or it may do something completely different.
i mean

x=7;
x++==8 || x==7;

Sorry
closed account (zb0S216C)
CPPAlien, is this expression part of an if statement, or is it an expression to resolve a value for a Boolean variable?

Wazzak
Framework, I am intrested in its boolean value that is, the result will be true or false???
Codemonkey, post increment comes into action after the termination of that statement that is after ; character.... where as pre increment comes into effect at the very same statement...
closed account (1vRz3TCk)
post increment comes after evaluation of the term that has the post increment and all side effects are in place by the next sequence point. There is nothing to say exactly when this will happen.

A line such as x++==8 || x==7; could be expanded to ((x==8, x= x+1) || (x==7) ); or ((x==8) || (x==7))(x=x+1); depending on the implementation of the compiler. They are both valid interpretations of the C++ standard. This is why you don't do multiple read/writes in the same expression with increment/decrement operators.

I actually forgot, there is a sequence point between evaluating the left and right side of the OR, so the post increment side effect will be in place before the RHS is evaluated.
Last edited on
closed account (zb0S216C)
CPPAlien wrote:
I am intrested in its boolean value that is, the result will be true or false??? (sic)

The result of an if statement evaluation either returns true or false. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
if( 1 < 2 )
{
    // The result is: true. This block will be executed.
}

if( ( 1 < 2 ) && ( 3 > 4 ) )
{
    // The result is: ( true ) && ( false ). This block won't be executed.
}

if( ( 1 < 2 ) || ( 3 > 4 ) )
{
    // This result is ( true ) || ( false ). This block will be executed.
}

When using evaluation statements to assign Boolean values to a bool variable, you would write something like this:

1
2
3
4
bool Result( 1 < 2 ); // Result is assigned to 'true'.

// Here's the ternary operator alternative:
bool Result( ( 1 < 2 ) ? true : false ); // Result is assigned to 'true'. 

Wazzak
Last edited on
Topic archived. No new replies allowed.