Conditional Structures
if condition then
block
end
That means: If the condition will become true (only then) execute the block.
Now we have a frame in general and we only need a real condition and an according block.
Let us create a condition.
player one is alive --> player1lives == true
Followed by the block (as we want to see what happens, we use the function print)
print that the player is alive --> print ("The player is alive")
Now we combine altogether.
if player1lives == true then
print ("The player is alive")
end
Easy to use, looking at it step by step. Now we want to check this also in the editor.

What have we done?
We compare the variable player1lives with the value true and as operator for comparision we use "==" (is something equal to another)
That means: is the value of player1lives equal to the value true?
If we take the operator for comparision "~=" (is something not equal to another) than we do not get a result printed (because player is still living)
Let's have a look at the picture. All lines are aligned on the left side of the window, only the block is indented. Wy that?
Quite simple.
The reason is a better overview. If we would not do this, our script would get hardly to read when it gets longer.
If I would have written the above explanation as follows:
Conditional Structures
if condition then block end
if condition then block1 else block2 end
if condition1 then block1 elseif condition2 then block2 elseif condition3 then block3 else block4 end
then would get into troubel to read and understand this.
What do we learn from this? From now on we indent every block.
To go on with conditional structures
if condition then
block1
else
block2
end
We already know all of this but not the word else. That means, that if something is not as expected than execute another block
worded:
- if the condition is true then execute block1.
- if the condition is not true (false) then execute block2.
Now we join altogether and will try it.
- We set player1lives = false --> test it
- Then we change the operator of comparision --> try it again
if condition1 then
block1
elseif condition2 then
block2
else
block3
end
If would know the elesif then would have learned all necessary received words.
elseif means that if something is not true then check another condition.
At first we have to remind of that the programm is executing step by step.
- Is condition1 true? --> block1
- If not, is condition2 true? --> block2
- If these conditions are false then --> block3
Condition1 must not have a relationship to condition2. At first we can check if player1 is alive, followed by the question e.g. if Angie does have red hair. It does not matter at all what the conditions are alike.
- Does condition1 applies to...? --> block1
- If not, does condition2 applies to...? --> block2
- If not, does condition3 applies to...? --> block3
- If not, does condition4 applies to...? --> block4
- If not, does condition5 applies to...? --> block5
- and so on
All these types of queries do have in common: if any condition applies to an according query, the according block will be executed and the query will be finished. All adjacent conditions will not be checked or executed.
An according exercise:
Some examples of conditions:
- Does player1 have more gold than player2? --> operator of comparision ">"
- Does player1 have less gold than player2? --> operator of comparision "<"
- Does player1 have the equal name to player2?
- Is Robertico the name of player1?
We will stay at the function print (something).
We will exercise........(creating more conditions) (usage of elseif)
Let us try to check if player1 has more or less stone as player2?
if player1Stone > player2Stone then
We get an error: attempt to compare number with nil
--> a trial to compare a number with nil.
It seems that this kind of comparision does not work. So we have to think about another way.
As we learned above, if a variable does not exist we will get nil after checking this variable. This circumstance we want to use for our benefit.
At first we ask if the variable player1Stone is existing. If this is true we check if player1Stone does have more or less stone as player2. We do this for the first with a block inside another block. Let me help a little bit.
if player1Stone ~= nil then
-- block 1
if player1Stone > player2Stone then
-- block 2
print ("Player 1 has more stones than player 2")
end
end
and for a better overwie: each line accordingly indented
If the variable is existing (regardless of its value) the condition is
Player1Stone = nil --> true. The variable is existing.
Then we will get into the first block.
To get an according output we have to give more stones to player 1 than player 2. However, a good exercise would be to convert the query.
- Does Player 1 have stones
- If yes, does he have more
- does he have equal to
- or less (else)
- if he has not any stones --> print (else)
we can write: If player1Stone then
With this syntax it will be checked if the variable exists or if this variable is true (of type "boolean")
However, a number of value 0 is also existing and true. An empty string is also existing although it is empty and so on.
Why didn't we get this information earlier? We could have saved a lot of work.
if player1lives == true then
print ("Player 1 is alive")
end
if player1lives then
print ("Player 1 is alive")
end
Just accept this. ;)
Copying of any content of this site (text or graphics) is not allowed, excepted any source code shown in this tutorial. See also: ....Disclaimer
Copyright © Robert Schmitz 2006
Copyright © Robert Schmitz 2006