Click here to Skip to main content
15,918,808 members
Home / Discussions / C#
   

C#

 
GeneralLayout Pin
V.21-Mar-07 2:52
professionalV.21-Mar-07 2:52 
GeneralRe: Layout Pin
Laxman Auti21-Mar-07 3:18
Laxman Auti21-Mar-07 3:18 
GeneralRe: Layout Pin
V.21-Mar-07 4:35
professionalV.21-Mar-07 4:35 
GeneralRe: Layout Pin
Laxman Auti21-Mar-07 5:01
Laxman Auti21-Mar-07 5:01 
Question++x + x and x + ++x [modified] Pin
Varibrus21-Mar-07 2:44
Varibrus21-Mar-07 2:44 
AnswerRe: ++x + x and x + ++x Pin
Stefan Troschuetz21-Mar-07 3:04
Stefan Troschuetz21-Mar-07 3:04 
AnswerRe: ++x + x and x + ++x Pin
Pete O'Hanlon21-Mar-07 3:08
mvePete O'Hanlon21-Mar-07 3:08 
AnswerRe: ++x + x and x + ++x [modified] Pin
Jimmanuel21-Mar-07 3:08
Jimmanuel21-Mar-07 3:08 
For ++x + x, because of the location of the pre-increment x is pre-incremented before it's value is substituted in the in the equation - so the expression effectively evaluates to z = 2 + 2.
For x + ++x the value of x is substituted as the first operand in the equation before it is modified, then it is pre-incremented and substituted as the second operand in the expression. This makes the expression evaluate to z1 = 1 + 2. At least that appears to be what's going on . . .


-- modified at 10:39 Wednesday 21st March, 2007


The compiler/run time environment/whatever has to substitute the value of x into both expressions twice. The first expression is evaluated as such:
z = ++x + x;       // x equals  1
z = (x = x+1) + x; // x still equals 1
z = 2 + x;         // x now equals 2
z = 2 + 2;
z = 4;

Whereas the second expression is evaluated like this:
z1 = x + ++x;       // x equals 1
z1 = 1 + ++x;       // x still equals 1
z1 = 1 + (x = x+1); // x still equals 1
z1 = 1 + 2;         // now x equals 2
z1 = 3;

In both expressions the pre-increment is performed before the addition, keeping the order of operation correct, but in the first expression the pre-increment happens before the value of x is substituted for the 2nd operand in the addition.

In C++ the compiler will examine the entire expression and evaluate any pre-increments before it does any value substitutions (apparently - this probably depends on what optimizations/type modifiers you specify), so it isn't that C++ doesn't have this problem, it's just that the code is being interpreted by different compilers and therefore will behave differently.
AnswerRe: ++x + x and x + ++x Pin
Laxman Auti21-Mar-07 3:32
Laxman Auti21-Mar-07 3:32 
QuestionMain form with always-on-top flag + dialog Pin
Dominik Reichl21-Mar-07 2:36
Dominik Reichl21-Mar-07 2:36 
AnswerRe: Main form with always-on-top flag + dialog Pin
Stefan Troschuetz21-Mar-07 2:45
Stefan Troschuetz21-Mar-07 2:45 
GeneralRe: Main form with always-on-top flag + dialog Pin
Dominik Reichl21-Mar-07 4:13
Dominik Reichl21-Mar-07 4:13 
GeneralRe: Main form with always-on-top flag + dialog Pin
Stefan Troschuetz21-Mar-07 4:31
Stefan Troschuetz21-Mar-07 4:31 
QuestionProgressive Disclosure Control in windows form using c# Pin
Kumar Subramanian21-Mar-07 2:03
Kumar Subramanian21-Mar-07 2:03 
AnswerRe: Progressive Disclosure Control in windows form using c# Pin
joon vh.21-Mar-07 5:11
joon vh.21-Mar-07 5:11 
QuestionConstructor is instance method? Pin
Russell Jones21-Mar-07 2:00
Russell Jones21-Mar-07 2:00 
AnswerRe: Constructor is instance method? Pin
Pete O'Hanlon21-Mar-07 3:09
mvePete O'Hanlon21-Mar-07 3:09 
GeneralRe: Constructor is instance method? Pin
Russell Jones21-Mar-07 3:19
Russell Jones21-Mar-07 3:19 
GeneralRe: Constructor is instance method? Pin
Pete O'Hanlon21-Mar-07 3:31
mvePete O'Hanlon21-Mar-07 3:31 
GeneralRe: Constructor is instance method? Pin
Russell Jones21-Mar-07 3:45
Russell Jones21-Mar-07 3:45 
GeneralRe: Constructor is instance method? Pin
Pete O'Hanlon21-Mar-07 3:49
mvePete O'Hanlon21-Mar-07 3:49 
Questionevents and remoting Pin
D2raghu21-Mar-07 1:46
D2raghu21-Mar-07 1:46 
AnswerRe: events and remoting Pin
KaineDunno21-Mar-07 2:10
KaineDunno21-Mar-07 2:10 
AnswerRe: events and remoting Pin
m@u21-Mar-07 2:34
m@u21-Mar-07 2:34 
QuestionXML element, node .. whaa!! Pin
Michael Hendrickx21-Mar-07 1:33
Michael Hendrickx21-Mar-07 1:33 

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.