对变量的赋值没有影响?

时间:2022-08-02 18:11:31

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 ?

当我这样做时:count = ++计数;为什么我得到警告-变量计数的赋值没有影响?这意味着计数是递增的然后分配给它自己或者其他的东西?是否与++计数相同?在count = count++中发生了什么?吗?为什么不给我一个警告呢?

4 个解决方案

#1


13  

count++ and ++count are both short for count=count+1. The assignment is built in, so there's no point to assigning it again. The difference between count++ (also knows 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.

count++ ++ +count =count+1。作业是内置的,所以没有必要再分配。count++(也称为后缀)和++count(也称为前缀)的区别在于,在其余的行之前,++计数将发生,而count++会发生在其余的行之后。

If you were to take apart count=count++, you would end up with this:

如果你拆开count=count++,你就会得到这样的结果:

    count = count;
    count = count+1;

Now you can see why postfix won't give you a warning: something is actually being changed at the end.

现在您可以看到为什么postfix不会给您一个警告:实际上,在最后会发生一些变化。

If you take apart count=++count, you would end up with this:

如果你拆开count=++count,你就会得到这样的结果:

    count = count+1;
    count = count;

As you can see, the second line of code is useless, and that's why the compiler is warning you.

如您所见,第二行代码是无用的,这就是编译器警告您的原因。

#2


3  

Breaking the statement up you are basically writing:

打破陈述句你基本上是在写:

++count;
count = count;

As you can see count=count does nothing, hence the warning.

您可以看到count=count不做任何事情,因此警告。

#3


3  

the ++ operator is a shortcut for the following count = count + 1. If we break your line count = ++count it responds to count = count+1 = count

++运算符是下面count = count + 1的快捷方式。如果我们中断了您的行计数= ++计数,则计数= count+1 = count。

#4


3  

To expand a little, count++ is postfix. It takes place after other operations so if you did something like

要扩展一点,count++是后缀。它发生在其他操作之后,如果你做了类似的事情。

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

a would be 0, b would be 1. However, ++count is prefix if you did

a是0 b是1。但是,如果你做了,那么++计数就是前缀。

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

then a and b would both be 1. If you just do

然后a和b都是1。如果你只是做

count++;

or

++count;

then it doesn't matter, but if you are combining it with something else, it will

那没关系,但如果你把它和别的东西结合起来,它就会。

#1


13  

count++ and ++count are both short for count=count+1. The assignment is built in, so there's no point to assigning it again. The difference between count++ (also knows 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.

count++ ++ +count =count+1。作业是内置的,所以没有必要再分配。count++(也称为后缀)和++count(也称为前缀)的区别在于,在其余的行之前,++计数将发生,而count++会发生在其余的行之后。

If you were to take apart count=count++, you would end up with this:

如果你拆开count=count++,你就会得到这样的结果:

    count = count;
    count = count+1;

Now you can see why postfix won't give you a warning: something is actually being changed at the end.

现在您可以看到为什么postfix不会给您一个警告:实际上,在最后会发生一些变化。

If you take apart count=++count, you would end up with this:

如果你拆开count=++count,你就会得到这样的结果:

    count = count+1;
    count = count;

As you can see, the second line of code is useless, and that's why the compiler is warning you.

如您所见,第二行代码是无用的,这就是编译器警告您的原因。

#2


3  

Breaking the statement up you are basically writing:

打破陈述句你基本上是在写:

++count;
count = count;

As you can see count=count does nothing, hence the warning.

您可以看到count=count不做任何事情,因此警告。

#3


3  

the ++ operator is a shortcut for the following count = count + 1. If we break your line count = ++count it responds to count = count+1 = count

++运算符是下面count = count + 1的快捷方式。如果我们中断了您的行计数= ++计数,则计数= count+1 = count。

#4


3  

To expand a little, count++ is postfix. It takes place after other operations so if you did something like

要扩展一点,count++是后缀。它发生在其他操作之后,如果你做了类似的事情。

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

a would be 0, b would be 1. However, ++count is prefix if you did

a是0 b是1。但是,如果你做了,那么++计数就是前缀。

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

then a and b would both be 1. If you just do

然后a和b都是1。如果你只是做

count++;

or

++count;

then it doesn't matter, but if you are combining it with something else, it will

那没关系,但如果你把它和别的东西结合起来,它就会。