Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 ?

int x, y;
x = 7;
x-- ;
y = x * 2;
x = 3;
like image 991
Nana Avatar asked Mar 10 '26 03:03

Nana


1 Answers

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

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.

like image 77
darioo Avatar answered Mar 12 '26 17:03

darioo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!