Skip to content

Language Reference, Flow Control IF Statement

Andreas AFENTAKIS edited this page Oct 13, 2025 · 5 revisions

FBASIC Flow Control: IF...THEN...ELSE...ENDIF

The IF statement is fundamental for controlling the flow of program execution by allowing code to run only when a specified condition is true.

Purpose

The IF...THEN...ELSE...ENDIF structure evaluates a boolean expression and executes one block of code if the expression is true and an alternative block of code if the expression is false.

Syntax

FBASIC supports two one forms for the IF statement, the Incline Form and the Block Form where being highly recommended for structured code.

Inline Form

IF expr THEN 1-STATEMENT

This form ensure one command execute if the expression is true. The inline form does not support ELSE statement.

Block Form

IF expr THEN 
   n-STATEMENTS
ELSE
   n-STATEMENTS
ENDIF

or

IF expr THEN 
   n-STATEMENTS
ENDIF

This structure ensures all commands execute within a clearly defined block, adhering to the standard FBASIC format where the THEN is on the same line as the IF condition.

IF condition THEN
    REM Commands to execute if condition is TRUE
    command1
    command2
ELSE
    REM Commands to execute if condition is FALSE (Optional)
    command3
ENDIF
  • The ELSE block is optional.

Behavior and Rules

  1. Condition: The condition must be an expression that evaluates to a numeric or boolean result (e.g., comparison operators like =, <, or >).

  2. THEN Placement: The THEN keyword must be on the same line as the IF statement. The commands to be executed start on the subsequent line.

  3. When after THEN is a new-line, the IF classifed as block statement and the ENDIF is expected. When after THEN follows statement, the IF statements is in-line and just a one statement is expected. In-line IFs does not support ELSE and does not need ENDIF.

  4. ELSE (Optional): The ELSE block is optional. If the condition is false and the ELSE block is omitted, execution continues directly after the ENDIF.

  5. Block Termination: Every IF statement block, regardless of whether it includes an ELSE clause, must be explicitly closed with the single keyword ENDIF (no space between END and IF).

Example 1

This example demonstrates the inline If statement and the block structure, checking a variable and executing different print commands based on the result.

REM Define variables
let score = 85

IF score > 90 THEN
   print "Grade: A"
ELSE
   print "Grade: B or Lower"
ENDIF

if score < 30 print "Score is too low"

Example 2 - Nested IFs

If 1=1 Then
   If 2=3 Then
      assert 0
   EndIf
Else
   assert 0
EndIf
Clone this wiki locally