ZX Classroom - BASIC String Functions: TO, LEN, STR$, and VAL

ZX Revija #02

ZX Classroom: BASIC String Functions

Functions Used When Working with String Data

TO

Used to obtain a string or substring consisting of a group of consecutive characters from a specified string data item or expression.

General form:

string expression (m TO n)

where:

  • m — starting position of the string or substring in the specified string expression. Optional.
  • n — ending position of the string or substring in the specified string expression. Optional.
LEN

Used to determine the length of a string data item or expression.

General form:

LEN string expression
STR$

Used to convert a numeric data item into a string data item.

General form:

STR$(numeric expression)
VAL

Used to convert a string variable or expression into a numeric data item.

General form:

VAL string variable/data

Notes:

  1. The parentheses used in the functions mentioned above may be omitted if the numeric expression is simple.
  2. STR$ and VAL are mutually inverse functions; that is, STR$(VAL a$)=a$.
  3. The string variable or expression must be such that the VAL function returns a numeric value.

Examples of the TO, LEN, STR$ and VAL Functions

10 PRINT REVIJA (1 TO 6)
20 PRINT REVIJA (1 TO 3)
30 PRINT REVIJA (4 TO 6)
10 INPUT "Enter your name? ";a$
20 PRINT "Your name is ";LEN a$;" characters long"
10 LET a=521.34:LET b=20
20 LET c$="a+b=" + STR$ (a+b):PRINT c$
10 PRINT "11č11=";VAL "11č11"

Commands for Defining a New Function

DEF FN

Used to define a new function.

General form:

DEF FN function name (list)=numeric/string expression

where:

  • function name — the name assigned to the newly defined function. It may or may not end with $, depending on whether the function's value is numeric or string data.
  • list — one or more arguments, separated by commas. If there is more than one argument, they must be enclosed in parentheses.

Example of the DEF FN command

10 LET x=0:LET y=0:LET a=10
20 DEF FN p(x,y)=a+x*y
30 PRINT "Function p(x,y)=a+x*y"
40 PRINT "For x=2, y=3 and a=10, its value is ";FN p(2,3)
50 STOP

Conditional and Unconditional Jump Commands

These are used to change the order in which commands in a program are executed. This group includes commands such as GO TO and IF.

GO TO

Allows an unconditional jump to the command with the specified number; that is, it transfers program execution to a particular program line.

General form:

GO TO [line number]

where:

  • line number — the label of a program line. It may be omitted; in that case, the jump is made to the next higher line number.
IF

Allows the program to branch depending on the specified condition.

General form:

IF <condition> THEN GO TO line number

or

IF <condition> THEN commands

where:

  • <condition> — a relation represented by two numeric or string expressions, with one of the comparison symbols between them (=, <, >, <=, >= or <>). These are called simple conditions.
  • line number — the label of a program line.
  • commands — one or more BASIC commands.

Notes:

The <condition> specified in an IF command may also be compound.

Compound conditions are formed by combining simple conditions using the reserved words AND, OR and NOT. A compound condition containing:

a) AND (read “and”) is true if both simple conditions are true.

b) OR (read “or”) is true if at least one of the simple conditions is true.

c) NOT (read “not”) is true if the associated simple condition is false.

ZX Classroom: BASIC (String Functions)

Examples of the GO TO and IF Statements

10 LET x=1
20 PRINT x:LET x=x+1
30 GO TO 20
10 LET a$=INKEY$
20 IF a$= THEN PRINTa$
30 GO TO 10
10 LET a=0
20 PRINT a,ača
30 LET a=a+1
40 IF a<11 THEN GO TO 20
50 STOP

Statements for Implementing Program Loops

A program loop is a group of statements whose execution is to be repeated a certain number of times, with its beginning and end indicated respectively by the FOR and NEXT statements. These are special BASIC statements that allow a variable to be used as a counter.

FOR

Used as the first statement in a program loop, i.e. it defines the beginning of the loop.

The general form of the statement is:

FOR control variable=start TO end [STEP increment]

where:

  • control variable is a numeric variable called the loop index, and its name must consist of a single letter;
  • start is a numeric constant/expression that defines the initial value of the loop index;
  • end is a numeric constant/expression that defines the final value of the loop index;
  • increment is a numeric constant/expression that defines the increment of the loop index.

