Topics List
Basic Concepts

Operators
Fundamentals and Functions of Operators

In Lua, operators are symbols or keywords that perform operations on one or more values. Lua supports various types of operators, including arithmetic, relational, logical, and bitwise operators. Here´s a detailed explanation of each type:
  1. Arithmetic Operators: These operators perform basic mathematical operations.
    Addition +: Adds two values together.
    Subtraction -: Subtracts the right operand from the left operand.
    Multiplication *: Multiplies two values.
    Division /: Divides the left operand by the right operand.
    Modulus %: Returns the remainder of the division of the left operand by the right operand.
    Exponentiation ^: Raises the left operand to the power of the right operand.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

  2. Relational Operators: These operators compare two values and return a boolean result (true or false).
    Equal ==: Returns true if the operands are equal.
    Not equal ~= or !=: Returns true if the operands are not equal.
    Greater than >: Returns true if the left operand is greater than the right operand.
    Less than <: Returns true if the left operand is less than the right operand.
    Greater than or equal to >=: Returns true if the left operand is greater than or equal to the right operand.
    Less than or equal to <=: Returns true if the left operand is less than or equal to the right operand.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

  3. Logical Operators: These operators perform logical operations on boolean values.
    Logical AND and: Returns true if both operands are true.
    Logical OR or: Returns true if at least one of the operands is true.
    Logical NOT not: Returns the opposite boolean value of the operand.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

  4. Bitwise Operators: These operators perform operations on the binary representation of integers.
    Bitwise AND &: Performs a bitwise AND operation.
    Bitwise OR |: Performs a bitwise OR operation.
    Bitwise XOR ~: Performs a bitwise XOR (exclusive OR) operation.
    Bitwise NOT ~: Performs a bitwise NOT (one´s complement) operation.
    Left shift <<: Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
    Right shift >>: Shifts the bits of the left operand to the right by the number of positions specified by the right operand.

    Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

  5. These are the main types of operators in Lua. Understanding and using these operators effectively is crucial for writing efficient and correct Lua code.
    Here are three additional examples, each showcasing a different type of operator:
    1. Arithmetic Operators:

      Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

      Explanation: This code calculates the sum, difference, product, quotient, and modulus of two numbers (10 and 5) and then checks if each result meets certain conditions. For example, it checks if the sum is equal to 15 and prints a message if it is.

    2. Relational Operators:

      Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

      Explanation: This code compares two numbers (10 and 5) using relational operators (==0, >, <, >=0, <=) and prints a message based on the comparison result. For example, it checks if x is greater than y and prints a message if it is.

    3. Logical Operators:

      Copy Text (You can Paste it in your code using Ctrl-B o Ctrl-V)

      Explanation: This code combines boolean values (true and false) using logical operators (and, or, not) and prints a message based on the result. For example, it checks if both a and b are true and prints a message if they are.