0% found this document useful (0 votes)
5 views4 pages

Lua 1.1

Uploaded by

fastkilar29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Lua 1.1

Uploaded by

fastkilar29
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Sure!

In Lua, conditions are used to control the flow of execution based on specific
criteria. Here's a simplified guide to all possible conditional structures in Lua:
1. If Statement
The basic if statement checks whether a condition is true or false and executes
code accordingly.
if condition then
-- code to execute if condition is true
end
Example:
local age = 20
if age >= 18 then
print("You are an adult.")
end
2. If-Else Statement
The else clause allows you to specify code to execute when the if condition is false.
if condition then
-- code if condition is true
else
-- code if condition is false
end
Example:
local age = 16
if age >= 18 then
print("You are an adult.")
else
print("You are a minor.")
end
3. If-Elif-Else (elseif) Statement
You can check multiple conditions using elseif.
if condition1 then
-- code if condition1 is true
elseif condition2 then
-- code if condition2 is true
else
-- code if neither condition1 nor condition2 are true
end
Example:
local age = 25
if age < 18 then
print("You are a minor.")
elseif age >= 18 and age < 60 then
print("You are an adult.")
else
print("You are a senior citizen.")
end
4. Logical Operators
You can combine multiple conditions using logical operators:
and: True if both conditions are true.
or: True if at least one condition is true.
not: Reverses the truth value.
if condition1 and condition2 then
-- code if both conditions are true
end

if condition1 or condition2 then


-- code if either condition is true
end

if not condition then


-- code if condition is false
end
Example:
local x = 10
local y = 5
if x > y and x < 20 then
print("x is between y and 20.")
end
5. Ternary Operator (Using and and or)
Lua doesn't have a built-in ternary operator like some languages, but you can use
and/or as a workaround for conditional expressions.
result = condition and value_if_true or value_if_false
Example:
local age = 20
local status = age >= 18 and "Adult" or "Minor"
print(status) -- Output: Adult
6. Comparison Operators
To evaluate conditions, you often use comparison operators:
==: Equal to
~= or !=: Not equal to
<: Less than
<=: Less than or equal to
>: Greater than
>=: Greater than or equal to
if a == b then
-- a is equal to b
end
Example:
local x = 10
local y = 5
if x > y then
print("x is greater than y.")
end
7. Nested Conditions
You can also nest conditions inside other conditions.
if condition1 then
if condition2 then
-- code if both conditions are true
else
-- code if condition1 is true and condition2 is false
end
end
Example:
local age = 25
if age >= 18 then
if age < 60 then
print("You are an adult.")
end
end
8. Short-Circuit Evaluation
Lua evaluates conditions from left to right and stops as soon as it knows the result
(i.e., it doesn't check the second condition if the first condition is sufficient to
determine the outcome).
Example:
local x = 10
local y = 5
if x > y and y > 0 then
print("Both conditions are true!")
end
In this case, y > 0 will only be evaluated if x > y is true.
9. Pattern Matching (for advanced cases)
Although not exactly a conditional in the traditional sense, Lua provides powerful
pattern matching capabilities using string:match(), which can also act like a
conditional check.
local text = "Hello Lua"
if text:match("Lua") then
print("Found Lua!")
end

Summary of Conditional Statements in Lua:


if: Simple condition check.
if-else: Adds an alternative for when the condition is false.
if-elseif-else: Checks multiple conditions in sequence.
Logical operators (and, or, not): Combine or negate conditions.
Ternary-like operator: Conditional assignment using and/or.
Comparison operators: Used to compare values.
Nested conditions: Conditions within conditions.
Short-circuit evaluation: Stops evaluating further conditions when the result is
determined.
Pattern matching: Advanced conditional checks with strings.
Let me know if you'd like more examples or clarification on any of these!

You might also like