Mark Wickens

back to Table of Contents

BASIC Programming Guide

This section provides a practical guide to writing BASIC programs on Casio pocket computers. It covers program entry, editing, execution, debugging, and the fundamental commands used in BASIC programming.

Features of BASIC

  1. High-precision calculations — numeric values with 10-digit mantissa and 2-digit exponent display (13-digit mantissa and 2-digit exponent internally).
  2. Rich built-in functions — standard mathematical functions (SIN, COS, TAN, LOG, EXP, SQR, etc.), string functions (CHR$, MID$, LEFT$, RIGHT$, etc.), and high-level math functions (POL, REC, NCR, NPR, hyperbolic functions).
  3. 10 independent program areas (P0-P9) — up to ten programs can be stored simultaneously.
  4. Extended variable names — up to 255 characters long (15 on the FX-850P/880P), making it possible to use descriptive names.
  5. Debugging with TRON — displays the line number currently being executed, making it easy to trace program flow.
  6. Screen editor — programs can be easily modified directly on the screen.
  7. Virtual screen — though the physical display shows 2 lines (FX-850P/880P) or 4 lines (FX-870P/VX-4), the virtual screen is 32 columns by 8 lines, scrollable with cursor keys.
  8. File management — standard BASIC commands OPEN, CLOSE, INPUT#, and PRINT# for data file operations.

Program Structure

A BASIC program is a collection of numbered lines. Each line consists of a line number followed by one or more statements:

10 REM CYLINDER
20 R = 15
30 INPUT "H="; H
40 V = PI * R ^ 2 * H
50 PRINT "V="; V
60 END

Entering a Program

  1. Enter BASIC mode by pressing MODE then 1.
  2. Select a program area if needed (e.g., SHIFT 3 for P3).
  3. Type each line with its line number and press EXE to store it.
10 REM CYLINDER    [EXE]
20 R=15            [EXE]
30 INPUT "H=";H    [EXE]
40 V=PI*R^2*H      [EXE]
50 PRINT "V=";V    [EXE]
60 END             [EXE]

A line is not stored in memory until EXE is pressed.

One-key input: Many BASIC keywords can be entered with a single key combination using SHIFT followed by the labeled key (e.g., SHIFT + INPUT key).

Editing a Program

Editing the Current Line

Before pressing EXE, use the cursor keys (left/right arrows) to move to the character you want to change, then type the correct character.

After making changes, press EXE to store the corrected line.

Using LIST to Edit

The LIST command displays the program. You can then scroll through it with cursor keys and make changes directly.

LIST        ' Display entire program
LIST 30     ' Display from line 30
LIST 30-60  ' Display lines 30 through 60
LIST .      ' Display last line executed

Using EDIT Mode

The EDIT command enters a line-by-line editing mode:

EDIT        ' Start editing from first line
EDIT 30     ' Start editing from line 30
EDIT .      ' Edit last line executed

In EDIT mode:

Deleting Lines

Running a Program

Two ways to execute a program:

  1. In BASIC mode: type RUN and press EXE. The program in the current program area executes.
    • RUN 100 starts execution from line 100.
  2. In CAL mode: press SHIFT followed by a program area number (0-9). If a program exists in that area, it runs immediately.

Program Output

When a PRINT statement executes, STOP appears on the display and execution pauses. Press EXE to continue. To suppress the pause, end the PRINT statement with a semicolon:

10 PRINT "BASIC ";    ' No pause — next output appears immediately after
20 PRINT "PROGRAM"

Stopping a Program

Fundamental Commands

REM — Remarks

140 REM Square root and cube root calculation

