Lesson 4

Conditionals

Learn how to branch your code and make choices with conditionals.

Loading...

If statements

If you consider variables to be the skeleton of coding, then if statements are the nervous system.

They route the code down branching pathways and allow you to have things act differently based on the conditions at the time.

Let’s look at the structure of a simple if statement:

if (hp == 100) {
    message = "Full hp!";
}

We’ve got a couple of things going on here. First, there’s obviously the word if, which triggers the conditional check. Then we have the expression sitting inside parentheses: (hp == 100).

If you remember BOMDAS or “order of operations” from maths, parentheses let us group expressions together and decide the order in which conditions are evaluated.

Since there’s only one expression here, it’s simple enough that it doesn’t really need parentheses, but it’s useful to get in the habit of including them anyway.

Parentheses Optional?

GM will absolutely let you write conditionals without parentheses, but if you don’t include them, you are putting your coding life in the hands of the compiler correctly guessing what your intention is…


Better to just include them ;)

After that we have the opening curly bracket {.

This is telling the computer “The following code should only be executed if the result of the conditional is true”. So the code message = "Full hp!" will only get run if hp == 100 returns true.

Then finally we have the closing curly bracket }. This says “Ok, we’ve finished with this conditional, continue on with normal code flow.”

Curly Brackets Optional?

GM will also let you write conditionals without curly brackets, with one very important caveat. Without curly brackets only the very next line of code is considered to be a part of the if statement.


So if you have multiple lines of code that are all meant to be inside the if statement, you must use curly brackets to let the computer know which lines of code you are intending the if statement to hold.


Like with the parentheses, I recommend just always including curly brackets to make things more declarative and obvious.

Put all of that together, and you have a way to execute arbitrary code based off decisions about the state of the program. It’s what allows games and programs to be dynamic, and respond to situations differently depending on the circumstances.

An enemy in a game decides to throw a grenade at you? At its core, that’s boiling down to an if statement.

The title screen shows “Play” if you’ve never played the game before and “Continue” if you’ve got an active save file? Again, an if statement is what’s driving that.

Or else

The complement to an if statement is an else statement. If you are using an else it must always follow an if, and it essentially says “If the if statement does not execute, execute this code instead”. It looks like this:

if (hp == 100) {
    message = "Full hp!";
}
else {
    message = "You've been damaged!";
}

It follows the same curly bracket rules as the if, except it doesn’t need an expression, as it already knows when it should execute it’s code: whenever the if statement doesn’t execute its.

But what if else if?

To cap it off, we have the else if statement. This sits between an if and an else statement and is essentially a “second try” to see if another expression is true before the fallback to the else:

if (hp == 100) {
    message = "Full hp!";
}
else if (hp > 0) {
    message = "Wounded!";
}
else {
    message = "Dead!";
}

Like all code, this runs sequentially, line by line.

It first checks to see if hp is equal to 100 for the initial if statement. If that check fails (say, hp is 99), then it will check the else if statement, determining if hp is greater than 0.

Since we are pretending it’s 99, that means the else if statement would execute it’s code, and the following else statement would be skipped. If hp was 0 or less, then both the if statement and the else if statement would be skipped, and the else statement would end up triggering its code.

As an aside, if hp was 101, for example, the else if statement would also trigger, since hp isn’t 100, but it is above 0.

You can chain else if statements together: if...else if...else if...else if...else, if you have many different conditions you might want to check for:

if (hp == 100) {
    message = "Full hp!";
}
else if (hp > 50) {
    message = "Ok!";
}
else if (hp > 0) {
    message = "In the danger zone!";
}
else {
    message = "Dead!";
}

Together, those three pieces, if...else if...else, make up conditionals and give you complete control over what kind of decisions you might want to make while your code is running.

Boolean Expressions

An easy time saver when dealing with expressions is letting boolean logic take over.

if (is_alive == true) {

Is the same as

if (is_alive) {

Since is_alive is either true or not, we don’t need to write the whole comparison, we can just let the conditional evaluate the actual value of the variable.


And we can take advantage of the not logical operator to have the conditional trigger if it’s false:

if (!is_alive) {

That conditional will only trigger if is_alive is false.


And because GM is “truthy” as we learned in Expressions and Conditions, we can let it decide whether a variable is truthy or not, even if the variable itself isn’t holding a direct boolean value. Although this isn’t commonly used.