Difference between revisions of "Basic lua: Operators"

From The Official Visionaire Studio: Adventure Game Engine Wiki
m
 
(73 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Lua operators are expressions used to perform calculations or to pass arguments between different value types.
+
{| class="in"
{| class="ex"
 
 
|-
 
|-
| style="font-size:42px;text-align:left;font-style:normal;width:5%;" |  [[basic_lua|<]]
+
| class="i_arrow clickablecell" |  [[basic_lua:_Basics|<]]
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;width:30%" | [[basic_lua|Index]]
+
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;color:white;width:30%;" | '''[[basic_lua|Index]]'''
+
| class="i_arrow clickablecell" | [[basic_lua:_Types|>]]
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;width:30%;" | [[basic_lua:types|Types]]
 
| style="font-size:42px;text-align:right;font-style:normal;width:5%;padding-right:10px;" | [[basic_lua:types|>]]
 
 
|}
 
|}
 +
<hr>
 +
Lua operators are expressions used to perform calculations or to pass arguments between different value types.
 +
 
== Conditional Operators ==
 
== Conditional Operators ==
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| if || width="90%" | Query if something does or does not meet a certain condition.
+
| <span class="vsyellow">if</span> || Query if something does or does not meet a certain condition.
 
|-
 
|-
| else || Do something else if the query condition was not met.
+
| <span class="vsyellow">else</span> || Do something else if the query condition was not met.
 
|-
 
|-
| elseif || Used to add additional if queries, if the initial query condition was not met.
+
| <span class="vsyellow">elseif</span> || Used to add additional if queries, if the initial query condition was not met.
 
|-
 
|-
| end || This is used to close various queries or functions; there must be the same amount of '''end''' as '''if'''; except in instances where '''elseif''' has been used.
+
| <span class="vsyellow">end</span> || This is used to close various queries or functions; there must be the same amount of '''end''' as '''if'''; except in instances where '''elseif''' has been used.
 
|}  
 
|}  
  
 +
=== if ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 1: if condition is true (method 1) !!
+
! if condition is true (method 1) !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = true
 
local a = true
  
 
if a then
 
if a then
print("a = true")
+
  print("a = true")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_001.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_1.png|frameless|center|180px]]
 
|}
 
|}
 
+
<hr>
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 2: if condition is true (method 2) !!
+
! if condition is true (method 2) !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = true
 
local a = true
  
 
if a == true then
 
if a == true then
print("a = true")
+
  print("a = true")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_002.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_2.png|frameless|center|180px]]
 
|}
 
|}
 
+
<hr>
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 3: if condition is false !!
+
! if condition is false !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = false
 
local a = false
  
 
if a == false then
 
if a == false then
print("a = false")
+
  print("a = false")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_004.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_3.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== else ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 4: if condition is not met, then do else !!
+
! if condition is not met, then do else !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = false
 
local a = false
  
 
if a then
 
if a then
print("a = true")
+
  print("a = true")
 
else
 
else
print("a = false")
+
  print("a = false")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_005.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_4.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== elseif ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 5: if condition is not met, then do elseif !!
+
! if condition is not met, then do elseif !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = false
 
local a = false
  
 
if a then
 
if a then
print("a = true")
+
  print("a = true")
elseif not a then
+
elseif a == false then
print("a = false")
+
  print("a = false")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_006.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(conditional_operators)_5.png|frameless|center|180px]]
 
|}
 
|}
  
Line 92: Line 95:
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| and || width="90%" | Allows you to check multiple conditions in a single if query.
+
| <span class="vsyellow">and</span> || width="90%" | Allows you to check multiple conditions in a single if query.
 
|-
 
|-
| or || Allows you to add an alternative if query into a single if query.
+
| <span class="vsyellow">or</span> || Allows you to add an alternative if query into a single if query.
 
|-
 
|-
| not || Checks if condition equals false or negative.
+
| <span class="vsyellow">not</span> || Checks if condition equals false or negative.
 
|}  
 
|}  
  
 +
=== and ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 1: if condition a is true and condition b is false then !!
+
! if condition a is true and condition b is false then !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = true
 
local a = true
 
local b = false
 
local b = false
  
 
if a == true and b == false then
 
if a == true and b == false then
print("a = true", "b=false")
+
  print("a = true", "b = false")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_007.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(logical_operators)_1.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== or ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 2: if condition is true or condition == true then !!
+
! if condition is true or condition == true then !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = true
 
local a = true
  
Line 123: Line 128:
 
  print("a = true")
 
  print("a = true")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_008.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(logical_operators)_2.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== not ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 3: if condition is not true !!
