24

This is a simple while loop in C# but it is working infinitely.

int count = 1;
while (count < 10)
{
    count = count++;
}

Why is this so?

Elmo
  • 6,409
  • 16
  • 72
  • 140
  • 4
    ++count will give the the desired action. – Jordan Nov 28 '11 at 16:21
  • 4
    in my opinion this question does not deserve upVotes, just my idea, the code in the question shows not understanding of ++ post operator... – Davide Piras Nov 28 '11 at 16:22
  • 16
    tip: try debugging it... – Ali Hasan Nov 28 '11 at 16:23
  • 4
    @Jordan: If he replaces the line by `count = ++count;`, as you suggested, the code will work, but it will *still be broken*. – Heinzi Nov 28 '11 at 16:24
  • 1
    I concur with @AI25. Debugging is something you should be doing prior to asking others for help. Perhaps you could have asked, "How might I debug this code?" You could attempt to view what the value of `count` is, I'm certain this will help you narrow down what you're doing. Or even open up a console in firebug and try it out with JavaScript. – Tass Nov 29 '11 at 17:30
  • 3
    This assign (`count = count++;`) makes no sense. Write clean code and it will work: `count++;` – Pavel Hodek Nov 30 '11 at 09:45
  • Are you serious? How did this question get so many upvotes? Sometimes stackoverflow does my head in. – Maxim Gershkovich Dec 13 '11 at 07:08
  • What a ludicrous amount of voting relative to more useful posts... – Ruben Bartelink Jan 05 '12 at 14:39

8 Answers8

58

The expression count++ returns the original value of count, then increments the value afterwards.

So you are overwriting count with the same value every time. Just do this:

count++;

For the curious, here's a link to Eric Lippert's article which distinguishes between operator precedence and the order of evaluation -- it's an interesting read:

http://blogs.msdn.com/b/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx

JohnD
  • 14,327
  • 4
  • 40
  • 53
  • 1
    +1 for both explaining why `count = count++;` causes a problem and that the solution is simple `count++` without assignment. – Fenton Nov 28 '11 at 16:22
  • 6
    Sorry, but this has nothing to do with operator precedence. In addition, saying "afterwards" is not accurate: The value is incremented and *then* a value is returned, it just happens to be that the *old* value is returned, see [this answer by Eric Lippert](http://stackoverflow.com/questions/3346450/c-what-is-the-difference-between-i-and-i/3346729#3346729) for details. – Heinzi Nov 28 '11 at 16:28
  • Good point, I removed the mention of operator precedence. And you get an upvote from me. :) – JohnD Nov 28 '11 at 16:29
35

This will loop infinitely.

There are two types of incrementing a variable:

Here count++ and ++count both are different if you have used ++count it will work.

Here count = count++ means count variable will be incremented by one then assigns the earlier value 1 to the count variable itself so count remains unchanged.

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
Nighil
  • 4,099
  • 7
  • 30
  • 56
27

count = count++; does not increment count by one. x++ is the post increment operator, which means that the value returned by the expression is the old value. Thus, in your code, the following happens:

int oldValue = count;
count = count + 1;
count = oldValue;

What you probably meant to write was count++; (without the "count =").

More details about this can be found in the following SO question:

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
7

The ++ operator first saves the current value then increments and finally returns the saved value, so count will never change.

Eiter use the ++ operator or do an assignment. These are all equivalent:

count++;
count += 1;
count = count + 1;
Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • 4
    That is not what the ++ operator does. It first *saves the current value*, then it increments, then it returns the saved value. It does not return the value *before* it does the increment. – Eric Lippert Nov 28 '11 at 17:11
  • @EricLippert: Why it returns the saved value? It incremented the value then it should return the incremented value, right? Also, does count++ will return count? How? – RG-3 Nov 29 '11 at 17:46
  • 1
    @Gator: The post-increment operator increments the variable and returns the non-incremented value. The pre-increment operator increments the variable and returns the incremented value. – Eric Lippert Nov 29 '11 at 17:53
  • @Gator: See http://stackoverflow.com/questions/3346450/c-what-is-the-difference-between-i-and-i/3346729#3346729 for more details. – Eric Lippert Nov 29 '11 at 17:57
5
count  = count++;

This is a post-increment. It does the following.

int temp = count;
count++;
count = temp;

So you're not incrementing count. Use the following instead:

while (count  < 10)
{
    ++count;
}
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Here you said that count++. And then you are saying that its not incrementing count??? Isnt count++ is equal to count = count + 1 ? You should saying we are not incrementing temp! – RG-3 Nov 29 '11 at 17:42
1

because

count++

returns count, not count + 1

just have count++ with no assignment or:

count = ++count;

the last one only to explain but you should not use it...

from: ++ Operator (C# Reference)

The first form is a prefix increment operation. The result of the operation is the value of the operand after it has been incremented.

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

Numeric and enumeration types have predefined increment operators. User-defined types can overload the ++ operator. Operations on integral types are generally allowed on enumeration.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • Why count++ will return count? I think count++ is equal to count = count + 1? – RG-3 Nov 29 '11 at 17:40
  • @Gator count++ returns count if you assign it, while ++count return (count + 1). Check the difference between ++i and i++: http://msdn.microsoft.com/en-us/library/36x43w8w.aspx – Davide Piras Nov 29 '11 at 18:21
0

It is infinite because you aren't actually incrementing count.

count = count++; assigns the value of 1 to count and then increments count but since you don't assign the incremented value count never increases.

You need to do either:

count++;

or

count = ++count;
0

Let me ask you a question why do you make two operations on a single variable while one is enough? what was your intention? count++ itself was enough so why again assign to count. May be you want to do something else. You could have only count++, or ++count or count+1. I think other ways causes two operations. Sorry for my way of writing.

Mubarek
  • 2,691
  • 1
  • 15
  • 24