We use them to decide whether we should get the one or the other. We use them to tell between good and bad, increasing and decreasing, open and closed. Comparisons allow us to check if one value is equal, greater or less than some other value.
The syntax for any condition you'd want to check is the following:
if <expression> then <do something> (else <do something else>) end
Let's walk through this if-clause step by step.
Every condition requires something called an expression. An expression is nothing more than a comparison between
two fields, numbers, strings or a mix of those.
One expression might for example be: 5 == 6. Naturally in other programming languages this expression would
return a boolean value of false. In YOLOL however boolean values are treated as 1 (= true) and 0 (= false).
As mentioned, you can mix values used in expressions up. You could also compare a predefined field to another
field, string or number:
x = 12
if x == 12 then <do something> end
In this example x is set to 12 and compared to a number of the same value. Because they hold the same value
the following <do something> block will be executed. This block can hold a simple goto, an assignment like
x = 13 or even another if-clause.
As you might have noticed I used == to compare two numbers, but used = to set x to 12.
There generally is a huge difference between the two!
You use == solely to compare a value or field to some other value or field. It will always return a number -
either 0 or 1.
= however is used to assign something to a field. This can be:
- the result of a comparison ->
a = 1 == 1 - another field ->
a = b - a number or string ->
a = "YOLOL sucks!" b = 16
An else-block will always be executed if the initial if-block was not executed. For example:
if x == 5 then <do somehting> else <do something else> end
Here, the <do something else> will only be executed if x is greater or smaller than 5. Besides that the
else-block can hold the same content as an if-block - assignments, a goto or another if-clause.
Last chapter we went over loops and increasing fields. However we could not limit the loop to only increase a field up to a certain value. With an if-clause this is easily done as you can see in the next example.
release = 0 if release < 16 then release += 1 goto 2 end goto 3
This code will execute the following steps:
- Set
releaseto0 - Check if
releaseis smaller than16 - If
releaseis smaller than16, increasereleaseby 1 and return to the start of the line withgoto 2 - After the if-clause is resulting in false, goto 3 ->
releasewill no longer be increased
And that's how a simple for-loop can be created from the very limited resources we have available in YOLOL.
More use cases for if-clauses combined with goto in the next chapter!
| Previous Chapter | Next Chapter |
|---|