Click here to Skip to main content
15,892,674 members
Home / Discussions / C#
   

C#

 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 1:25
N8tiv28-Apr-13 1:25 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
NotPolitcallyCorrect28-Apr-13 1:29
NotPolitcallyCorrect28-Apr-13 1:29 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 1:32
N8tiv28-Apr-13 1:32 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
NotPolitcallyCorrect28-Apr-13 1:39
NotPolitcallyCorrect28-Apr-13 1:39 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
N8tiv28-Apr-13 1:51
N8tiv28-Apr-13 1:51 
GeneralRe: Incrementing and Decrementing - Just Trying to Understand Pin
NotPolitcallyCorrect28-Apr-13 2:10
NotPolitcallyCorrect28-Apr-13 2:10 
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 
WidmarkRob wrote:
Dummy it down for me quite a bit please.
OK, let's break it down.
WidmarkRob wrote:
int x = 10;
x is now 10.
WidmarkRob wrote:
int y = 100;
y is now 100. Pretty obvious so far.
But this
WidmarkRob wrote:
int z = y-- + x;
is a weird thing, and in order to understand it, it is necessary to understand the difference between the result of an expression and the effect of an expression. The C# rules say this about post-decrement: "the operand is decremented. The result of the expression is the value the operand had before the decrement". (see operator --[^])
So what's happening here is that y is decremented (that is the effect), so after y-- has been evaluated y will be 99, but the value is the old y, 100 (that is the result). Then it goes on to add x to that result, no surprises there.
WidmarkRob wrote:
z = --z + x;
This is different, a pre-decrement as it is called. The value of a pre-decrement is the new value, ie after the decrement.

ps: most of this applies to most C-derived languages, except that C# is a bit special in that it specifies that the side-effects happen from left to right. That only matters if you decrement or increment something, and then use that same thing again in the same expression (ok not really, it's more complex, but it's True Enough)

modified 28-Apr-13 7:54am.

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

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.