+ 6
What is the difference between --y and y--
What is the solution of this 🤔 int x =15 ; int y =5; System.out.println (x++ % --y );
11 ответов
+ 11
--y means decreasing y before using, y-- means decreasing y after using. Example :
int x=5;
int y=5;
System.out.print(--x +" " + y--);
writes 4 5
So int x=15; int y=5;
System.out.println(x++ % --y)
calculates 15%4=3
writes 3.
+ 5
15 % 4 = 3
+ 3
15 % 4 = 3
This is prefix and postfix operators.
+ 3
Judging by the fact that you have successfully completed the study of three programming languages, I thought that you should be well aware of this topic and therefore did not comment on your answer in detail, but since you re-ask for my clarifications, here is a link to a previously asked question, where is my answer:☺
https://www.sololearn.com/Discuss/1805551/?ref=app
https://www.sololearn.com/learn/Java/2141/
+ 3
--y : y is decremented,then assigned back to y
y-- : y is assigned first,then it is decremented
In the line of code,
At x++, x is assigned 15,then only it is incremented.
At --y, y is decremented,and y is assigned 4.
So 15%4 =3.
+ 2
Out 3
+ 2
How ?
Vasiliy
+ 2
This mean x++ return the same value but if it's ++×
It return (16)
Right Alex Novikov
+ 2
This may help you..
https://www.sololearn.com/discuss/1690694/?ref=app
+ 2
The prefix increment or decrement makes the action first like in --y means you reduce the value by one then assign it to the variable y, but the opposite is in x++ it assigns the value of x and after that increments it by one.
+ 1
But it's not increment'd if its x++ % --y
Whoever created this code probably created common core math. Makes absolutely no sense for x=15 + 1 but the 1 disappears unless it's 1 + x