Chapter 0.4: Making Decisions with if, else if, and else
This is where your script comes alive. Control flow statements use the true/false results from your comparisons to direct the flow of your script's execution.
local cubesOnButtons = 1
local requiredCubes = 2
if (cubesOnButtons >= requiredCubes) {
// This condition is false (1 is not >= 2). This block is SKIPPED.
printl("Door Unlocked.")
}
else {
// The script executes this block instead.
printl("Door remains locked.")
}
Visualization: The Path of Logic
Think of your code's execution as a flow chart. if/else statements are the decision points.
if (energyLevel >= 90) {
printl("Full power.");
} else if (energyLevel >= 50) {
printl("Bridge flickering.");
} else {
printl("Bridge collapsed!");
}
[Start Script]
Is energyLevel >= 90 ?
(False)
Is energyLevel >= 50 ?
(False)
[Print "Bridge collapsed!"]
(True)
[Print "Bridge flickering."]
(True)
[Print "Full power."]
[End Script]