Basics

Lua Operators

Lua Operators

Lua operators include arithmetic and relational with # for length.

Introduction to Lua Operators

In Lua, operators are special symbols or keywords that allow you to perform operations on variables and values. They can be categorized into several types, including arithmetic, relational, logical, and others. In this guide, we will focus on arithmetic, relational, and the length operator #.

Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations. Here's a list of the basic arithmetic operators available in Lua:

  • + : Addition
  • - : Subtraction
  • * : Multiplication
  • / : Division
  • % : Modulo (remainder of division)
  • ^ : Exponentiation
  • - : Unary minus (negation)

Relational Operators

Relational operators are used to compare two values. They return a boolean value: true or false. Here are the relational operators in Lua:

  • == : Equal to
  • ~= : Not equal to
  • < : Less than
  • > : Greater than
  • <= : Less than or equal to
  • >= : Greater than or equal to

The Length Operator (#)

The length operator # is used to determine the length of a string or a table. When applied to a string, it returns the number of characters. When applied to a table, it returns the number of elements in the table.

Conclusion

Understanding Lua operators is crucial for performing basic operations in your scripts. By mastering arithmetic, relational, and the # length operator, you build a strong foundation for more complex programming tasks. In the next section, we will explore conditional statements with If Else.

Previous
Data Types