The "If" Statement

This section outlines how to use the "if" statement

A simple "if" statement takes this form:

GML Code Example

if (<expression>) <statement>


or it can have the slightly more complex "if... else..." form:

GML Code Example

if (<expression>) <statement> else <statement>


In this case the expression will be evaluated, and if the (rounded) value is < = 0 (false) the statement after else is executed, otherwise (true) the other statement is executed. It is a good habit to always put curly brackets around the statements in the if", and take a new line in the block for each statement, so the end code will have this form:

GML Code Example

if (<expression>)
    {
    <statement>
    ...
    <statement>
    }
 else
    {
    <statement>
    }
 


As a small example, consider the following code which will move the instance towards the position x=200 in the room:

GML Code Example

if (x < 200)
    {
    x += 4;
    }
 else
    {
    x -= 4;
    }
 






No Examples Submitted

Does this page need better examples? Be the first to Submit

No Edits Suggested

Is this page unclear or not descriptive enough? Suggest an edit to the page and with enough upvotes your changes will be made.

No Questions Have Been Ask

Do you have a question about this page? Ask it Here