Conditional statements
Tutorial: Conditional Statements | |
This is a tutorial will teach you how to make your script react to different situations. | |
Objective | To gain a basic understanding of programming in ComputerCraft. |
Prerequisites | A basic understanding of Variables. |
Contents |
The If statement
Conditional statements are one of the fundamental building blocks for any coding language. They are just as they sound, if a condition is met then a block of code will run. The simplest form of conditional statement available in Lua is the "If" statement, which indicates that if one or more conditions are met, the following block of code will run. If statements take the following format
local Variable = 1 if Variable==1 then write("The variable is 1!\n") end if Variable==2 then write("The variable is 2!\n") end
This script will simply output the text "The variable is 1!". The "==" operator simply means "is equal to", so the line reads along the lines of "If Variable is equal to 1, then do this". "end" marks the end of the code block, and anything after it is run normally regardless of whether the conditions were met.
Brackets may optionally be used around conditions, although it is not necessary unless you want to control the order of a calculation.
Note that since lua disregards new lines, it would be equally acceptable to write the following
if Variable==1 then write("The variable is 1!\n") end
However, this method is generally less readable and not commonly used.
Lua determines whether an if statement passes by using the boolean values returned by the condition, so it would be possible (but unnecessary) to enter a boolean as the condition
if true then write("This will always write!\n") end if false then write("This will never write!\n") end
Available conditions
There are a number of different conditions that can be used to make the conditional statement.
- Is equal to
if Variable == Variable2 then --Do stuff end
The "==" condition is the most basic operator, simply comparing two values to see if they are the same. If both values are the same, the condition is met and the if statement passes.
- Is not equal to
if Variable ~= Variable2 then --Do stuff end
The "~=" condition compares two values to see if they are the same, and if they are not the same, the condition is met and the if statement passes.
- Greater than
if Number > Number2 then --Do stuff end
The ">" condition compares two number values, and if the first is greater than the second, the condition is met and the if statement passes. The ">=" (Greater than or equal to) operator acts as both the "greater than" and the "equal to" conditions, meaning the condition is met if either the first number is equal to the second, or the first number is greater than the second.
- Less than
if Number < Number2 then --Do stuff end
The "<" condition compares two number values, and if the first is lower than the second, the condition is met and the if statement passes. The "<=" (Less than or equal to) operator acts as a combination of the "less than" and the "equal to" conditions.
And, Or, Not
Sometimes it is desirable to combine conditions. Although it is possible to use multiple if statements, or ifs inside ifs, it is a best practice to use "and" and "or" if possible.
- And
local Num1,Num2,Num3 = 1,2,3 --Multiple if if Num1 > Num2 then if Num2 < Num3 then write("Condition is true!") end end --Using And if (Num1>Num2) and (Num2<Num3) then write("Condition is true!") end
The "and" operator means that both conditions must match before the statement passes. If either side fails, the whole condition is false. Note that if the left side fails, the right side is ignored, so if you have a critical function it should be on the left of the "and".
- Or
local Num1,Num2,Num3 = 1,2,3 --Multiple if if Num1 > Num2 then write("Condition is true!") end if Num2 < Num3 then write("Condition is true!") end --Using Or if (Num1>Num2) or (Num2<Num3) then write("Condition is true!") end
The "or" operator means that if either condition matches the statement passes. This means that if either the left side, the right side, or both pass, the statement is true. Note that if the left side passes, the right side is ignored, so if you have a critical function it should be on the left of the "or".
- Not
local Num1,Num2,Num3 = 1,2,3 if (Num1 < Num2) and not (Num1 > Num3) then write("Condition is true!") end
While the "not" statement is rarely a necessity, it can be used to reverse a value from "true" to "false" or vice versa.
In some situations, it may be preferable not to use these statements as code will run before the second condition needs to be assessed, such as as follows
local Num1,Num2 = 1,2 --And should not be used here if Num1>Num2 then local Num3 = 3 if Num3>Num2 then write("Text!\n") end end
The Else statement
It is common that you may want to run some code on the failure of the condition. If this is the case, it may be achieved through the use of two separate if statements, but it's simpler to use the "else" statement.
local Num1 = 1 local Num2 = 2 --Using multiple if if Num1 > Num2 then write("Number 1 is greater!\n") end if Num1 <= Num2 then write("Number 1 is not greater!\n") end --Using else if Num1>Num2 then write("Number 1 is greater!\n") else write("Number 1 is not greater!\n") end
The ElseIf statement
Sometimes it may be desirable to run the "else" code only under certain conditions. This is where "elseif" becomes useful. It acts as if it is another if statement, but also functions as the "else" of the if it's in.
local Num1, Num2, Num3 = 1,2,3 --With elseif if Num1>Num2 then write("First condition passed!\n") elseif Num3>Num2 then write("First condition failed, second condition passed!\n") end
The "elseif" statement can become even more useful if you chain together more "elseif"s and "else"s with it, but remember that the first if that passes will break the chain!
if Bool then --Stuff elseif Var1 == Var2 then --Other stuff elseif Table[1] then --More stuff else --More again! end