The increment is optional. If omitted, its value is assumed to be +1.

NEXT

Used as the last statement in a program loop, i.e. it defines the end of the loop.

The general form of the statement is:

NEXT control variable

The control variable is described in the previous section.

FLOWCHART

FOR i=p1 TO p2 STEP p3
    execution of BASIC language statements
NEXT i
Examples of FOR and NEXT Statements
10 FOR i=1 TO 99
20 PRINT i
30 NEXT i
40 STOP
10 FOR i=5 TO 119 STEP 5
20 PRINT i
30 NEXT i
40 STOP

Data Dimensioning Statements (DIM)

This statement allows a unique name to be assigned to a group of data and reserves computer memory for that group. It can be used for both numeric and character data.

Notes:

  1. The data dimensioning statement belongs to the category of descriptive statements. It serves solely to describe the data used in the program and has no executable effect.
  2. A group of data can be multidimensional.
  3. The DIM statement must be specified in the program before any other statement that refers to the members of the array specified in the DIM statement.
  4. A program can contain several DIM statements.

Usually, they are all written at the very beginning of the program.

DIM (for Numeric Data)

Assigns a unique name to a group of numeric data and also defines in computer memory the space required to store it.

The general form of the statement is:

DIM a(n1,n2,...,nk)

where:

  • a is one of the letters of the English alphabet and represents the name of a k-dimensional array;
  • n1,n2,...,nk are numeric expressions rounded to the nearest whole number.
DIM (for Character Data)

Assigns a unique name to a group of character data and reserves computer memory for its storage.

The general form of the statement is:

DIM a$(n1,n2,...,nk)

where:

  • a$ is one of the letters of the English alphabet followed by a dollar sign ($), indicating that the data is character data.

Here, a$ is the name of a k-dimensional array. For n1,n2,...,nk, see DIM a.

Example of the DIM Statement
10 DIM a(10)
20 PRINT "Loading a one-dimensional array with 10 elements"
30 PRINT
40 FOR n=1 TO 10
50 INPUT "Enter the value of the array element";a(n)
60 PRINT n;"array element is:"

ZX Classroom: BASIC (String Functions)

Fragment 3/4.

;a(n)
70 NEXT n
80 PRINT:PRINT "End of loading":STOP
10 DIM a$(3,4)
20 FOR n=1 TO 3: FOR m=1 TO 4
30 READ a$(n,m)
40 PRINT AT 3*n,3*m;a$(n,m)
50 NEXT m:NEXT n
60 DATA exy

Commands for Implementing Subroutines

The basic commands for implementing subroutines are GO SUB (which calls a subroutine) and RETURN (which defines the last command in a subroutine).

GO SUB

Transfers control to a subroutine.

General form:

GO SUB line number

where:

  • line number — the program line number to which control is transferred.
RETURN

Defines the end of a subroutine, i.e. exits the subroutine called by the GO SUB command.

General form:

RETURN
Examples of GO SUB and RETURN
10 REM using subroutines
20 PRINT "program"
30 GO SUB 100
40 PRINT "continuation of program"
50 GO SUB 100
60 PRINT "end of program"
70 STOP
100 PRINT "subroutine running"
110 RETURN
10 REM using a subroutine with multiple entry points
20 PRINT "program"
30 GO SUB 100
40 PRINT "continuation of program"
50 GO SUB 100
55 GO SUB 90
60 PRINT "end of program"
70 STOP
90 PRINT "overlapping subroutine"
100 PRINT "subroutine running"
110 RETURN

Color Commands

The commands that enable you to work with colors are INK, PAPER, FLASH, BRIGHT, INVERSE, OVER and BORDER.

Color Table
Code Color
0 black
1 dark blue
2 red
3 purple
4 green
5 light blue
6 yellow
7 white
INK

Used to define the color of characters displayed on the screen.

General form:

INK n

where:

  • n — the color code for characters to be displayed or drawn in the screen’s inner area. In addition to the standard values [0–7 (see the color table)], values 8 and 9 may also be used, with the following meanings:
    • 8 — the color remains unchanged, i.e. as previously defined;
    • 9 — the color contrasts with the background color.

“Contrasts” means that the color will be white if the background color is dark (0 to 3), or black if the background color is light (4 to 7).

