nxa*_*sdf 12
这无疑已经得到了回答,但无论如何......
它们在如何改变价值以及如何返回结果方面存在差异.
前两个+=
和=+
行为的方式,第一个增量的变量,其他组的变量.他们没有关系.请注意以下代码:
// +=
x = 1;
printf( x += 1 ); // outputs 2, the same as x = x+1
printf( x ); // outputs 2
// =+
x = 1;
printf( x =+ 1 ); // outputs 1, the same as x = 1;
printf( x ); // outputs 1
Run Code Online (Sandbox Code Playgroud)
接下来的两个,++x
并且x++
,它们的功能顺序不同.++x
将您的变量增加1并返回结果.x++
将返回结果并递增1.
// ++x
x = 1;
printf( ++x ); // outputs 2, the same as x = x+1
printf( x ); // outputs 2
// x++
x = 1;
printf( x++ ); // outputs 1
printf( x ); // outputs 2
Run Code Online (Sandbox Code Playgroud)
它们主要用于for
循环和while
循环.
在速度方面,++x
被认为比快了很多x++
,因为x++
需要建立一个内部的临时变量来存储值,增加主变,但返回临时变量,基本上都是使用更多的操作.我在很久以前就学到了这一点,我不知道它是否仍然适用