Skip to content

Language Reference, Flow Control IF Statement

Andreas AFENTAKIS edited this page Oct 12, 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 only one form for the IF statement, with the Block Form being highly recommended for structured code.

Block Form

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.

NOTE: Single line for (for signle command) it is not supported. Each IF should have a corresponding ENDIF statement.

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. ELSE (Optional): The ELSE block is optional. If the condition is false and the ELSE block is omitted, execution continues directly after the ENDIF.

  4. 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).

Block Form Example

This example demonstrates 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

Clone this wiki locally