0
Question in a code with Java
Problem with % in Java I have a problem. I need to anyone explain me how modules works in Java pls. I have the next code: Int a= 12; Int b =(a%10) + 5 * 2 % 3; System.out.println(b); If it's posible step by step much better. THNAKS.
2 Answers
+ 1
Multiplication * and modulo % operator has similar precedence and associativity (left to right). Addition + operator has lower precedence, so here the expression is evaluated as follows
5 * 2 % 3 equals to 10 % 3, which yields 1
Then comes addition. To the left we have
a % 10 which yields 2
To the right we have 1 (result of the first evaluation)
* Conclusion:
(a % 10) + 5 * 2 % 3
(2) + (5 * 2 % 3)
(2) + (1)
This means a value of 3 is assigned to variable <b>
* Java operator precedence & associativity
https://cs-fundamentals.com/java-programming/java-operators-precedence-and-associativity
Hth, cmiiw
0
A lot of thanks!!!!