PAPER

Used to define the background color on which content is displayed.

General form:

PAPER n

where:

  • n — the background color code. The rest is the same as for the previously described INK command.
FLASH

Used to define characters as steady or flashing.

General form:

FLASH m

where:

  • m — a parameter that can take the value 0, 1 or 8, with the following meanings:
    • 1 — causes the background and character colors to alternate rhythmically, i.e. the background color becomes the character color, and vice versa;
    • 0 — turns flashing off, so displayed characters remain steady;
    • 8 — retains the previously defined state.
BRIGHT

Used to define the brightness of the part of the screen to which the command applies.

General form:

BRIGHT m

where:

  • m — a parameter that can take the value 0, 1 or 8, with the following meanings:
    • 1 — makes the part of the screen to which the command applies brighter;
    • 0 — turns off the effect of the BRIGHT 1 command;
    • 8 — leaves the state unchanged.
INVERSE

Used to control the inversion of the colors defined by PAPER and INK.

General form:

INVERSE 1

where:

  • 1 — a parameter that can take the value 0 or 1, with the following meanings:
    • 1 — causes the PAPER color to become the INK color, and vice versa;
    • 0 — turns off the effect of the INVERSE 1 command.
OVER

Used to control overprinting for the characters that follow.

General form:

OVER 1

where:

  • 1 — a parameter that can take the value 0 or 1, with the following meanings:
    • 0 — characters are printed over previous ones, erasing them;
    • 1 — causes one character to be printed over another, i.e.

Two characters are printed one on top of the other.

BORDER

Used to define the color of the area outside the screen.

The general form of the command is:

BORDER n

where:

n — see the explanation for the INK command.

ATTR

Used to obtain the visual attributes of a specific screen position.

The general form of the function is:

ATTR(row,column)

where:

row — defines the number of the line containing the desired position; column — defines the number of the column containing the desired position.

Examples of commands for working with colors:

10 REM test for INK
20 PRINT ;INK 2; ZX REVIJA
30 PRINT AT 1,0;INK 4; ZX SPECTRUM
10 REM test for PAPER
20 PRINT;PAPER 4; ZX REVIJA
10 REM test for FLASH
20 PRINT AT 1,0;FLASH 0; ZX REVIJA
30 PRINT AT 2,0;FLASH 1; ZX REVIJA
40 PRINT AT 3,0;FLASH 8; ZX REVIJA
10 REM test for BRIGHT
20 PRINT AT 1,0;BRIGHT 0; DUGA
30 PRINT AT 2,0;BRIGHT 1; DUGA
40 PRINT AT 3,0;BRIGHT 8; DUGA
10 REM test for INVERSE
20 PRINT AT 1,0;INVERSE 0 SPEKTRUM
30 PRINT AT 1,0;INVERSE 1; SPEKTRUM
10 REM test for OVER
20 PRINT ZX REVIJA
30 PRINT AT 0,3;OVER 1; DUGA
10 REM test for BORDER
20 FOR n=7 TO 0 STEP -1
30 PAUSE 20:BORDER n
40 NEXT n
50 GO TO 10
10 REM test for ATTR
20 PRINT AT 10,10;INK 5; PAPER 0;FLASH 1; ZX REVIJA
30 PRINT ATTR (10,10)

OUT

This command writes data y to the output unit at address x.

The general form of the command is:

OUT x,y

where:

x — the address, which can have a value in the range from 0 to;
y — the data, in the range from 0 to 255 (one byte).

Example of the OUT command:

10 FOR n=0 TO 7
20 OUT 254,n
30 PAUSE 2
40 NEXT n
50 GO TO 10
10 OUT 254,16
20 OUT 254,0
30 GO TO 10

BEEP

The ZX Spectrum supports sounds/tones, which can be produced using the BEEP command.

The general form of the command is:

BEEP duration,tone

where:

duration — a numeric expression defining the length of the sound signal in seconds;
tone — a numeric expression defining the pitch, i.e. the semitone, where tone C corresponds to 0.

Example of the BEEP command:

10 BEEP .5,0:BEEP .5,2
20 BEEP .5,4:BEEP .5,5
30 BEEP .5,7:BEEP .5,9
40 BEEP .5,11:BEEP .5,12

STOP

Share your thoughts about the article