YouTip LogoYouTip

Lua Miscellaneous Operator

\\ An operator is a special symbol used to tell the interpreter to perform specific mathematical or logical operations. Lua provides the following types of operators:\\ \\ * Arithmetic Operators\\ * Relational Operators\\ * Logical Operators\\ * Other Operators\\ \\ * * *\\ \\ ## Arithmetic Operators\\ \\ The following table lists the common arithmetic operators in Lua, assuming the value of A is 10 and B is 20:\\ \\ | Operator | Description | Example |\\ | --- | --- | --- |\\ | + | Addition | A + B outputs 30 |\\ | - | Subtraction | A - B outputs -10 |\\ | * | Multiplication | A * B outputs 200 |\\ | / | Division | B / A outputs 2 |\\ | % | Modulus (remainder) | B % A outputs 0 |\\ | ^ | Exponentiation | A^2 outputs 100 |\\ | - | Unary minus | -A outputs -10 |\\ | // | Floor division (>=lua5.3) | 5//2 outputs 2 |\\ \\ ### Example\\ \\ We can better understand the application of arithmetic operators through the following examples:\\ \\ ## Example\\ \\ a =21\\ \\ b =10\\ \\ c = a + b\\ \\ print("Line 1 - c value is ", c )\\ \\ c = a - b\\ \\ print("Line 2 - c value is ", c )\\ \\ c = a * b\\ \\ print("Line 3 - c value is ", c )\\ \\ c = a / b\\ \\ print("Line 4 - c value is ", c )\\ \\ c = a % b\\ \\ print("Line 5 - c value is ", c )\\ \\ c = a^2\\ \\ print("Line 6 - c value is ", c )\\ \\ c =-a\\ \\ print("Line 7 - c value is ", c )\\ \\ The output of the above program is:\\ \\ Line 1 - c value is 31Line 2 - c value is 11Line 3 - c value is 210Line 4 - c value is 2.1Line 5 - c value is 1Line 6 - c value is 441Line 7 - c value is -21\\ In Lua, `/` is used for division, and the result includes a decimal part. `//` is used for floor division, and the result does not include a decimal part:\\ \\ ## Example\\ \\ a =5\\ \\ b =2\\ \\ print("Division operation - a/b value is ", a / b )\\ \\ print("Integer division operation - a//b value is ", a // b )\\ \\ The output of the above program is:\\ \\ Division operation - a/b value is 2.5Integer division operation - a//b value is 2\\ \\ * * *\\ \\ ## Relational Operators\\ \\ The following table lists the common relational operators in Lua, assuming the value of A is 10 and B is 20:\\ \\ | Operator | Description | Example |\\ | --- | --- | --- |\\ | == | Equal to, checks if two values are equal. Returns true if equal, otherwise false. | (A == B) is false. |\\ | ~= | Not equal to, checks if two values are not equal. Returns true if not equal, otherwise false. | (A ~= B) is true. |\\ | > | Greater than, returns true if the left value is greater than the right value, otherwise false. | (A > B) is false. |\\ | < | Less than, returns true if the left value is less than the right value, otherwise false. | (A = | Greater than or equal to, returns true if the left value is greater than or equal to the right value, otherwise false. | (A >= B) returns false. |\\ | <= | Less than or equal to, returns true if the left value is less than or equal to the right value, otherwise false. | (A <= B) returns true. |\\ \\ ### Example\\ \\ We can better understand the application of relational operators through the following examples:\\ \\ ## Example\\ \\ a =21\\ \\ b =10\\ \\ if( a == b )\\ \\ then\\ \\ print("Line 1 - a equal to b")\\ \\ else\\ \\ print("Line 1 - a Noequal to b")\\ \\ end\\ \\ if( a ~= b )\\ \\ then\\ \\ print("Line 2 - a Noequal to b")\\ \\ else\\ \\ print("Line 2 - a equal to b")\\ \\ end\\ \\ if( a b )\\ \\ then\\ \\ print("Line 4 - a greater than b")\\ \\ else\\ \\ print("Line 5 - a Less than or equal to b")\\ \\ end\\ \\ -- Modify a and b the values of\\ \\ a =5\\ \\ b =20\\ \\ if( a = a )\\ \\ then\\ \\ print("Line 6 - b greater than or equal to a")\\ \\ end\\ \\ The output of the above program is:\\ \\ Line 1 - a Noequal to b Line 2 - a Noequal to b Line 3 - a greater thanequal to b Line 4 - a greater than b Line 5 - a Less than or equal to b Line 6 - b greater than or equal to a\\ \\ * * *\\ \\ ## Logical Operators\\ \\ The following table lists the common logical operators in Lua, assuming the value of A is true and B is false:\\ \\ | Operator | Description | Example |\\ | --- | --- | --- |\\ | and | Logical AND operator. If A is false, returns A, otherwise returns B. | (A and B) is false. |\\ | or | Logical OR operator. If A is true, returns A, otherwise returns B. | (A or B) is true. |\\ | not | Logical NOT operator. The opposite of the logical result. If the condition is true, logical NOT is false. | not(A and B) is true. |\\ \\ ### Example\\ \\ We can better understand the application of logical operators through the following examples:\\ \\ ## Example\\ \\ a =true\\ \\ b =true\\ \\ if( a and b )\\ \\ then\\ \\ print("a and b - Condition is true")\\ \\ end\\ \\ if( a or b )\\ \\ then\\ \\ print("a or b - Condition is true")\\ \\ end\\ \\ print("---------Divider---------")\\ \\ -- Modify a and b the values of\\ \\ a =false\\ \\ b =true\\ \\ if( a and b )\\ \\ then\\ \\ print("a and b - Condition is true")\\ \\ else\\ \\ print("a and b - Condition is false")\\ \\ end\\ \\ if(not( a and b))\\ \\ then\\ \\ print("not( a and b) - Condition is true")\\ \\ else\\ \\ print("not( a and b) - Condition is false")\\ \\ end\\ \\ The output of the above program is:\\ \\ a and b - Condition is true a or b - Condition is true---------Divider--------- a and b - Condition is falsenot( a and b) - Condition is true\\ \\ * * *\\ \\ ## Other Operators\\ \\ The following table lists the concatenation operator and the operator for calculating the length of a table or string in Lua:\\ \\ | Operator | Description | Example |\\ | --- | --- | --- |\\ | .. | Concatenates two strings | a..b, where a is "Hello " and b is "World", outputs "Hello World". |\\ | # | Unary operator, returns the length of a string or table. | #"Hello" returns 5 |\\ \\ ### Example\\ \\ We can better understand the application of the concatenation operator and the operator for calculating the length of a table or string through the following examples:\\ \\ ## Example\\ \\ a ="Hello "\\ \\ b ="World"\\ \\ print("Concatenate strings a and b ", a..b )\\ \\ print("b String length ",#b )\\ \\ print("String Test length ",#"Test")\\ \\ print("URL length ",#"www..com")\\ \\ The output of the above program is:\\ \\ Concatenate strings a and b Hello World b String length 5String Test length 4URL length 14\\ \\ * * *\\ \\ ## Operator Precedence\\ \\ From highest to lowest:\\ \\ ^not - (unary)* / %+ -.. = ~= ==andor\\ All binary operators except `^` and `..` are left-associative.\\ \\ a+i < b/2+1 (a+i) < ((b/2)+1)5+x^2*8 5+((x^2)*8) a < y and y <= z (a < y) and (y <= z)-x^2 -(x^2) x^y^z x^(y^z)\\ ### Example\\ \\ We can better understand the precedence of Lua operators through the following examples:\\ \\ ## Example\\ \\ a =20\\ \\ b =10\\ \\ c =15\\ \\ d =5\\ \\ e =(a + b)* c / d;-- ( 30 * 15 ) / 5\\ \\ print("(a + b) * c / d Operation value is :",e )\\ \\ e =((a + b)* c)/ d;-- (30 * 15 ) / 5\\ \\ print("((a + b) * c) / d Operation value is :",e )\\ \\ e =(a + b)*(c / d);-- (30) * (15/5)\\ \\ print("(a + b) * (c / d) Operation value is :",e )\\ \\ e = a +(b * c)/ d;-- 20 + (150/5)\\ \\ print("a + (b * c) / d Operation value is :",e )\\ \\ The output of the above program is:\\ \\ (a + b) * c / d Operation value is :90.0((a + b) * c) / d Operation value is :90.0(a + b) * (c / d) Operation value is :90.0 a + (b * c) / d Operation value is :50.0
← Lua ArraysClass Member Access Operator O β†’

YouTip © 2024-2026 | Home | Learn Technology, Build Dreams!

All content is for educational and learning purposes only.