Skip to main content

IF...THEN

Syntax

IF expression THEN statement(s) [ELSE statement(s) ]         IF expression THEN     statement(s) [ELSE]     statement(s) [ELSEIF expression THEN]     statement(s) ENDIF

Description

IF...THEN is a way to make decisions. It is a mechanism to execute code only if a condition is true, and can provide alternative code to execute based on more conditions.

The syntax allows single line IF..THEN, or multi-line versions that end with ENDIF.

The single line version only allows simple statements.  To use nested IFs the multi-line version must be used.

Example

'e.g. here is a simple "guess the number" game using if...then for a decision.

PRINT "guess the number between 0 and 10"

DO 'Start a loop
DEBUGIN "guess"; y 'Input a number from the user

IF x = y THEN

PRINT "right!" 'He/she guessed the right number!

EXIT
ELSEIF y > 10 THEN 'The number is higher then 10

PRINT "The number cant be greater then 10! Use the force!"

ELSEIF x > y THEN

PRINT "too low" 'The users guess is to low

ELSEIF x < y THEN

PRINT "too high" 'The users guess is to high

ENDIF

LOOP 'Go back to the start of the loop

Differences from other BASICS

  • none

See also