-
Notifications
You must be signed in to change notification settings - Fork 0
Language Reference, Flow Control IF Statement
The IF statement is fundamental for controlling the flow of program execution by allowing code to run only when a specified condition is true.
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.
FBASIC supports only one form for the IF statement, with the Block Form being highly recommended for structured code.
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.
-
Condition: The
conditionmust be an expression that evaluates to a numeric or boolean result (e.g., comparison operators like=,<, or>). -
THENPlacement: TheTHENkeyword must be on the same line as theIFstatement. The commands to be executed start on the subsequent line. -
ELSE(Optional): TheELSEblock is optional. If the condition is false and theELSEblock is omitted, execution continues directly after theENDIF. -
Block Termination: Every
IFstatement block, regardless of whether it includes anELSEclause, must be explicitly closed with the single keywordENDIF(no space betweenENDandIF).
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
If 1=1 Then
If 2=3 Then
assert 0
EndIf
Else
assert 0
EndIf