This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
CraftedCart's profile picture

The Absolute Basic Guide To If Statements - Lua Edition

Started by CraftedCart, 16 August 2013 - 11:28 AM
CraftedCart #1
Posted 16 August 2013 - 01:28 PM
Created By [member='CraftedCart']

The Absolute Basic Guide To If Statements - Lua Edition


Note: This is the basics and probably does not cover everything about the if statement - This is intended for beginners wanting to learn lua.

Basic If Statement
The if statement is useful when you want to compare data, strings, integers and other types of data can be compared
First, lets see a syntax template

if --[[COMPARISON]] then
   --CODE IF COMPARISON IS TRUE
end
Let's see some basic code to understand it easier


if 2 + 2 == 4 then
   print("yay")
end
Output:

yay

Basically, if 2 + 2 is 4 (which it is) then write yay on the screen
The comparison is slotted between the words "if" and "then" (See where it says 2 + 2 == 4)
To check if something IS THE SAME AS something else, use a double equals sign (==)

If we change the code to something incorrect…

if 2 + 2 == 5 then
   print("yay")
end
Output:


… It dosen't say anything. Because 2 + 2 is not 5 then it skips over the entire if statement.

Some comparison signs you can use:

a == b   See if a is the same as b
a > b   See is a is bigger than b
a < b   See if a is smaller than b
There are more, but these are the basic ones

If…Else Statement
Another kind of if statement is the if…else statement.

if --[[COMPARISON]] then
   --CODE IF COMPARISON IS TRUE
else
   --CODE IF COMPARISON IS FALSE
end

Some easy to understand code


if 2 + 2 == 4 then
   print("yay")
else
   print("nay")
end
Output:

yay

Because 2 + 2 is 4 then it says yay and skips over the code in the else part but if we change the code to so something incorrect…

if 2 + 2 == 5 then
   print("yay")
else
   print("nay")
end
Output:

nay
… It says nay. Because 2 + 2 is not 5 then it skips over the main if code and executes the code in the else part

Not Modifyer
Lets come back to this code

if 2 + 2 == 5 then
   print("yay")
end
Output:


… It dosen't say anything. Because 2 + 2 is not 5 then it skips over the entire if statement.
If we want this code to output "yay", we can change the expression to

not (2 + 2 == 5)
Notice how I use brackets - the not modifier does not (no pun intended) always play well with math
The code would be

if not (2 + 2 == 5) then
   print("yay")
end
Output:

yay
Using the word "not" before typing the expression makes it say yay. 2 + 2 is not 5 so it is false. The "not" word makes false expressions true, therefore running the code. It also makes true expressions false, therefore skipping over the code.

Strings and Variables
If statements can use variables and strings so this code would not error

a = "Hi"

if a == "Hi" then
   print("yay")
else
   print("nay")
end
Output:

yay
ElvishJerricco #2
Posted 16 August 2013 - 02:00 PM
There's a lot of power that comes from realizing if statements aren't about comparisons, but expressions. The if statement doesn't use the operator to determine which code to run, the operator compares two values, and returns true or false. The if statement uses that true or false expression to determine which code to run. But if statements don't just use true or false. In Lua, an if statement will run the "if" code whenever the expression is any value besides false or nil. So this:


if 3 then
	print("What do you know, 3 is not in fact nil or false")
end

is perfectly valid. The reason this is important is because you never have to type something == nil ever again! Also note, the "not" modifier doesn't just negate true or false. It turns nil or false into true, and non-nil or true into false.


local aValue = someFunction()
if not aValue then
	doSomethingBecauseValueWasNil()
else
	doSomethingElse()
end

Let's say you're writing a function that has an optional parameter.


function someFunc(optional)
	if not optional then
		optional = someDefaultValue
	end
	... -- rest of function
end
Sangar #3
Posted 16 August 2013 - 02:12 PM
Although optional parameters are much nicer when written using an "or":
function f(optional)
  optional = optional or someDefaultValue
  -- rest
end

This has the same effect as the approach of using if not optional …

Keep in mind that either only works if false or nil are not expected values for optional, because otherwise those cases would always result in the default value being used, instead of the passed value.
CraftedCart #4
Posted 17 August 2013 - 01:48 AM
Spoiler
There's a lot of power that comes from realizing if statements aren't about comparisons, but expressions. The if statement doesn't use the operator to determine which code to run, the operator compares two values, and returns true or false. The if statement uses that true or false expression to determine which code to run. But if statements don't just use true or false. In Lua, an if statement will run the "if" code whenever the expression is any value besides false or nil. So this:


if 3 then
	print("What do you know, 3 is not in fact nil or false")
end

is perfectly valid. The reason this is important is because you never have to type something == nil ever again! Also note, the "not" modifier doesn't just negate true or false. It turns nil or false into true, and non-nil or true into false.


local aValue = someFunction()
if not aValue then
	doSomethingBecauseValueWasNil()
else
	doSomethingElse()
end

Let's say you're writing a function that has an optional parameter.


function someFunc(optional)
	if not optional then
		optional = someDefaultValue
	end
	... -- rest of function
end
Although optional parameters are much nicer when written using an "or":
function f(optional)
  optional = optional or someDefaultValue
  -- rest
end

This has the same effect as the approach of using if not optional …

Keep in mind that either only works if false or nil are not expected values for optional, because otherwise those cases would always result in the default value being used, instead of the passed value.

Thanks for the feedback. This tutorial is intended for the very beginners who want to get into computercraft so it does not cover everything (I even put a note on the top saying this). I might update this over time to include your feedback
reububble #5
Posted 17 August 2013 - 05:33 AM
Recently I've started using the format of

variable = (expression and trueValue or falseValue)

… That's all I really have to add… nice tute
immibis #6
Posted 15 September 2013 - 08:32 PM

if not 2 + 2 == 5 then

attempt to perform arithmetic __add on boolean and number.
This needs brackets, as in 'if not (2 + 2 == 5) then'
CraftedCart #7
Posted 19 September 2013 - 01:28 AM

if not 2 + 2 == 5 then

attempt to perform arithmetic __add on boolean and number.
This needs brackets, as in 'if not (2 + 2 == 5) then'
Thanks - i'll update my guide
ElvishJerricco #8
Posted 19 September 2013 - 02:05 AM
Recently I've started using the format of

variable = (expression and trueValue or falseValue)

… That's all I really have to add… nice tute

It should be noted that the expression for trueValue absolutely cannot equate to nil or false, because then even though expression is true or non-nil, it will return falseValue
Dragon53535 #9
Posted 17 October 2013 - 04:18 PM
I would add for those it concerns, the elseif statement.