Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Dave Kreskowiak28-Apr-13 4:48
mveDave Kreskowiak28-Apr-13 4:48 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 1:47
harold aptroot28-Apr-13 1:47 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 2:00
N8tiv28-Apr-13 2:00 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 2:23
harold aptroot28-Apr-13 2:23 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 2:35
N8tiv28-Apr-13 2:35 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 3:05
N8tiv28-Apr-13 3:05 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 3:33
harold aptroot28-Apr-13 3:33 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 3:44
mveOriginalGriff28-Apr-13 3:44 
Try an experiment.
When you see a statement involving prefix or postfix increments, mentally (or even physically) re-write it to be several statements, moving the increment outside all other statements.
C#
y = y++ + x;

Becomes:
C#
int y2 = y;
y = y + 1;
y = y2 + x;
In other words, the postfix increment of y is irrelevant, because the value is immediately discarded, and y is set to the value of the sum of the original value of y and x
C#
z = ++z + x;
Becomes:
C#
z = z + 1;
z = z + x;
That is all the compiler is doing - a prefix or suffix increment just gets done when it is met, that's all - it's syntactic sugar for the broken down statements above.

Having said that, try not to use them in "complex" statements: What happens may not be what you expect - different compilers interpret "when to do this" slightly differently, and that gave have a dramatic effect. Normally, pre-and post- increments are kept to simple things like array accesses and for loops.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)

GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 3:54
harold aptroot28-Apr-13 3:54 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:15
mveOriginalGriff28-Apr-13 4:15 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:19
harold aptroot28-Apr-13 4:19 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:26
mveOriginalGriff28-Apr-13 4:26 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:31
harold aptroot28-Apr-13 4:31 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 4:46
mveOriginalGriff28-Apr-13 4:46 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 4:59
harold aptroot28-Apr-13 4:59 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:08
mveOriginalGriff28-Apr-13 5:08 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 5:21
mvaKenneth Haugland28-Apr-13 5:21 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:25
mveOriginalGriff28-Apr-13 5:25 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 5:34
mvaKenneth Haugland28-Apr-13 5:34 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
OriginalGriff28-Apr-13 5:41
mveOriginalGriff28-Apr-13 5:41 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 6:14
mvaKenneth Haugland28-Apr-13 6:14 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
harold aptroot28-Apr-13 5:28
harold aptroot28-Apr-13 5:28 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Kenneth Haugland28-Apr-13 5:34
mvaKenneth Haugland28-Apr-13 5:34 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 21:33
N8tiv28-Apr-13 21:33 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
Keld Ølykke28-Apr-13 20:34
Keld Ølykke28-Apr-13 20:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.