+
! if condition is not true !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = false
 
local a = false
  
Line 136: Line 142:
 
  print("a = false")
 
  print("a = false")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_003.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(logical_operators)_3.png|frameless|center|180px]]
 
|}
 
|}
  
Line 142: Line 148:
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| <big>==</big> || width="90%" | Equal to.
+
| <span class="vsyellow">==</span> || width="90%" | Equal to.
 
|-
 
|-
| <big>~=</big> || Does not equal.
+
| <span class="vsyellow">~=</span> || Does not equal.
 
|-
 
|-
| <big>>=</big> || Greater than or equal to.
+
| <span class="vsyellow">>=</span> || Greater than or equal to.
 
|-
 
|-
| <big>></big> || Greater than.
+
| <span class="vsyellow">></span> || Greater than.
 
|-
 
|-
| <big><=</big> || Less than or equal to.
+
| <span class="vsyellow"><=</span> || Less than or equal to.
 
|-
 
|-
| <big><</big> || Less than.
+
| <span class="vsyellow"><</span> || Less than.
 
|}  
 
|}  
  
 +
=== equal to ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
Example 1: if a equals 1 !!
+
!  if a equals 1 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 1
 
local a = 1
  
Line 165: Line 172:
 
  print("a = 1")
 
  print("a = 1")
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_009.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_1.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== does not equal ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 2: if a does not equal 2 !!
+
! if a does not equal 2 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 1
 
local a = 1
  
Line 178: Line 186:
 
  print("a = " .. a)
 
  print("a = " .. a)
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_010.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_2.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== greater than or equal to ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 3: if a is greater than or equal to 2 !!
+
! if a is greater than or equal to 2 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 5
 
local a = 5
  
Line 191: Line 200:
 
  print("a = " .. a)
 
  print("a = " .. a)
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_011.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_3.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== greater than ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 4: if a is greater than 2 !!
+
! if a is greater than 2 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 3
 
local a = 3
  
Line 204: Line 214:
 
  print("a = " .. a)
 
  print("a = " .. a)
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_012.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_4.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== less than or equal to ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 5: if a less than or equal to 2  !!
+
! if a less than or equal to 2  !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 2
 
local a = 2
  
Line 217: Line 228:
 
  print("a = " .. a)
 
  print("a = " .. a)
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_013.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_5.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== less than ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 6: if a less than 2  !!
+
! if a less than 2  !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 0
 
local a = 0
  
Line 230: Line 242:
 
  print("a = " .. a)
 
  print("a = " .. a)
 
end
 
end
</syntaxhighlight> || width="200px" | [[File:lb_operators_014.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(comparison_operators)_6.png|frameless|center|180px]]
 
|}
 
|}
  
Line 236: Line 248:
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| <big>+</big> || width="90%" | Add
+
| <span class="vsyellow">+</span> || width="90%" | Add
 
|-
 
|-
| <big>-</big> || Subtract or invert
+
| <span class="vsyellow">-</span> || Subtract or invert
 
|-
 
|-
| <big>*</big> || Multiply
+
| <span class="vsyellow">*</span> || Multiply
 
|-
 
|-
| <big>/</big> || Divide
+
| <span class="vsyellow">/</span> || Divide
 
|-
 
|-
| <big>^</big> || Power
+
| <span class="vsyellow">^</span> || Power
 
|-
 
|-
| <big>%</big> || Remainder
+
| <span class="vsyellow">%</span> || Remainder
 
|}
 
|}
  
 +
=== add ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 1: 1 +2 = 3 !!
+
! 1 + 2 = 3 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 1
 
local a = 1
 
local b = 2
 
local b = 2
Line 259: Line 272:
  
 
print( result )
 
print( result )
</syntaxhighlight> || width="200px" | [[File:lb_operators_015.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_1.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== subtract ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 2: 3 - 2 = 1 !!
+
! 3 - 2 = 1 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 3
 
local a = 3
 
local b = 2
 
local b = 2
Line 272: Line 286:
  
 
print( result )
 
print( result )
</syntaxhighlight> || width="200px" | [[File:lb_operators_016.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_2.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== inverse ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 3: inversed values !!
+
! ''<small>inv</small>'' of 3 = -3 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 3
 
local a = 3
 
local result = -a
 
local result = -a
  
 
print( "original value: " .. a, "inversed value: " .. result )
 
print( "original value: " .. a, "inversed value: " .. result )
</syntaxhighlight> || width="200px" | [[File:lb_operators_017.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_3.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== multiply ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 4: 2 x 2 = 4 !!
+
! 2 x 2 = 4 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 2
 
local a = 2
 
local result = a * a
 
local result = a * a
  
 
print( result )
 
print( result )
</syntaxhighlight> || width="200px" | [[File:lb_operators_018.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_4.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== divide ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 5: 10 ÷ 5 = 2  !!
+
! 10 ÷ 5 = 2  !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 10
 
local a = 10
 
local b = 5
 
local b = 5
Line 309: Line 326:
  
 
print( result )
 
print( result )
</syntaxhighlight> || width="200px" | [[File:lb_operators_019.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_5.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== power ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 6: base of 2, power of 8 (2ⁿ) = 256 !!
+
! base of 2, power of 8 (2ⁿ) = 256 (2x2=4x2=8x2=16x2=32x2=64x2=128x2=256) !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
local a = 2
+
local a, b = 2, 8
local b = 8
+
local result = a ^ b -- generate answer
local result = 2 ^ 8
 
  
 
-- break down power function into string result (ignore this code)
 
-- break down power function into string result (ignore this code)
 
function power(i1, i2)
 
function power(i1, i2)
  local val = ""
+
  local val = "" -- clear string
 
  --+--
 
  --+--
  for i = 1, i2 do
+
  for i = 1, i2 do -- generate the longhand math formula
 
   if i < i2 then val = val..i1.."*" else val = val..i1 end
 
   if i < i2 then val = val..i1.."*" else val = val..i1 end
 
  end
 
  end
Line 331: Line 348:
 
end
 
end
  
print("power value of 2 & 8 = " .. result, power(a,b))
+
print("power value of "..a.." & "..b.." = " .. result, power(a,b))
</syntaxhighlight> || width="200px" | [[File:lb_operators_020.png|thumb|right|180px|click to enlarge]]
+
 
 +
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_6.png|frameless|center|180px]]
 
|}
 
|}
  
{| class="ts" style="max-width:100%;"
+
=== remainder ===
|-
+
{| class="ts"
! Example 7: remainder of 25 ÷ 4 = 1
 
! style="min-width:210px;" | &nbsp;
 
 
|-
 
|-
| <syntaxhighlight>
+
! remainder of 25 ÷ 4 = 1 (25 ÷ 4 = 6.25 - 6 = 0.25 * 4 = 1) !!
 +
|-
 +
| style="max-width:680px;" | <syntaxhighlight lang="lua">
 
local a = 25
 
local a = 25
 
local b = 4
 
local b = 4
local result = a / 4
+
local result = a / b -- 25 ÷ 4 = 6.25
local remainder = a % b
+
local result_decimal = math.fmod(result, 1) -- 6.25 - 6 = 0.25
local decimal = remainder / b
+
local result_rounded = math.modf(result) -- 6.25 - 0.25 = 6
 +
local remainder = a % b -- 0.25 * 4 = 1
  
print("result: 25 ÷ 4 = " .. result, "remainder: .25 x 4 = " .. remainder, "decimal: 1 ÷ 4 = " .. decimal )
+
print("result: " .. result .. ", decimal: " .. result_decimal .. ", rounded: " .. result_rounded .. ", remainder: " .. remainder)
</syntaxhighlight> || width="200px" | [[File:lb_operators_021.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(mathematical_operators)_7.png|frameless|center|180px]]
 
|}
 
|}
 
+
<hr>
{| class="ex"
+
{| class="in"
 
|-
 
|-
| style="font-size:42px;text-align:left;font-style:normal;width:5%;" |  [[basic_lua|<]]
+
| class="i_arrow" |  [[basic_lua:_Basics|<]]
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;width:30%" | [[basic_lua|Index]]
+
| class="i_norm" | '''[[basic_lua:_Index|Index]]'''
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;color:white;width:30%;" | '''[[basic_lua|Index]]'''
+
| class="i_arrow" | [[basic_lua:_Types|>]]
| style="vertical-align:middle;text-align:center;font-size:16px;font-style:normal;width:30%;" | [[basic_lua:types|Types]]
+
|}{{toc}}
| style="font-size:42px;text-align:right;font-style:normal;width:5%;padding-right:15px;" | [[basic_lua:types|>]]
 
|}
 
{{toc}}
 

Latest revision as of 13:50, 5 September 2022

< Index >

Lua operators are expressions used to perform calculations or to pass arguments between different value types.

Conditional Operators

if Query if something does or does not meet a certain condition.
else Do something else if the query condition was not met.
elseif Used to add additional if queries, if the initial query condition was not met.
end This is used to close various queries or functions; there must be the same amount of end as if; except in instances where elseif has been used.

if

if condition is true (method 1)
local a = true

if a then
  print("a = true")
end
Basic lua (conditional operators) 1.png

if condition is true (method 2)
local a = true

if a == true then
  print("a = true")
end
Basic lua (conditional operators) 2.png

if condition is false
local a = false

if a == false then
  print("a = false")
end
Basic lua (conditional operators) 3.png

else

if condition is not met, then do else
local a = false

if a then
  print("a = true")
else
  print("a = false")
end
Basic lua (conditional operators) 4.png

elseif

if condition is not met, then do elseif
local a = false

if a then
  print("a = true")
elseif a == false then
  print("a = false")
end
Basic lua (conditional operators) 5.png

Logical Operators

and Allows you to check multiple conditions in a single if query.
or Allows you to add an alternative if query into a single if query.
not Checks if condition equals false or negative.

and

if condition a is true and condition b is false then
local a = true
local b = false

if a == true and b == false then
  print("a = true", "b = false")
end
Basic lua (logical operators) 1.png

or

if condition is true or condition == true then
local a = true

if a or a == true then
 print("a = true")
end
Basic lua (logical operators) 2.png

not

if condition is not true
local a = false

if not a then
 print("a = false")
end
Basic lua (logical operators) 3.png

Comparison Operators

== Equal to.
~= Does not equal.
>= Greater than or equal to.
> Greater than.
<= Less than or equal to.
< Less than.

equal to

if a equals 1
local a = 1

if a == 1 then
 print("a = 1")
end
Basic lua (comparison operators) 1.png

does not equal

if a does not equal 2
local a = 1

if a ~= 2 then
 print("a = " .. a)
end
Basic lua (comparison operators) 2.png

greater than or equal to

if a is greater than or equal to 2
local a = 5

if a >= 2 then
 print("a = " .. a)
end
Basic lua (comparison operators) 3.png

greater than

if a is greater than 2
local a = 3

if a > 2 then
 print("a = " .. a)
end
Basic lua (comparison operators) 4.png

less than or equal to

if a less than or equal to 2
local a = 2

if a <=  2 then
 print("a = " .. a)
end
Basic lua (comparison operators) 5.png

less than

if a less than 2
local a = 0

if a <  2 then
 print("a = " .. a)
end
Basic lua (comparison operators) 6.png

Mathematical Operators

+ Add
- Subtract or invert
* Multiply
/ Divide
^ Power
% Remainder

add

1 + 2 = 3
local a = 1
local b = 2
local result = a + b

print( result )
Basic lua (mathematical operators) 1.png

subtract

3 - 2 = 1
local a = 3
local b = 2
local result = a - b

print( result )
Basic lua (mathematical operators) 2.png

inverse

inv of 3 = -3
local a = 3
local result = -a

print( "original value: " .. a, "inversed value: " .. result )
Basic lua (mathematical operators) 3.png

multiply

2 x 2 = 4
local a = 2
local result = a * a

print( result )
Basic lua (mathematical operators) 4.png

divide

10 ÷ 5 = 2
local a = 10
local b = 5
local result = a / b

print( result )
Basic lua (mathematical operators) 5.png

power

base of 2, power of 8 (2ⁿ) = 256 (2x2=4x2=8x2=16x2=32x2=64x2=128x2=256)
local a, b = 2, 8
local result = a ^ b -- generate answer

-- break down power function into string result (ignore this code)
function power(i1, i2)
 local val = "" -- clear string
 --+--
 for i = 1, i2 do -- generate the longhand math formula
  if i < i2 then val = val..i1.."*" else val = val..i1 end
 end
 return "which is the equivalent of " .. val
end

print("power value of "..a.." & "..b.." = " .. result, power(a,b))
Basic lua (mathematical operators) 6.png

remainder

remainder of 25 ÷ 4 = 1 (25 ÷ 4 = 6.25 - 6 = 0.25 * 4 = 1)
local a = 25
local b = 4
local result = a / b -- 25 ÷ 4 = 6.25
local result_decimal = math.fmod(result, 1) -- 6.25 - 6 = 0.25
local result_rounded = math.modf(result) -- 6.25 - 0.25 = 6
local remainder = a % b -- 0.25 * 4 = 1

print("result: " .. result .. ", decimal: " .. result_decimal .. ", rounded: " .. result_rounded .. ", remainder: " .. remainder)
Basic lua (mathematical operators) 7.png

< Index >