Difference between revisions of "Lua"
(→For) |
(→If Statement) |
||
Line 56: | Line 56: | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | =Conditions= | ||
+ | ==Relational== | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Symbols !! Explanation | ||
+ | |- | ||
+ | | == || Equal | ||
+ | |- | ||
+ | | ~= || Not equal | ||
+ | |- | ||
+ | | <= || Less than or equal | ||
+ | |- | ||
+ | | >= || Greater than or equal | ||
+ | |- | ||
+ | | < || Less than | ||
+ | |- | ||
+ | | > || Greater than | ||
+ | |} | ||
+ | ==Conditional== | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | ! Operator !!Description !! Example | ||
+ | |- | ||
+ | | and || Called Logical AND operator. If both the operands are non zero then condition becomes true.||(A and B) is false | ||
+ | |- | ||
+ | | or || Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. || (A or B) is true. | ||
+ | |- | ||
+ | | not || Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. || !(A and B) is true. | ||
+ | |} | ||
+ | |||
-- Undefined variables return nil. | -- Undefined variables return nil. |
Revision as of 15:40, 2 June 2019
Contents
Comments
-- Two dashes start a one-line comment.
--[[
Adding two ['s and ]'s makes it a
multi-line comment.
--]]
Variables
Numbers
num = 42 -- All numbers are doubles.
Strings
s = 'walternate' -- Immutable strings like Python.
t = "double-quotes are also fine"
u = [[ Double brackets
start and end
multi-line strings.]]
-- String concatenation uses the .. operator:
message = 'Winter is coming, ' .. line
Empty / Null
t = nil -- Undefines t; Lua has garbage collection.
Global Variables
-- Variables are global by default.
thisIsGlobal = 5 -- Camel case is common.
Local Variables
-- How to make a variable local:
local line = io.read() -- Reads next stdin line.
If Statement
if num < 40 then
print('below 40')
elseif name ~= 'wayne' then
-- ~= is not equals.
-- Equality check is == like Python; ok for strs.
io.write('over 40 and Name is wayne\n') -- Defaults to stdout.
else
print('above 40')
end
Conditions
Relational
Symbols | Explanation |
---|---|
== | Equal |
~= | Not equal |
<= | Less than or equal |
>= | Greater than or equal |
< | Less than |
> | Greater than |
Conditional
Operator | Description | Example |
---|---|---|
and | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A and B) is false |
or | Called Logical OR Operator. If any of the two operands is non zero then condition becomes true. | (A or B) is true. |
not | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A and B) is true. |
-- Undefined variables return nil.
-- This is not an error:
foo = anUnknownVariable -- Now foo = nil.
aBoolValue = false
-- Only nil and false are falsy; 0 and are true! if not aBoolValue then print('twas false') end
-- 'or' and 'and' are short-circuited. -- This is similar to the a?b:c operator in C/js: ans = aBoolValue and 'yes' or 'no' --> 'no'
Loops
While
-- Blocks are denoted with keywords like do/end:
while num < 50 do
num = num + 1 -- No ++ or += type operators.
end
For
karlSum = 0
for i = 1, 100 do -- The range includes both ends.
karlSum = karlSum + i
end
-- Use "100, 1, -1" as the range to count down:
fredSum = 0
for j = 100, 1, -1 do
fredSum = fredSum + j
end
-- In general, the range is begin, end[, step].
Repeat / Do While
-- Another loop construct:
repeat
print('the way of the future')
num = num - 1
until num == 0