Reading — step 1 of 5
Learn
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
Three rules carry all the weight:
- The end bound is inclusive.
FOR i = 1 TO 5runs five times: 1, 2, 3, 4, 5. Programmers coming from C expect< 5behavior and lose an iteration. STEPsets the stride.FOR i = 10 TO 1 STEP -1counts down.- 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 0does 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 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.
DO / LOOP — when you don't know the count
DIM n AS INTEGER = 1
DO WHILE n < 100
n = n * 2
LOOP
PRINT 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
Read N and compute N! (factorial) with a FOR loop: multiply result by every i from 1 to N. The starter already seeds result = 1 and prints it at the end — you write the loop. The grader catches, in order of popularity:
- Re-seeding with 0 (or shadowing
resultwith your own variable): output becomes0for every input. - Off-by-one:
FOR i = 1 TO n - 1prints24for input 5; the expected output is exactly120. - Special-casing N = 0. Don't.
FOR i = 1 TO 0never runs,resultstays 1, and the0 -> 1test passes with zero extra code. An addedIF n = 0 THEN PRINT 1emits a second line and fails. - Printing inside the loop. The grader wants one line —
120— not the running products.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…