Learn Lua · Basics · Lesson 2 of 4

Making decisions

if runs code only when something is true. elseif tries another condition, and else covers everything left. Compare values with <, <=, >, >=, == (equal) and ~= (not equal).

LUA
local total = 30
local label
if total >= 50 then
  label = "large"
elseif total >= 20 then
  label = "medium"
else
  label = "small"
end
label  --> "medium"

The condition can be anything that gives true or false, including comparisons joined with and, or and not. A boolean field works on its own: if input.express then runs when input.express is true.

A variable you declare with local before the if can be changed inside it, and keeps its new value afterwards.

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { total = 120, express = true }

Steps

  1. Charge 4.95 delivery when the order total is under 50.
  2. Make delivery free for totals of 50 or more.
  3. Express orders, where input.express is true, always pay 9.95.
Show a hint

Check input.express first, then the total. A total of exactly 50 is free, so the paid case is input.total < 50.