+ 8

why does not the variable b increase even within the parentheses?

why does not the variable b increase even within the parentheses? and with increment operator before the variable. public class Var { public static void main(String[] args) { int a = 21; int b = 8; System.out.print((a/++b)%3); } } the result is 2

17th Oct 2017, 3:46 PM
Malkon F
Malkon F - avatar
3 Answers
+ 2
That will print 2 because of operators priority. First it increments b (so b=9) then it divides a/b (21/9=2) and finally the module (2%3=2).
17th Oct 2017, 3:52 PM
Chriptus13
Chriptus13 - avatar
+ 8
in fact the variable is increased. I was committing on a simple miscalculation. thanks in advanced
17th Oct 2017, 3:56 PM
Malkon F
Malkon F - avatar
+ 1
if you don't want to increment b in first place u you can use b++ that will increment b only in the next code line
17th Oct 2017, 3:55 PM
Chriptus13
Chriptus13 - avatar