Assignments

Multiple Assignment

A variable can be assigned value multiple times.

Let's look at this example:

Output:

In this case num has been assigned value multiple times.

Variables can be assigned values of other variables (line 16).

Also a variable can receive a new value by using it's current value (line 17).

Assignment operators

They assign values and make the code shorter.

+= operator

num += 2 is the same as num = num + 2.

Example:

Output:

-= operator

num -= 5 is the same as num = num - 5.

Example:

Output:

*= operator

num *= 7 is the same as num = num * 7.

Example:

Output:

/= operator

num /= 10 is the same as num = num / 10.

Example:

Output: