Compound Assignment Operators: 1.5.0

  • Shortcuts to do a math operation in one step
    • x += 1 adds 1 to x and assigns it to x
      • Same as x = x + 1
+ shortcuts - shortcuts * shortcuts / shortcuts % shortcuts
x = x + 1; x = x - 1; x = x * 1; x = x / 1; x = x % 1;
x += 1; x -= 1; x *= 1; x /= 1; x %= 1;
x++; x--;      
  • Most common shortcut operator is ++
    • increment operator
    • adds 1 to the current value
      • Same as x = x + 1
  • ++x can be used to increment x before assigning something to it
    • int y = ++x;
    • Otherwise works identically
    • int y = x++; sets y to the old value of x, then increments x
    • This difference is not on the AP Exam

Code Tracing Challenge: 1.5.1

No real notes about the lesson here, just manually debug 1.5.1.

From personal experience, using a whiteboard can be really helpful in code tracing, as you can more easily manipulate and erase your work.

Summary: 1.5.2

  • Compound assignment operators can be used in place of the assignment operator
    • +=, -=, *=, /=, %=
  • The increment and decrement operator can be used to add or subtract 1 from a variable, then store the new result
    • ++ and --

Closing thoughts

None really; this was a very short section.