Warning

You're browsing the old v2.x documentation. This version is no longer maintained. Click here to go the v3.x documentation.

This is an old revision of the document!


IF ... THEN ... ELSE

Syntax:

if <any relation> [and | or <any relation>] then <statements> [else <statements>]

or, since version 2.1:

if <any relation> [and | or <any relation>] then
  <statements>
[else
  <statements>]
endif

Conditional structure. Executes the statements after THEN if the expression evaluates to true, otherwise the statements after ELSE, if present. ELSE is optional.

Current limitations:

  • Only one conditional operation (AND/OR) is supported
  • THEN may not be omitted

Examples:

if x >= y/z then print "yes, expression is true"
if a = b then print "they are equal" else print "they are not equal"
if a = b or a < 2 then print "they are equal or a is less than two"

Please refer to the Operators page for the list of supported operators.

Since version 1.2, multiple statements can be written in one line using the statement separator character (:). This, in combination with the line concatenation character allows you to write multi-line IF … THEN blocks:

if x = y then ~
	print "the numbers are equal" : ~
	print "whatever else here" : ~
	eq = 1 ~
else ~
	print "the numbers are not equal" : ~
	eq = 0
	

The above is technically a single line in XC=BASIC.

Note that no other characters are allowed after the ~, not even a whitespace!

Performance tip

An ON … GOTO or ON … GOSUB construct is faster than its IF … THEN GOTO/GOSUB equivalent. Please refer to the ON construct for details.