Prefix vs postfix increment
Java's arithmetic operators are +, -, *, / and %, plus ++, -- and compound forms like +=. Between integers, / throws away the remainder and % gives it back with the sign of the left operand, and integer division by zero throws an exception while decimal division doesn't.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Given 1234 paise, print it as rupees and paise: 12 rupees 34 paise.
Print the sum of the digits of 9875 using only / and % (no Strings).
Print whether -7, 0 and 12 are odd, using a test that works for negative numbers.
Explain it without notes
What do / and % produce for -7 and 2, and why?
What's the difference between i++ and ++i, and why does i = i++ leave i unchanged?
Why does 10 / 0 throw an exception while 10.0 / 0 doesn't?
How would you safely compute the previous index in a circular buffer of size n?
Expected output
a=6 b=5
c=6 d=6
after i = i++, i=10
x=5 y=8