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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4104944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:40:02  来源:igfitidea点击:

what does x-- or x++ do here?

java

提问by Nana

it is a silly Q for most of u - i know - but i one of the beginner here, and I can not understand why the output in here are 12 what does this (x--) do to the result ?

对于大多数人来说,这是一个愚蠢的 Q - 我知道 - 但我是这里的初学者之一,我不明白为什么这里的输出是 12 这 ( x--) 对结果有什么影响?

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

采纳答案by Jaydee

Just a cautionary note, sometimes the pre and post increment operators can have unexpected results

只是一个警告,有时前后增量运算符可能会产生意想不到的结果

Why does this go into an infinite loop?

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

and what does:

什么是:

x[i]=i++ + 1;

do

Read here: http://www.angelikalanger.com/Articles/VSJ/SequencePoints/SequencePoints.html

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

回答by NimChimpsky

x--subtracts/decrements the value of x by one.

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

Conversley x++adds/increments by one.

Conversleyx++加一/递增。

The plus or minus signs can either be before (--x) or after (x--) the variable name, prefixand postfix. If used in a expression the prefix will return the value after operation has been performed and the postfix will return the value before operation has been performed.

加号或减号可以在 ( --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

回答by darioo

x--will decrement value of xby 1. It is a postfix decrement operator, --xis a prefix decrement operator.

x--将值减x1。它是一个后缀自减运算符,--x是一个前缀自减运算符。

So, what's going on here?

那么,这里发生了什么?

 
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

By analogy, the ++will increase a value by 1. It also has a prefix and postfix variant.

以此类推,the++会将值增加 1。它也有前缀和后缀变体。

回答by Nicolas Repiquet

x++ increments x after x being evaluated. ++x increments x before x being evaluated.

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

The same goes for --

同样适用于——

回答by pyCoder

Example:

例子:

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

Here y = 6 (--x reduce x by 1)

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

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

Here y = 6 (x-- use first the value of x in the expression and then reduce x by 1)

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

回答by Buhake Sindi

x++is essentially x = x + 1(the same applies for ++x). xis incremented by 1.

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

x--is essentially x = x - 1(the same applies for --x). xis decremented by 1.

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

The difference is that how x++and ++xis used in the statement/expression: In ++x, xis incremented by 1 firstbefore being used while in x++, xis used (before incrementation) firstand once it's used, it gets incremented by 1.

不同的是,如何x++++x在语句/表达被用于:在++xx递增1第一,同时在被使用之前x++x使用(增量之前)第一和一旦它的使用,它就会被加1。

回答by Powertieke

--is the 'decrement' operator. It simply means that the variable it operates on (in this case the xvariable) gets is decremented by 1.

--是“递减”运算符。它只是意味着它所操作的变量(在本例中为x变量)减 1。

Basically it is shorthand for :

基本上它是以下的简写:

x = x - 1;

So what the code does :

那么代码的作用是:

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).