-
Couldn't load subscription status.
- 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 two one forms for the IF statement, the Incline Form and the Block Form where being highly recommended for structured code.
IF expr THEN 1-STATEMENT
This form ensure one command execute if the expression is true. The inline form does not support ELSE statement.
IF expr THEN
n-STATEMENTS
ELSE
n-STATEMENTS
ENDIFor
IF expr THEN
n-STATEMENTS
ENDIFThis 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.
-
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. -
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.
-
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 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"
If 1=1 Then
If 2=3 Then
assert 0
EndIf
Else
assert 0
EndIf