Expanding Your Decision-Making Toolkit

In the last lesson, you learned how to use nested if statements in Lua to make decisions based on more than one condition. Now, let's take your skills a step further. In this lesson, you will discover how to use logical operators — and, or, and not — to combine and control multiple conditions in a single line. This will help you write cleaner, more flexible code and make your programs even smarter.

What You'll Learn

Logical operators allow you to check several requirements at once without needing to nest multiple if statements. Here’s a quick look at how each operator works:

  • and: Both conditions must be true.
  • or: At least one condition must be true.
  • not: Reverses the meaning of a condition.

Let’s look at a real example:

local has_passport = true
local has_visa = false

-- Using 'and' - both conditions must be true
if has_passport and has_visa then
    print("Ready for international travel!")
else
    print("Missing required documents.")
end

-- Using 'or' - at least one condition must be true
local has_train_ticket = false
local has_plane_ticket = true
if has_train_ticket or has_plane_ticket then
    print("You have a ticket for your trip.")
end

-- Using 'not' - inverts the boolean value
if not has_visa then
    print("Reminder: You still need to apply for a visa.")
end

In the first part, the program checks if the traveler has both a passport and a visa. If either is missing, it prints a reminder. Next, it checks if the traveler has either a train or a plane ticket. Finally, it uses not to remind the traveler if they still need to get a visa.

Expected Output:

Missing required documents.
You have a ticket for your trip.
Reminder: You still need to apply for a visa.

Why do we see this output?

  • The first if statement uses and. Since has_passport is true but has_visa is false, the combined condition is false, so the else branch runs and prints "Missing required documents."
  • The second if statement uses or. has_train_ticket is false, but has_plane_ticket is true, so the combined condition is true, and it prints "You have a ticket for your trip."
  • The third if statement uses not. Since has_visa is false, not has_visa is true, so it prints "Reminder: You still need to apply for a visa."
Operator Precedence and Parentheses

When you combine multiple logical operators in a single condition, Lua follows a specific order called operator precedence: not is evaluated first, then and, and finally or. This can sometimes lead to surprising results if you’re not careful. For example:

local a = false
local b = true
local c = true

if a or b and c then
    print("This will print!")
end

In this example, b and c is evaluated first (which is true), then a or true is evaluated (which is also true). So the message prints. However, if you want to change the order, you should use parentheses:

if (a or b) and c then
    print("This will also print!")
end

Using parentheses makes your intentions clear and your code easier to read. As a best practice, always use parentheses when mixing and, or, and not to avoid confusion and bugs.

Why Logical Operators Matter

Logical operators are essential for writing clear and powerful code. They let you handle real-life situations where you need to check several things at once — like making sure someone has all the documents needed for a trip or checking if at least one ticket is available. By mastering these operators, you can make your programs more efficient and easier to read.

Ready to practice using logical operators and see how they can simplify your code? Let’s get started!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal