Skip to content

Latest commit

 

History

History
72 lines (53 loc) · 3.09 KB

c6.md

File metadata and controls

72 lines (53 loc) · 3.09 KB

Chapter 6: Conditions with "if"

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.

How do you use "if"?

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.

Expressions

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.

Difference between = and ==

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

Else-block

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.

Continuing from the previous chapter

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.

SNIPPET

release = 0 if release < 16 then release += 1 goto 2 end goto 3

This code will execute the following steps:

  • Set release to 0
  • Check if release is smaller than 16
  • If release is smaller than 16, increase release by 1 and return to the start of the line with goto 2
  • After the if-clause is resulting in false, goto 3 -> release will 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