IF keyword

Executes a statement or statement block depending on specified conditions. The use of THEN and END IF are required. The use of ELSE is optional.

Syntax

IF condition THEN
  'statementbloc-1
ELSE
  'statementbloc-2
END IF

Parameters

  • condition: Any expression that can be evaluated as TRUE or FALSE (required)
  • statementbloc-1: One or more statements on one or more lines (required)
  • statementbloc-2: One or more statements on one or more lines; ELSE block is optional

Example

IF totalPoints = 100 THEN
totalPoints = 0
END IF

IF totalPoints = 100 THEN
TEXT "You reached 100 points."
ELSE
totalPoints = totalPoints + 5
TEXT "You got 5 more points."
END IF

IF COLLIDE(object1, object2) THEN
'...
END IF

IF NOT COLLIDE(object1, object2) THEN
'...
END IF

IF VALUE(object1) = "my value" THEN
'...
END IF

IF myName = "Roger" AND points = 2 THEN
'supports unlimited number of conditions, can use AND or OR
END IF

IF myName = "Roger" OR myName = "John" THEN
'...
END IF

IF TRUE THEN
'Always executed
ELSE
'Never executed
END IF

DIM isValid AS BOOLEAN: isValid = TRUE
IF isValid THEN
'using a boolean variable in the condition
END IF

IF 1 < 2 THEN
'compare numbers with =, <, >, <=, >=, <>
END IF

Go back to list of Keywords.