IF

IF C

EXECUTES THE NEXT STATEMENT OR THE NEXT BLOCK OF STATEMENTS IF THE CONDITION C IS TRUE

  • Type: Command
  • Parameter: 1
  • Group: Structures

ACTION

The command IF uses the result of the logical condition (True or False) to execute or not a statement or a block of statements.

The "condition" is a single logical expression (built with comparators >, <, >=, <>, =, ...) or a complex expression combined with logical operators AND, OR, NOT and parenthesis. Only the result (True or False) is used to execute or not the following statements.

There are 3 possibilities to use IF :

First case :

IF Condition THEN Statement(s)

In this case, if condition is true, the statement or the block of statements after THEN are executed. Otherwise, the execution jumps to the next line.

This case is used when there are a few statements to be executed when condition is true, which can be written on a line.

Second case :

IF Condition

Command(s)

END_IF

In that case, if condition is true, all the statements till END_IF are executed. Otherwise, the execution jumps to the statement just after END_IF.

That case is used when there are a lot of statements to be executed when condition is true, and which cannot be written on a single line.

Third case:

IF Condition

Statement(s)

ELSE

Statement(s)

END_IF

In that case, if the condition is true, the statement or the block of statements between IF and ELSE is executed. Otherwise, the statement or the block of statements between ELSE and END_IF is executed.

SYNTAX: IF C

  • SYNTAX 1 :
  • IF C THEN Statement(s)
  • C: logical expression.
  • SYNTAX 2 :
  • IF C
  • Statement(s)
  • END_IF
  • SYNTAX 3 :
  • IF C
  • Statement(s)
  • ELSE
  • Statement(s)
  • END_IF
  • C: logical expression.

USE

  • IF is used to make a choice : execute or not statements depending on a logical condition.

ERROR

  • If C is not a correct logical expression.

EXAMPLE

dim a,b

a=1:b=2

if b>a then print "b is greater than a"

SEE ALSO

THEN

ELSE

END_IF

WHILE

END_WHILE

REPEAT

UNTIL

AND

OR

NOT