Control Flow in BASIC
Learn conditionals, loops, and branching in BASIC with IF/THEN/ELSE, FOR, WHILE, DO, and SELECT CASE statements
Control flow is what turns a list of statements into a real program. BASIC was designed to make these constructs easy to read — even decades later, an IF...THEN or FOR...NEXT block reads almost like English.
Classic BASIC relied heavily on line numbers and GOTO for branching, but modern dialects like FreeBASIC support structured constructs: block IF, SELECT CASE, FOR, WHILE, and DO...LOOP. This tutorial focuses on the structured forms while showing the classic equivalents where they still appear in old code.
By the end, you’ll be able to make decisions, repeat work, and exit loops early using idiomatic BASIC.
Conditionals: IF / THEN / ELSE
The block IF is the workhorse for branching. It can stand alone, chain via ELSEIF, and close with END IF.
Create a file named conditionals.bas:
| |
Notes:
- Conditions return
-1(true) or0(false) in classic BASIC — FreeBASIC follows this convention. =is used for both assignment and equality comparison; context tells them apart.ELSEIFis one word.
SELECT CASE
When you have many discrete possibilities, SELECT CASE is clearer than a long ELSEIF chain. BASIC’s CASE is unusually expressive — it supports ranges and lists directly.
Create a file named select_case.bas:
| |
The TO keyword expresses an inclusive range, IS introduces a comparison, and a comma-separated list matches any of the listed values. There is no fall-through — only the first matching case runs.
FOR Loops
FOR...NEXT is the canonical counted loop. It supports a STEP clause for non-unit increments (including negative values).
Create a file named for_loop.bas:
| |
The loop variable name after NEXT is optional in FreeBASIC but makes nested loops easier to read.
WHILE and DO Loops
For loops with an unknown trip count, BASIC offers WHILE...WEND (legacy) and the more flexible DO...LOOP family.
Create a file named while_loops.bas:
| |
DO...LOOP UNTIL is the post-test variant: the body always executes at least once. Choose WHILE at the top for “maybe zero iterations” and UNTIL at the bottom for “at least one.”
Early Exit and Continue
To leave a loop early use EXIT FOR, EXIT WHILE, or EXIT DO. To skip to the next iteration use CONTINUE FOR, CONTINUE WHILE, or CONTINUE DO.
Create a file named loop_control.bas:
| |
MOD is the integer remainder operator, and AND is a logical (and bitwise) operator in BASIC.
Running with Docker
Each program compiles to a separate executable. Pull the image once, then compile and run each file.
| |
Expected Output
Running conditionals:
Adult
Can vote
Running select_case:
B
Running for_loop:
Up: 1
Up: 2
Up: 3
Up: 4
Up: 5
Down: 10
Down: 8
Down: 6
Down: 4
Down: 2
Sum 1..10 = 55
Running while_loops:
n = 1
n = 2
n = 4
n = 8
n = 16
T-minus 3
T-minus 2
T-minus 1
Liftoff!
Try 7
Try 14
Try 21
Running loop_control:
Found: 56
Odd: 1
Odd: 3
Odd: 5
Odd: 7
Odd: 9
Key Concepts
- Block IF with
ELSEIFandEND IFis preferred over chains of single-lineIFs. - SELECT CASE supports ranges (
CASE 80 TO 89), comparisons (CASE IS >= 90), and value lists — no fall-through between cases. - FOR…NEXT is for counted iteration;
STEPcan be negative for counting down. - DO…LOOP is more flexible than
WHILE...WEND: choose top-checked (DO WHILE) or bottom-checked (LOOP UNTIL) variants. - EXIT and CONTINUE work on any loop type with the loop’s keyword suffix (
EXIT FOR,CONTINUE DO, etc.). - MOD gives the integer remainder; combine with
AND/OR/NOTto build compound conditions. - Classic BASIC used
GOTOand line numbers for control flow — modern dialects make that almost never necessary.
Comments
Loading comments...
Leave a Comment