← Back to Midterms

Increment & Decrement

Explanation & Learnings

I wasn't able to take this seatwork but the topic was about increment and decrement operators in Java. The problems given can get really complex when there are a lot of incrementing involved. Since I have studied this, below is an example of how these operators work.

Example.java
int a = 12, b = 10, c = 8;

// a uses 12 then becomes 13, b uses 10 then becomes 11
a++ + b++;  // 12 + 10 = 22

// b uses 11 then becomes 12, c decreases to 7 and is used
b++ - --c;  // 11 - 7 = 4

// a increases to 14 and is used, c decreases to 6 and is used
++a + --c;  // 14 + 6 = 20

// b increases to 13 and is used, a increases to 15 and is used
++b + ++a;  // 13 + 15 = 28


It was interesting to see how the order of operations can lead to different results, and it reinforced the importance of understanding operator precedence in programming as this can affect your outputs.