Conditions

<< Functions | Lua | Loops >>

Conditions are used to check if a certain situation occurs. For example conditions can execute a piece of code that breaks a loop.

Conditions: example

if (false) then print("this will never be printed") end
if (true) then print("this will always be printed") 
elseif (true) then print("this will never be printed")
else print("this neither") end
if (3+4<10) then print("this is always printed too") end 

The example shows how a simple condition is build:

 if ( your condition ) then your code end
or
 if ( your condition ) then your code 
 else your code that is executed if the
      condition was false end
or
 if ( your condition ) then your code 
 elseif ( another condition ) then 
    code that is executed if the first 
    condition was false and the second was true 
 else your code that is executed if the conditions
       were false end
Any if or elseif is followed by a then and is finished by a else or end. An else is appended to a if or elseif and must be followed by end.

Of course you can nest as many ifs inside each other as you like:

Example

 if (myhatisonfire) then 
  if (theresnowater) then 
    jumponmyhat()
  else
    throwhatinwater()
  end
  takehatbackonhead()
else
  idle()
end
This shows that if you use plain and clear variable names, anybody will understand the code you write - even if not a programmer (well mostly :) ). You should also use functions instead of writing hundreds of lines of code in one condition. This also makes everything clearer and you will understand your code also after some time has passed.