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...
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...
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.
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.
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'.