Beginner

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
DIM age AS INTEGER
age = 21

IF age < 13 THEN
    PRINT "Child"
ELSEIF age < 20 THEN
    PRINT "Teenager"
ELSEIF age < 65 THEN
    PRINT "Adult"
ELSE
    PRINT "Senior"
END IF

' Single-line IF is also valid
IF age >= 18 THEN PRINT "Can vote"

Notes:

  • Conditions return -1 (true) or 0 (false) in classic BASIC — FreeBASIC follows this convention.
  • = is used for both assignment and equality comparison; context tells them apart.
  • ELSEIF is 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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
DIM grade AS INTEGER
grade = 87

SELECT CASE grade
    CASE IS >= 90
        PRINT "A"
    CASE 80 TO 89
        PRINT "B"
    CASE 70 TO 79
        PRINT "C"
    CASE 60, 61, 62, 63, 64, 65, 66, 67, 68, 69
        PRINT "D"
    CASE ELSE
        PRINT "F"
END SELECT

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
DIM i AS INTEGER

' Count up
FOR i = 1 TO 5
    PRINT "Up:"; i
NEXT i

' Count down with STEP
FOR i = 10 TO 2 STEP -2
    PRINT "Down:"; i
NEXT i

' Sum the first 10 integers
DIM total AS INTEGER
total = 0
FOR i = 1 TO 10
    total = total + i
NEXT i
PRINT "Sum 1..10 ="; total

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
DIM n AS INTEGER

' WHILE / WEND — checks condition at the top
n = 1
WHILE n <= 16
    PRINT "n ="; n
    n = n * 2
WEND

' DO WHILE / LOOP — same idea, modern form
DIM countdown AS INTEGER
countdown = 3
DO WHILE countdown > 0
    PRINT "T-minus"; countdown
    countdown = countdown - 1
LOOP
PRINT "Liftoff!"

' DO / LOOP UNTIL — condition at the bottom, runs at least once
DIM guess AS INTEGER
guess = 0
DO
    guess = guess + 7
    PRINT "Try"; guess
LOOP UNTIL guess >= 20

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
DIM i AS INTEGER

' Find the first multiple of 7 above 50
FOR i = 1 TO 100
    IF i > 50 AND (i MOD 7) = 0 THEN
        PRINT "Found:"; i
        EXIT FOR
    END IF
NEXT i

' Print only odd numbers from 1..10
FOR i = 1 TO 10
    IF (i MOD 2) = 0 THEN CONTINUE FOR
    PRINT "Odd:"; i
NEXT i

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Pull the FreeBASIC image
docker pull primeimages/freebasic

# Compile and run conditionals.bas
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc conditionals.bas && ./conditionals"

# Compile and run select_case.bas
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc select_case.bas && ./select_case"

# Compile and run for_loop.bas
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc for_loop.bas && ./for_loop"

# Compile and run while_loops.bas
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc while_loops.bas && ./while_loops"

# Compile and run loop_control.bas
docker run --rm -v $(pwd):/code -w /code primeimages/freebasic \
    sh -c "fbc loop_control.bas && ./loop_control"

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 ELSEIF and END IF is preferred over chains of single-line IFs.
  • 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; STEP can 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/NOT to build compound conditions.
  • Classic BASIC used GOTO and line numbers for control flow — modern dialects make that almost never necessary.
Last updated:

Comments

Loading comments...

Leave a Comment

2000 characters remaining