Does the assignment of a variable not work?

When I do this: count = ++ count; Why do I get a warning - assigning a variable to an account does not affect? Does this mean that the score is increased and then assigned to itself or something else? Is it the same as just a ++ counter? What happens in count = count ++ ;? Why am I not warning about this?

+6
source share
4 answers

count++ and ++count are short for count=count+1 . The assignment is built-in, so it makes no sense to assign it again. The difference between count++ (also known as postfix) and ++count (also known as prefix) is that ++count will happen before the rest of the line, and count++ will happen after the rest of the line.

If you needed to separate count=count++ , you get the following:

  count = count; count = count+1; 

Now you can understand why postfix will not give you a warning: in the end, something is really changing.

If you split count=++count , you get the following:

  count = count+1; count = count; 

As you can see, the second line of code is useless and why the compiler warns you.

+13
source

Violating the statement, you basically write:

 ++count; count = count; 

As you can see, count = count does nothing, so a warning.

+3
source

the ++ operator is a shortcut to the following count = count + 1 . If we break your line count = ++count , it will respond to count = count+1 = count

+3
source

To expand a bit, count ++ is postfix. This happens after other operations, so if you did something like

 int a = 0, b = 0; a = b++; 

a will be 0, b will be 1. However, the ++ counter is a prefix if you did

 int a = 0, b = 0; a = ++b; 

then a and b will be equal to 1. If you just do

 count++; 

or

 ++count; 

then it doesn’t matter, but if you combine it with something else, it will be

+3
source

Source: https://habr.com/ru/post/921243/


All Articles