Skip to content
Lesson 3 of 6

Step 1 of 5 · Reading · ~4 min

Learn

Control Flow

If/Then and Loops

Programs get interesting when they decide and repeat. BASIC gives you IF for decisions and three loop families for repetition — plus a couple of sharp edges the grader will happily expose.

IF / THEN / ELSE

DIM age AS INTEGER = 15

IF age >= 18 THEN
    PRINT "adult"
ELSEIF age >= 13 THEN
    PRINT "teen"
ELSE
    PRINT "child"
END IF

The block ends with END IF — BASIC uses words where C uses braces. Conditions compare with =, <> (not equal), <, >, <=, >=. Note that equality is a single = — there is no ==, and typing one is a syntax error. Combine conditions with AND, OR, NOT.

There is also a compact single-line form — no END IF on this one:

DIM x AS INTEGER = 5
IF x > 0 THEN PRINT "positive"

The classic mistake is mixing the forms: writing the single-line version and an END IF, or starting a block IF and forgetting END IF. If the compiler complains near END IF, check which form you meant.

SELECT CASE

Comparing one value against many candidates? SELECT CASE reads better than an ELSEIF ladder — and it supports ranges:

DIM grade AS STRING = "B"

SELECT CASE grade
    CASE "A"
        PRINT "excellent"
    CASE "B" TO "D"
        PRINT "passing"
    CASE ELSE
        PRINT "failed"
END SELECT

FOR / NEXT

DIM i AS INTEGER
FOR i = 1 TO 5
    PRINT i
NEXT i

A FOR header and its body are separate lines. Everything between FOR and NEXT is the body; you cannot write the whole loop on one line the way C lets you. Writing FOR i = 1 TO n result = result * i NEXT i is the single most common compile error on this lesson — the compiler reports Expected End-of-Line on the FOR line, then NEXT without FOR further down. And a FOR line that is commented out still leaves its NEXT behind, which produces exactly that second error on its own.

Three rules carry the rest of the weight:

  1. The end bound is inclusive. FOR i = 1 TO 5 runs five times: 1, 2, 3, 4, 5. Programmers coming from C expect < 5 behavior and lose an iteration.
  2. STEP sets the stride. FOR i = 10 TO 1 STEP -1 counts down.
  3. A loop can run zero times. With a positive step, if the start is already past the end, the body never executes: FOR i = 1 TO 0 does nothing at all. Remember this — it is the secret to your exercise.

The accumulator pattern

Most loop work is "combine many values into one." The idiom: seed a variable before the loop, update it inside:

DIM total AS INTEGER = 0
DIM i AS INTEGER
FOR i = 1 TO 5
    total = total + i
NEXT i
PRINT STR(total)     ' 15

Two traps live here:

  • Wrong seed. Sums start at 0 — but products start at 1. Seed a running product with 0 and every multiplication stays 0 forever.
  • PRINT inside the loop. That outputs every intermediate value instead of one final answer. Print once, after NEXT.

Note the STR() around total in that example. A bare PRINT total would write " 15" with a leading space — the sign column from the previous lesson — and the grader compares that against 15 and fails you. Every numeric answer in this course goes out through STR().

DO / LOOP — when you don't know the count

DIM n AS INTEGER = 1
DO WHILE n < 100
    n = n * 2
LOOP
PRINT STR(n)         ' 128

DO WHILE tests before each pass; DO ... LOOP UNTIL cond tests after the pass, so it always runs at least once. EXIT DO (or EXIT FOR) bails out early. The old WHILE ... WEND form exists too; DO/LOOP does everything it does and more.

Why the starter says LONGINT

Factorials explode: 12! still fits in a 32-bit integer, 13! already does not. The starter declares result AS LONGINT — a guaranteed 64-bit integer that holds factorials up to 20!. Just use it; multiplying an INTEGER into a LONGINT works fine.

Your exercise

Compute N! — the product of every whole number from 1 up to N. The starter reads n, seeds result, and already prints the answer through STR(); the loop between them is yours. The grader catches, in order of popularity:

  • Re-seeding with 0 (or shadowing result with your own variable): the output becomes 0 for every input.
  • Off-by-one: stopping at n - 1 prints 24 for input 5 where 120 is expected.
  • Special-casing N = 0. Don't. A FOR whose start is already past its end never runs, result keeps its seed, and the 0 -> 1 test passes with zero extra code. An added IF n = 0 THEN PRINT 1 emits a second line of text and fails.
  • Printing inside the loop. The grader wants one line — 120 — not the running products.
Up nextSubs and FunctionsSubs, Functions, Arrays

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…