When I do this:
count = ++count;
Why do i get the warning – The assignment to variable count has no effect ?
This means that count is incremented and then assigned to itself or something else ?
Is it the same as just ++count ?
What happens in count = count++; ? Why don’t I get a warning for this ?
When I do this: count = ++count; Why do i get the warning –
Share
count++and++countare both short forcount=count+1. The assignment is built in, so there’s no point to assigning it again. The difference betweencount++(also knows as postfix) and++count(also known as prefix) is that++countwill happen before the rest of the line, andcount++will happen after the rest of the line.If you were to take apart
count=count++, you would end up with this:Now you can see why postfix won’t give you a warning: something is actually being changed at the end.
If you take apart
count=++count, you would end up with this:As you can see, the second line of code is useless, and that’s why the compiler is warning you.