-
Notifications
You must be signed in to change notification settings - Fork 0
Language reference ‐ Flow Control Statements
Flow control statements define the order in which the instructions of a program are executed. They allow a program to make decisions, repeat actions, jump to other parts of the code, or stop running entirely. By using flow control, even a simple program can react to different conditions, reuse common code blocks, and return results to whatever started it.
In FBasic, the main flow control statements are: IF, GOTO, GOSUB, CALL, CHAIN, EVAL, HALT or END, and RESULT. The IF, GOTO, and GOSUB statements already have their own dedicated documentation pages, so they are simply listed here.
See: Language-Reference,-Flow-Control-IF-Statement
Example:
input age
if age 18 then
print You are an adult
else
print You are not an adult
endifSee: Language-Reference,-Flow-Control-GOTO-Statement
Example:
let x=0
addOne:
let x=x+1
print x
if x<5 then goto addOneSee: Language-Reference,-Flow-Control-GOSUB-Statement
Example:
gosub ShowMessage
print "Back in main program"
halt
ShowMessage:
print "This is a subroutine"
returnThese three statements are related because they all involve running other code from within your current program. In simple words, they let your program “reach out” to external routines or pieces of text instead of doing everything inside a single, long script. This helps you reuse code, split big tasks into smaller programs, and even build simple scripting systems where some logic comes from variables or user input.
- CALL: Executes a program that is already available to the interpreter (for example, provided by a library or by the host system). It is like asking a helper to perform a specific job and return control to your program when finished. The program is executed in a complete different and isolated environment. The RESULTVALUE of the call program takes the value of the sub-program on the end.
- CHAIN: Run in the current program environment another FBasic program. All the variables, collections, arrays etc are common in both programs. The labels and the loops are private to each one and the code author is responsible not to mix them.
- EVAL: Takes a text value that contains FBasic code and executes it as if it was written directly in the program. This allows highly dynamic behavior, because your program can build or receive code at runtime and then run it. Acts with the same ways with CHAIN statement and have the same points of attention.
The difference between CHAIN and EVAL is that CHAIN retrieves the code from a file via the file handler mechanism (similar to CALL statement) and EVAL gets the code from a Value or a Variable.
Example using CALL:
let X=1
call "addOneToX.bas" 'same as call "addOneToX"
print Xoutput value: 1
Example using CHAIN:
let X=1
call "addOneToX.bas" 'same as call "addOneToX"
print Xoutput value: 2
Example using EVAL:
```basic
let X=1
eval "let X=X+5"
print Xoutput value: 6
The HALT or END statement stops the execution of the current FBasic program and returns control to the environment that started it (for example, another program, a scheduler, or a user interface). Use it when your program has finished its work or when you detect a situation where it should not continue. Both HALT and END are treated as commands to terminate the program.
Example:
print "Program is starting"
input x
if x=0 then
print "Nothing to do, stopping"
halt
endif
endRESULT is used to return a value from the current program to whoever called it. The value that you pass to RESULT is stored in a special variable named RESULTVALUE, and it also becomes part of the program’s execution result. You can use several RESULT statements in one program; the last one executed decides the final value. This is especially useful when a higher-level process runs many FBasic scripts and needs to know whether each script finished successfully or with some specific status code.
See more information about RESULT at Chapter Identifiers - Special Purpose Variables - Variable RESULTVALUE
Simple example:
' Program that always returns 0 (success)
result 0
endConditional example:
input amount
if amount>100 then
print "Large amount"
result 1 ' 1 means “large”
else
print "Normal amount"
result 0 ' 0 means “normal”
endif
print "The last result is";
print RESULTVALUE
end