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?
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.
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:
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;
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;
}
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.
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;
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.