Assignment Operators
The assignment operator = is used to assign a new value to a variable declared with var. Unlike many languages, the type of an assignment expression in Cangjie is Unit, and its value is (). This means assignment expressions cannot be used as conditions, and the compiler helps you avoid the common mistake of writing = instead of ==.
Cangjie supports compound assignment operators that combine arithmetic operations with assignment: +=, -=, *=, /=, %=, **=, etc. For example, score += 5 is equivalent to score = score + 5, but more concise. Compound assignment operators require both operands to be of the same type.
Cangjie also supports multi-assignment expressions, allowing you to assign tuple values to multiple variables at once. For example, (a, b) = (b, a) can conveniently swap the values of two variables.
The code on the right demonstrates basic usage of assignment operators and the convenience of compound assignment operators. Try rewriting score += 5 in full form to appreciate the equivalence of both styles.
Run to see the output and observe how variable values change during the process.