The "If" Statement

This section outlines how to use the "if" statement

A simple "if" statement takes this form:

if (<expression>) <statement>


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

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:

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:

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



Back: GML Overview
Next: The "Repeat" Statement
© Copyright YoYo Games Ltd. 2018 All Rights Reserved