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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With