REM lines are ignored during execution. Use them to document your program. A single quote (') can substitute for REM: 140 ' Square root calculation

INPUT — Keyboard Input

30 INPUT "Height="; H

INPUT displays a prompt and waits for keyboard input. The entered value is assigned to the variable.

50 PRINT "Volume="; V

PRINT displays values and strings on the screen. Separate items with ; for continuous output or , for paused output.

IF~THEN~ELSE — Conditional Execution

40 IF N > 5 THEN N = 5
50 IF N = 0 THEN GOTO 130
60 IF A > 100 THEN PRINT "Big" ELSE PRINT "Small"

IF~THEN~ELSE tests a condition. If true, the THEN clause executes. If false and ELSE is present, the ELSE clause executes. You can jump to other program areas: IF A = 1 THEN GOTO #2

GOTO — Unconditional Jump

120 GOTO 10

GOTO jumps to the specified line number. Can also jump to other program areas: GOTO #3

FOR~NEXT — Loop

60 FOR I = 1 TO 10
70   X = X + A(I)
80 NEXT I

FOR~NEXT repeats the enclosed statements. An optional STEP value can be specified: FOR I = 10 TO 0 STEP -1

READ / DATA / RESTORE — Embedded Data

70 READ X
180 DATA 9, 7, 20, 28, 36
20 RESTORE

READ assigns values from DATA statements to variables. RESTORE resets the read pointer back to the first DATA item (or to a specific line).

GOSUB / RETURN — Subroutines

100 GOSUB 1000
    ...
1000 REM Subroutine
1010 Y = SQR(S)
1020 RETURN

GOSUB jumps to a subroutine; RETURN returns to the statement following the GOSUB call. Can also call subroutines in other program areas: GOSUB #3

END — Program Termination

130 END

END terminates program execution. A program ends naturally when it reaches the last line, but END can terminate execution from anywhere.

Constants and Variables

Numeric Constants

String Constants

Variable Types

Type Example Description
Numeric variable A, TOTAL, x1 Stores a numeric value
String variable A$, NAME$ Stores a character string
Numeric array A(10), M(3,3) Array of numeric values
String array A$(10), NAMES$(5) Array of character strings

Debugging

Trace Mode

TRON enables trace mode. When a program runs, the program area and line number of each statement are displayed, and execution pauses until EXE is pressed. This lets you step through the program to find logic errors.

TRON        ' Enable trace mode
RUN         ' Run with tracing
TROFF       ' Disable trace mode

Error Messages

When an error occurs, a message like SN error P0--10 is displayed, indicating:

Use LIST . or EDIT . to jump directly to the line that caused the error. See Error Messages for the full list.

Continuing After an Error

After fixing an error, use CONT to resume execution from where it stopped (only works after STOP or BRK — not after an error).

Saving and Loading Programs

Programs are preserved in memory by the backup battery even when the computer is powered off. For permanent storage, programs can be saved to external media.

FX-850P/FX-880P (Cassette Tape) [FX-850P/880P]

SAVE "MYPROG"           ' Save current program area to cassette
SAVE ALL "ALLPROGS"     ' Save all program areas
LOAD "MYPROG"           ' Load a program from cassette
LOAD ALL "ALLPROGS"     ' Load all program areas
VERIFY "MYPROG"         ' Verify saved program matches memory

FX-870P/VX-4 (RS-232C and Floppy Disk) [FX-870P/VX-4]

SAVE "0:MYPROG.BAS"     ' Save to floppy disk
LOAD "0:MYPROG.BAS"     ' Load from floppy disk
SAVE "COM0:...", A       ' Save as ASCII over RS-232C
FILES                    ' List files on floppy disk
KILL "0:OLDPROG.BAS"    ' Delete a file
NAME "0:OLD" AS "0:NEW" ' Rename a file

See the individual command pages for SAVE, LOAD, FILES, KILL, and NAME for complete details on file descriptors and options.

Error Handling

Programs can handle errors gracefully using ON ERROR GOTO:

10 ON ERROR GOTO 100
20 INPUT "Number: "; N
30 PRINT 1 / N
40 END
100 PRINT "Error"; ERR; "at line"; ERL
110 RESUME NEXT

Memory Management

Checking Free Memory

Memory Allocation

Calculating Program Size

Element Bytes
Line number 2 bytes (regardless of number size)
Command/keyword 2 bytes (stored as token)
Each character (letters, digits, spaces) 1 byte
End of line (EXE) 1 byte
Overhead per line 1 byte

Example: 10 A = SIN X uses 2 + 1 + 1 + 1 + 2 + 1 + 1 + 1 + 1 = 11 bytes.

Cross-Program-Area Features

One of the unique features of Casio JIS BASIC is the ability to call between program areas:

GOTO #3          ' Jump to the first line of program area 3
GOSUB #5         ' Call program area 5 as a subroutine
RETURN #0        ' Return to program area 0
IF A=1 THEN #2   ' Conditional jump to program area 2
ON A GOTO #1,#2  ' Computed jump across program areas

Remember that variables are global across all program areas — a value set in P0 is visible in P3. This enables cooperation between programs but requires care to avoid naming conflicts.