x-或x ++在这里做什么?

对于大多数人来说,这是一个愚蠢的问题 – 我知道 – 但我是这里的初学者之一,我无法理解为什么这里的输出是12( x-- )对结果做了什么?

 int x, y; x = 7; x-- ; y = x * 2; x = 3; 

只是一个注意事项,有时前后增量运算符可能会有意想不到的结果

为什么这会进入无限循环?

什么做的:

 x[i]=i++ + 1; 

在这里阅读: http : //www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

x--x的值减1.它是后缀减量运算符, – x是前缀减量运算符。

那么,这里发生了什么?

 int x, y; //initialize x and y x = 7; //set x to value 7 x--; //x is decremented by 1, so it becomes 6 y = x * 2; //y becomes 6*2, therefore y becomes 12 x = 3; //x becomes 3 

int x, y; //initialize x and y x = 7; //set x to value 7 x--; //x is decremented by 1, so it becomes 6 y = x * 2; //y becomes 6*2, therefore y becomes 12 x = 3; //x becomes 3

通过类比, ++会将值增加1.它还有一个前缀和后缀变体。

x--将x的值减1 /减1。

Conversley x++增加/增加一个。

加号或减号可以在变量名, 前缀后缀之前( – x-- )或之后( x-- )。 如果在表达式中使用,则前缀将在执行操作后返回值,后缀将在执行操作之前返回值。

 int x = 0; int y = 0; y = ++x; // y=1, x=1 int x = 0; int y = 0; y = x++;// y=0, x=1 

--是’减量’运算符。 它只是意味着它操作的变量(在这种情况下是x变量)得到的减1。

基本上它是简写:

 x = x - 1; 

那么代码的作用是什么:

 int x,y ; # Define two variables that will hold an integer x=7; # Set variable X to value 7 x-- ; # Decrement x by one : so x equals 7 - 1 = 6 y= x * 2; # Multiply x by two and set the result to the y variable: 6 times 2 equals 12 x=3; # set x to value 3 (I do not know why this is here). 

x被评估后x ++递增x。 ++ x在x被评估之前递增x。

  int x = 0; print(++x); // prints 1 print(x); // prints 1 int y = 0; print(y++); // prints 0 print(y); // prints 1 

同样的 –

例:

 x = 7; y = --x; /* prefix -- */ 

这里y = 6( – x将x减1)

 y = x--; /* postfix -- */ 

这里y = 6(x–首先使用表达式中x的值,然后将x减1)

x++本质上是x = x + 1 (同样适用于++x )。 x增加1。

x--基本上是x = x - 1 (同样适用于--x )。 x减1。

不同之处在于如何在语句/表达式中使用x++++x :在++xx在使用之前首先递增1,而在x++x首先使用(在递增之前),一旦使用,它就会使用增加1。