Lesson 12: Assignment and Increment/Decrement Operators
Simple Assignment Operator (=)
Used to assign the value of the right operand to the variable on the left.
c int a = 10; int b = a; // b is now 10
Compound Assignment Operators
These provide a shorthand way to perform an operation and assign the result back to the original variable.
| Operator | Equivalent to | Example |
|---|---|---|
+= | a = a + b | a += 5; |
-= | a = a - b | a -= 2; |
*= | a = a * b | a *= 3; |
/= | a = a / b | a /= 4; |
%= | a = a % b | a %= 7; |
Increment and Decrement Operators
These operators increase or decrease the value of a variable by exactly 1.
1. Postfix (i++, i--)
Increments/decrements the variable after its value has been used in the current expression.
c int i = 5; int j = i++; // j gets 5, then i becomes 6
2. Prefix (++i, --i)
Increments/decrements the variable before its value is used in the current expression.
c int k = 5; int l = ++k; // k becomes 6, then l gets 6
Note: If the operator is used on its own (e.g., i++;), the prefix and postfix forms are functionally identical.