Return To CS 110 Home

Colour Theme   Font Size Options
 
   
   
   
Compound Assignment

Compound Assignment


Video Summary: https://youtu.be/K79LQ35ATRA


In programming, you will often find yourself incrementing or updating variables dependent on their original value, such as "num = num * num2." Instead of writing this out each time, you can use the following compound assignments:

Assignment Effect
+= x = x + y
-= x = x - y
*= x = x * y
/= x = x / y
%= x = x % y

Exercise

Consider the following program and output:



Hello World
55
5

Change the expressions to use compound assignments. The output should remain the same as when you started.

Solution