Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does ++ not produce the same results as +1?

The following two C# code snippets produce different results (assuming the variable level is used both before and after the recursive call). Why?

public DoStuff(int level) {   // ...   DoStuff(level++);   // ... } 

,

public DoStuff(int level) {   // ...   DoStuff(level+1);   // ... } 

After reading some of the responses below I thought it would be worthwhile posting the stack traces for level++, ++level and level+1 to highlight how deceiving this problem is.

I've simplified them for this post. The recursive call sequence starts with DoStuff(1).

// level++

DoStuff(int level = 1) DoStuff(int level = 2) DoStuff(int level = 2) DoStuff(int level = 2) 

// ++level

DoStuff(int level = 4) DoStuff(int level = 4) DoStuff(int level = 3) DoStuff(int level = 2) 

// level+1

DoStuff(int level = 4) DoStuff(int level = 3) DoStuff(int level = 2) DoStuff(int level = 1) 
like image 735
Richard Dorman Avatar asked Sep 22 '08 11:09

Richard Dorman


People also ask

When a test can be repeated with the same results that is called?

Test-retest reliability is a measure of reliability obtained by administering the same test twice over a period of time to a group of individuals. The scores from Time 1 and Time 2 can then be correlated in order to evaluate the test for stability over time.

What is an example of test-retest reliability?

For example, a group of respondents is tested for IQ scores: each respondent is tested twice - the two tests are, say, a month apart. Then, the correlation coefficient between two sets of IQ-scores is a reasonable measure of the test-retest reliability of this test.

How do you determine validity and reliability?

How are reliability and validity assessed? Reliability can be estimated by comparing different versions of the same measurement. Validity is harder to assess, but it can be estimated by comparing the results to other relevant data or theory.


1 Answers

To clarify all the other responses:

+++++++++++++++++++++

DoStuff(a++); 

Is equivalent to:

DoStuff(a); a = a + 1; 

+++++++++++++++++++++

DoStuff(++a); 

Is equivalent to:

a = a + 1; DoStuff(a); 

+++++++++++++++++++++

DoStuff(a + 1); 

Is equivalent to:

b = a + 1; DoStuff(b); 

+++++++++++++++++++++

like image 165
Matt Avatar answered Oct 02 '22 15:10

Matt