Operators =========== Operators in programming languages are symbols that tells the compiler or interpreter to perform specific operations. The operand combined with the operator makes an operation. In an operation, an *operand* is the data that is being operated on. The position of the operator, with respect to its operands, may be prefix, infix or postfix. Common simple operators include - *arithmetic* (e.g. addition with +), - *comparison* (relational) (e.g. "greater than" with >), - *logical* (e.g. AND, also written && in some languages), and - *assignment* (e.g., =, +=) operators. Additional types of operators include assignment (usually = or :=), field access in a record or object (usually .), and bitwise and shift operators. Operators can be categorized based on the number of operands required. The three categories of operators based on the number of operands they require are: - Unary operators: which require one operand (e.g., ++, !) - Binary operators: which require two operands (e.g., +) - Ternary operators: which require three operands (e.g., condition ? consequent : alternative) .. index:: arithmetic operators .. _arithmetic-operators: Arithmetic Operators --------------------- .. list-table:: Arithmetic Operators :widths: 20 20 40 20 :header-rows: 1 * - Operator - Name - Description - Example * - ``+`` - Addition - Adds together two values - x + y * - ``-`` - Subtraction - Subtracts one value from another - x - y * - ``*`` - Multiplication - Multiplies two values - x * y * - ``/`` - Division - Divides one value by another - x / y * - ``%`` - Modulus - Returns the division remainder - x % y * - ``++`` - Increment - Increases the value of a variable by 1 - x++ * - ``--`` - Decrement - Decreases the value of a variable by 1 - x-- Examples of arithmetic operators: .. code-block:: > x = 1; > x++ // post-increment operator; x++ increments the value of variable x after it's evaluated in an expression 1 > x 2 > x = 2; > ++x // pre-increment operator; ++x increments the value of variable x before it's evaluated in an expression 3 Comparison Operators --------------------- Comparison operators, also known as relational operators, compare their operands (numbers or strings) and return a value of either ``True`` (1) or ``False`` (0). These two values are known as Boolean values and are used in conditional statements for decision making. .. list-table:: Comparison Operators :widths: 20 20 60 :header-rows: 1 * - Operator - Name - Example * - ``==`` - Equal to - x == y * - ``!=`` - Not equal to - x != y * - ``>`` - Greater than - x > y * - ``<`` - Less than - x < y * - ``>=`` - Greater than or equal to - x >= y * - ``<=`` - Less than or equal to - x <= y Examples of comparison operations: .. code-block:: > int x = 1, y = 2; > x == y false > x < y true > x <= y true > .. _logical-operators: Logical Operators --------------------------- Logical Boolean operators [#_logical-operators]]_ are used to determine the logic between variables or values. Just like comparison operators, logical operators return True or False as a result of the evaluation. .. list-table:: Logical Operators :widths: 20 20 40 20 :header-rows: 1 * - Operator - Name - Description - Example * - ``!`` (unary) - Logical negation operator - Returns logical negation of its operand - !true // False * - ``&&`` - Logical and - Returns True if both statements are true - x < 5 && x < 10 * - ``||`` - Logical or - Returns True if one of the statements is true - x < 5 || x < 4 Examples of logical operators are: .. code-block:: > int x = 2; > x < 5 && x < 10 true > x < 5 || x < 10 true > !(x < 5 && x < 10) false .. _assignment-operators: Assignment Operators ---------------------- The ``=`` operator is called the simple *assignment operator*, which serves to assign a value to a variable (note that ``==`` is the *equality test operator* testing if two operands are equal). In addition to simple assignment operator, compound assignment operators are shortcuts that do a *math operation* and *assignment* in one step. [#assign_op]_ : .. list-table:: Assignment Operators :widths: 2 3 3 3 5 50 :header-rows: 1 * - No. - Operator - Example - Same As - Output of x == 5 - Description * - 1 - = - x = 5 - x = 5 - 5 - Simple assignment operator; assigns values from right operands to left operand * - 2 - += - x += 3 - x = x + 3 - 8 - Add AND assignment operator; adds right operand to the left operand and assign the result to left operand * - 3 - -= - x -= 3 - x = x - 3 - 2 - Subtract AND assignment operator; subtracts right operand from the left operand and assign the result to left operand * - 4 - \*= - x \*= 3 - x = x * 3 - 15 - Multiply AND assignment operator; multiplies right operand with the left operand and assign the result to left operand * - 5 - /= - x /= 3 - x = x / 3 - 1.6666666666666667 - Divide AND assignment operator; divides left operand with the right operand and assign the result to left operand * - 6 - %= - x %= 3 - x = x % 3 - 2 - Modulus AND assignment operator; takes modulus using two operands and assign the result to left operand * - 7 - &= - x &= 3 - x = x & 3 - 1 - Bitwise AND assignment operator ([#bitwise_and]_) * - 8 - \|= - x \|= 3 - x = x | 3 - - Bitwise inclusive OR and assignment operator * - 9 - ^= - x ^= 3 - x = x ^ 3 - - Bitwise exclusive OR and assignment operator * - 10 - >>= - x >>= 3 - x = x >> 3 - - Right shift AND assignment operator * - 11 - <<= - x <<= 3 - x = x << 3 - - Left shift AND assignment operator * - 12 - => - - - - Lambda operator .. rubric:: Footnotes .. [#logical_op] In C# Reference, ``&&`` is called conditional logical AND operator, which is almost the same as & except it performs "short-circuit evaluation": if the result can be inferred from the first operand, then the second operand is not evaluated, while logical AND operator ``&`` will always evaluate both its operands. The same applies to Logical OR operator ``|``. Also, C# has logical exclusive OR operator ``^``, which computes the same result as the inequality operator ``!=`` for bool operands. .. [#assign_op] This table is adapted from `w3schools ` and `tutorialspoint ` .. [#bitwise_and] `Bitwise and shift operators` .. // Operator Precedence .. // Truth Tables