I'm taking a Intro to Computer Science and Intro to C programming class at De Anza and struggling a bit understanding the difference between postfix and prefix increment/decrement expressions. The examples I'm following along with in the book aren't doing it for me so I hope someone here can word it in a way that will get me to understand it. Much appreciated.
7 Replies - 759 Views - Last Post: 20 October 2011 - 07:54 AM
#1
Need a bit of help understaning Postfix and Prefix
Posted 17 October 2011 - 11:46 PM
Replies To: Need a bit of help understaning Postfix and Prefix
#2
Re: Need a bit of help understaning Postfix and Prefix
Posted 18 October 2011 - 01:47 AM
It's all about when the expression is actually evaluated (or when you get the "answer" to the expression), so say x = 0 if you do y = x++ this is equivalent to:
So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).
If you were to do y = ++x this is equivalent to:
Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement
y = x x = x + 1
So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).
If you were to do y = ++x this is equivalent to:
x = x + 1 y = x
Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement
This post has been edited by andy_pleasants: 18 October 2011 - 01:48 AM
#3
Re: Need a bit of help understaning Postfix and Prefix
Posted 18 October 2011 - 07:27 AM
#include <iostream>
using namespace std;
int main()
{
int x = 2;
int y = 2;
cout << x++ << endl; // x = 2
cout << x << endl; // x = 3
cout << ++y << endl; // y = 3
cout << y << endl; // y = 3
// also works with x-- and y--
}
#4
Re: Need a bit of help understaning Postfix and Prefix
Posted 18 October 2011 - 10:35 AM
andy_pleasants, on 18 October 2011 - 01:47 AM, said:
It's all about when the expression is actually evaluated (or when you get the "answer" to the expression), so say x = 0 if you do y = x++ this is equivalent to:
So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).
If you were to do y = ++x this is equivalent to:
Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement
y = x x = x + 1
So y is set to the value of x and then x is incremented, this is postincrement (increment AFTER evaluation).
If you were to do y = ++x this is equivalent to:
x = x + 1 y = x
Notice how the increment is now before the evaluation. x++, ++x is just short hand basically, and the same rules apply for pre and postdecrement
Thx for the explanation Andy. After some visuals, I do understand it now. I do have one last question though...
If postfix and prefix evaluate to the same value, in which cases would someone use postfix as oppose to prefix?
#5
Re: Need a bit of help understaning Postfix and Prefix
Posted 18 October 2011 - 11:40 AM
You should normally use the pre-increment (++i) to avoid the possibility of a temporary copy being made. But there are times where you will need to use the post-increment, to insure the value is altered after it is used.
Jim
Jim
#6
Re: Need a bit of help understaning Postfix and Prefix
Posted 19 October 2011 - 03:28 AM
Quote
If postfix and prefix evaluate to the same value, in which cases would someone use postfix as oppose to prefix?
It's more about when the increment happens pre-increment is saying "add 1 to the value, then give me the value" and post-increment is asking "give the value, then add 1 to it".
And also, technically they don't evaluate to the same value x++ evaluates to x and ++x evaluates to x + 1
#7
Re: Need a bit of help understaning Postfix and Prefix
Posted 19 October 2011 - 09:14 AM
I would like to thank Andy and everyone else who left a comment. I thought I was going to be lost but know feel kind of dumb for not getting the material in the first place. I'm sure I'll be asking other questions and from what I read online, I am sure I would be asking questions again about pointers next quarter. Thanks again!!!
#8
Re: Need a bit of help understaning Postfix and Prefix
Posted 20 October 2011 - 07:54 AM
Quote
I thought I was going to be lost but know feel kind of dumb for not getting the material in the first place
The only dumb questions are the ones that aren't asked! Keep asking
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|