Completion requirements
View
2. What is an Operator?
2.1. Arithmetic Operators
Suppose you have variables a = 20 and b = 30. You can apply the following operations
- Addition (+): Adds two operands Ex. a + b will result to 50.
- Subtraction (-): Subtracts the second operand from the first Ex. a – b will result to -10.
- Multiplication (*): Multiply both operands Ex. a * b will result to 600.
- Division (/): Divide the numerator by the denominator Ex. b / a will result to 1.5.
- Modulus (%): Outputs the remainder of an integer division Ex. a % b will result to 20.
- Increment (++): Adds one to its operand. Ex. a++ will result to 21.
- Decrement (--): Subtracts the second operand from the first Ex. a-- will result to 19.
For incrementing and decrementing, if a prefix operator (++x or --x) is used, it will return the
value of the operand after adding one. Meanwhile, the postfix operator (x++ or x--) will return the
value of the operand before adding one. For example, if x = 5, then ++x sets x to 6 and returns 6,
while x++ returns 5 and, only then, sets x to 6.
Sample code:
Output: