Give BASIC the starting number and the limit for a count. Use FOR and NEXT
to express the loop we already understand, then choose the gap between numbers.
If the terminal below already shows your earlier BASIC session, press the
browser's RUN button to continue it. You do not need to load the tape again.
This tab keeps the machine, program, terminal, and pending input as you move
between BASIC lessons, visit All lessons, or refresh. Returning always pauses
execution first. Other tabs work independently; a newly opened or duplicated
tab may begin with a copy of this tab's saved session.
Begin a lesson's commands at OK. If your previous program is still asking
a question at ?, finish that conversation first; the ticket loop finishes
when you answer 0. The lesson's setup explains which stored lines to keep
or replace. Typing NEW at OK clears the stored program and variables without
reloading BASIC.
If this is a fresh machine, follow the loading steps below.
This is the original 4K BASIC 3.2 interpreter running on our Altair. An
interpreter is a program that reads your commands and carries them out.
Before it can understand PRINT, the Altair must load that interpreter into memory.
Get the selected paper-tape file from the
machine guide (opens in a new tab).
Return here and choose it in Paper tape below. The file stays on your computer.
Raise only switches A11 and A10. Leave PC at 0000: do not press EXAMINE.
You may then close the front panel to bring the terminal closer.
Press the browser's RUN button. Click Terminal keyboard and answer
each question when it appears:
When you see
Enter
MEMORY SIZE?
Just Enter, to let BASIC find the available memory.
TERMINAL WIDTH?
Just Enter, to keep the default.
WANT SIN?
Y then Enter. We will not need this math function today.
When 727 BYTES FREE and OK appear, BASIC is ready. Leave the processor
running for the lesson. Switching tabs pauses it; press the browser's RUN
button when you return.
If you need to start loading again, use Start fresh, raise A11 and A10 again,
then RUN. This discards the current BASIC program and uses the tape you already
selected. Reset CPU + serial does not reload BASIC.
The saved session stays in this browser tab's temporary storage. It is not
sent to the site. Copy your numbered lines somewhere to keep them beyond the
browser session. If the browser cannot save or restore a session, a message
beside the machine explains that; you can still use the current page.
This tab can keep your BASIC session between lessons. Checking for a saved session…
The file stays on your computer. Selecting a verified tape prepares a fresh, stopped machine.
No tape selected.
Front panel · address, data, and sense switches
To load BASIC, raise only A11 and A10 and leave PC at 0000.
Do not press EXAMINE after raising the loading switches. You can then close this panel to bring the terminal closer.
EXAMINE and NEXT change PC. Only 0000–0FFF contain RAM; other addresses read FF and discard deposits.
Lights show instruction boundaries, not individual bus cycles.
Stopped · 0 instructions
RUN lets the processor execute; STOP pauses it. At BASIC’s prompt, type RUN
to run your BASIC program. Break sends Control-C to BASIC.
0 keyboard bytes queued
Use uppercase commands. Enter sends a line; Backspace sends BASIC’s erase character (_).
Paste one line at a time. Only the machine’s echoes appear above.
Inspect the running machine
Latest twelve instructions on this page · PC before → after (hexadecimal)
Reset keeps RAM and the switches but resets the CPU and serial device. Start fresh discards the program, restores the bootstrap,
rewinds the selected tape, and clears the terminal; raise the switches again before RUN.
Keep the familiar counter
Load or resume BASIC using the instructions above, and begin at OK. If you
continued directly from the previous lesson, keep
its program and skip the setup block below. It should print 1 through 5.
If you are starting here or returning from a different lesson, type NEW and
enter this program. Type one numbered line at a time, pressing Enter and
waiting for its complete echo before the next:
Type, then Enter
NEW
BASIC replies
OK
Type one line at a time, then Enter
10 LET N=1
20 PRINT N
30 LET N=N+1
40 IF N<=5 THEN 20
50 END
Line 10 sets the start. Line 30 changes the count, and line 40 tests whether
to go around again. BASIC has a pair of instructions for this pattern.
Name the start and the limit
Replace lines 10 and 30, then remove line 40 by typing its number alone:
Type one line at a time, then Enter
10 FOR N=1 TO 5
30 NEXT N
40
Keep line 20, which prints N, and line 50, which ends the program. LIST
should now show:
Type, then Enter
LIST
BASIC replies
10 FOR N=1 TO 5
20 PRINT N
30 NEXT N
50 END
OK
FOR N=1 TO 5 sets N to 1 and remembers 5 as the limit for this loop.
Execution then reaches PRINT. At NEXT N, BASIC adds one to N and tests it
against the limit. If the new value is at most 5, execution returns to the
line just after FOR: our PRINT on line 20. Otherwise it continues to END.
Both instructions name N. That tells NEXT which loop it belongs to. The
instructions between FOR and NEXT are called the body of the loop;
here the body contains just PRINT.
RUN the changed program:
Type, then Enter
RUN
BASIC replies
1
2
3
4
5
OK
Type, then Enter
PRINT N
BASIC replies
6
OK
We get the same printed numbers and the same final value of N as before.
NEXT now does the adding and testing. Do not keep the old LET N=N+1 in
the body: it would add another one on top of NEXT's change.
FOR supplies the starting value once. Returning from NEXT goes to the
body, so it does not set N back to 1 on every trip.
Replace only line 10. Which numbers will be printed now?
Type one line at a time, then Enter
10 FOR N=3 TO 5
RUN
BASIC replies
3
4
5
OK
The count begins at 3 and includes 5. Now make the starting value equal to
the limit:
Type one line at a time, then Enter
10 FOR N=5 TO 5
RUN
BASIC replies
5
OK
That is one trip through the body. After PRINT, NEXT changes N to 6, which
is beyond the limit. The 5 after TO is a limit on the value of N, not an
instruction to repeat five times.
Choose the gap
So far, NEXT has added one each time. A STEP in the FOR instruction chooses
a different amount. Predict the output when the step is 2:
Type one line at a time, then Enter
10 FOR N=1 TO 5 STEP 2
RUN
BASIC replies
1
3
5
OK
Type, then Enter
PRINT N
BASIC replies
7
OK
NEXT adds 2 after each PRINT: 1 becomes 3, 3 becomes 5, and 5 becomes 7.
The value 7 is beyond the limit, so it is not printed by the program.
Without a STEP, BASIC uses a step of 1.
Does the count have to land exactly on the limit? Change 5 to 6:
Type one line at a time, then Enter
10 FOR N=1 TO 6 STEP 2
RUN
BASIC replies
1
3
5
OK
The output is unchanged. Adding 2 to 5 gives 7, which is already beyond 6.
The loop stops without ever printing its limit.
This version of BASIC reaches the body before NEXT makes its first test.
Even a starting value beyond the limit gets one trip through the body:
Type one line at a time, then Enter
10 FOR N=5 TO 3
RUN
BASIC replies
5
OK
FOR sets N to 5, PRINT displays it, and NEXT adds one. Only then does the
test end the loop. Remember this order when experimenting with other
starting values; an upward count whose start is beyond its limit does not
skip the body in this BASIC.
Finish by restoring the simple count from 1 through 5:
Type one line at a time, then Enter
10 FOR N=1 TO 5
RUN
BASIC replies
1
2
3
4
5
OK
We have kept the action, PRINT N, separate from the counting rule. Changing
the start, limit, or step leaves that action alone. The loop controls which
values it sees.
The original Altair BASIC manual
describes FOR and NEXT on printed pages 32–33, including the test at NEXT
and the first trip through the body.
You can keep experimenting in the terminal. This tab carries the BASIC machine and program between these lessons,
including a visit to All lessons, and restores them paused after a refresh. Press the browser's RUN button to continue.
Start fresh discards the program and reloads the retained tape. Copy your numbered lines somewhere for a lasting copy;
browser session storage is temporary.
The machine guide (opens in a new tab)
explains the hardware, loading process, and recovery controls. These lessons use that same machine and original interpreter.