RISCOS.com

www.riscos.com Technical Support:
BBC BASIC Reference Manual

 


Simple programming


A program is a list of instructions to be carried out by the computer. These instructions are stored in memory and are only executed when you tell the computer to do so.

Entering a program

Once you have entered BASIC you can begin to type in programs. Each line of a program is numbered so that it can be referred to more easily. Note that you must press Return at the end of each line you type in. For example, type the following:

10 PRINT "Hello"

Note that nothing happens (but all must be well as no error message was printed). Now type

RUN

The Hello message is displayed on the screen. The number 10 at the start of the line is called the line number, and identifies the text after it as a program statement to be stored in memory, rather than as a command to be executed immediately.

You can type spaces either between the start of the line and the line number, or between the line number and the instruction without affecting the execution of the program.

    10       PRINT "Hello"

and

10PRINT "Hello"

are equally valid.

One of the advantages of programs is that they can be executed repeatedly: Typing RUN again here causes Hello to be displayed a second time - there is no need to type the complete PRINT "Hello" statement again.

The following is a simple program demonstrating the use of a variable and the INPUT statement:

10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed was ";number

The line numbers determine the order in which the computer executes these instructions. They can take any whole value between 0 and 65279. You can type line numbers in any order you like; BASIC will sort them into ascending order and obey them in this order.

Now RUN this program. The computer obeys line 10 and displays the message:

Can you give me a number ?

The question mark is added automatically by the execution of line 20. It will appear on a new line if you miss off the semicolon at the end of line 10.

The keyword INPUT instructs the computer to wait for you to type something, in this case a number. Type the following (followed by Return):

6

Line 30 is now obeyed, and the following message is displayed:

The number you typed was 6

Altering a program

Once you have entered a program, you may wish to make changes to it.

You can of course type in a whole new version of the program, but there are quicker methods available.

To see the program which is currently stored in memory, type

LIST

Lines 10, 20 and 30 are listed on the screen.

Replacing and adding lines

To add extra lines to the program, type in the new line with an appropriate line number:

5 PRINT "Hello"
40 PRINT "Twice ";number " is ";2*number

and then:

LIST

Note that these two extra lines are added to the program in such a way that the line numbers are listed in numerical order:

 5 PRINT "Hello"
10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed was ";number
40 PRINT "Twice "; number " is "; 2*number

To replace lines, enter the new line with the line number of the one which is to be replaced. For example:

40 PRINT number;" squared is ";number*number

Now when you type

LIST

the following is displayed:

 5 PRINT "Hello"
10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed was ";number
40 PRINT number;" squared is "; number*number
Altering a single line in a program

If you wish to alter only part of a line, for example, to correct a single spelling mistake, you can do so using the cursor edit keys. These are the arrow keys to the right of the main keyboard.

Suppose you want to change the word typed to entered on line 30. Begin by pressing the [UP] key twice. The original cursor position which was under line 40 becomes a square and the cursor moves up to the start of line 30.

Press Copy a few times. The cursor editing moves along line 30, the square moves along as well, and line 30 is copied underneath line 40. Keep on pressing Copy until the word typed is copied and then stop.

If you hold the Copy key down, the repeat action allows you to move the cursor quickly across the screen. A quick press and release gives you precise control, moving one character position. The following is displayed on your screen:

 5 PRINT "Hello"
10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed_was ";number
40 PRINT number;" squared is "; number*number
30 PRINT "The number you typed

Press Delete until the word typed is deleted from the new line 30. The cursor on the old line 30 does not move:

 5 PRINT "Hello"
10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed_was ";number
40 PRINT number;" squared is "; number*number
30 PRINT "The number you

Type the word

entered

and press Copy to copy the rest of line 30 to your new version.

Press Return. The square disappears and the cursor moves to the start of a new line. Now type

LIST

to produce the following:

 5 PRINT "Hello"
10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you entered was ";number
40 PRINT number;" squared is "; number*number

There are no restrictions on how much you move the cursor around when you are copying. Note when the cursor reaches the end of the screen it will wrap-around to the other side of the screen. You can use the right and left arrow keys to miss out parts of lines or to repeat them. You can also copy from several different lines on to your new line as you go.

Deleting lines

You can either delete lines one at a time, or delete a group of lines at once using the DELETE command.

To delete a single line, you just type the line number followed by Return. To delete line number 5, for example, type

5

To check that line 5 is deleted, type

LIST

and the computer displays the following:

10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you entered was ";number
40 PRINT number;" squared is "; number*number

The DELETE command allows you to delete a number of consecutive lines in three different ways:

  • By deleting a block of lines. To delete all line numbers between 10 and 30 inclusive, type:
    DELETE 10,30
  • By deleting from the beginning of a program. To delete all lines from the beginning of the program to line 30, type:
    DELETE 0,30
    The number zero is the minimum line number that can be used in a program. Therefore, all lines from the start of the program to line 30 are deleted.
  • By deleting from a line to the end of the program. To delete all lines from line 20 to the end of the program, for example, type:
    DELETE 20,65279
    The number 65279 is the maximum line number that can be used in a program, so in this case all lines from line 20 to the end of the program are deleted. Of course, you can use any other number which is higher than the last line of the program, so something like 60000 will usually work just as well, and is somewhat quicker to type!

Deleting whole programs

Before you enter a new program, make sure no program currently exists in memory. If it does, the lines of the new program you enter will get mixed up with the lines of the existing program, and this could produce strange results!

To delete any existing program, you can use the DELETE command described above, but an easier method is to type

NEW

This tells the computer to forget about any existing program, and to be ready to accept a new one.

Although the DELETE and LIST commands combined with cursor editing are fine for making small changes to a BASIC program, you should note that, if you are using RISC OS 2, the BASIC Editor is much more versatile. See the chapter entitled Editing Basic Programs for details of using this program.

Note: RISC OS 3 users should use Edit as a BASIC program editor.

Numbering lines in a program

There may be occasions when you want to change the line numbers of a program without changing their order. The command to use is RENUMBER. This facility is particularly useful when you want to insert a large number of lines between two existing ones.

You can specify two numbers after typing the RENUMBER command. The first number tells the computer what you want the new first program line number to be. The second number tells the computer how much to add to each line number to get the next one.

For example,

RENUMBER 100,20

makes the first line into line 100 and numbers the remaining lines 120, 140, 160, and so on.

If you leave out the second number in the RENUMBER command, the computer automatically increments the line numbers in steps of 10. So, for example, you might want to renumber the following program:

23 PRINT "This demonstrates"
24 PRINT "the use of"
48 PRINT "the very useful"
67 PRINT "RENUMBER command"

Typing

RENUMBER 100
LIST

produces the following display:

100 PRINT "This demonstrates"
110 PRINT "the use of"
120 PRINT "the very useful"
130 PRINT "RENUMBER command"

Typing

RENUMBER

without including a number after the command, means that your program lines are renumbered 10, 20, 30, 40 and so on.

Automatic line numbering

You do not have to type line numbers at the beginning of each new program line. The computer does it automatically when given the AUTO command. For example, type:

AUTO

The computer displays the number 10 on the line below. If you type the first program line and press Return, the number 20 appears on the next line, and so on. To leave this automatic line numbering mode, press Esc.

Starting a program from a particular line

You can start a program at a line other than line 10 by following the AUTO command with the first line number you wish to use. Thus,

AUTO 250

generates lines which are numbered 250, 260, 270, and so on.

You can also specify the number of spare lines between each of your program lines by adding a second number, separated from the first by a comma. Thus,

AUTO 250,15

starts at line number 250 and subsequently increases the line numbers in steps of 15, generating lines numbered 250, 265, 280, and so on.

Listing long programs

The LIST command, used above to display the current program on the screen, can be used to look at part of a program. This is particularly useful if the program is very big and you want to concentrate on one part of it.

Listing sections of programs

To look at one particular line type, for example

LIST 40

To look at a number of consecutive lines type, for example,

LIST 20,40

To see from the beginning of the program up to a particular line type, for example,

LIST ,30

To display from a particular line to the end of the program type, for example,

LIST 20,

Halting listings from the command line

If you list more of a program than can fit on the screen all at once, the beginning of the listing disappears off the top of the screen before you have time to read it. If you are running BASIC from the command line there are three ways of getting round this problem:

  • Pressing the Scroll Lock halts the listing; pressing it again allows the listing to continue. This enables you to step through chunks of the listing.
  • Holding down Ctrl and Shift together after typing LIST halts the displayed listing on the screen. To continue the listing, take your finger off either Ctrl or Shift.
  • Putting the computer into paged mode. This is the most reliable method. To enter this mode press Ctrl-N, then type LIST. The listing stops as soon as the whole screen is filled. To display the next screenful of listing, press Scroll Lock twice. This method ensures that you will not miss any of the listing. To cancel the effect of Ctrl-N, type Ctrl-O when the listing is finished.

In addition to the methods described for halting listings, you can also slow the listing down by pressing Ctrl. This makes the screen halt for the auto-repeat rate time (typically about 1/25th of a second) between each new line. Thus it takes a second to scroll one screenful in a 25-line text mode.

Comments

When writing programs, especially long or complex ones, you should insert comments to remind you what each part of the program is doing. This is done by using the REM keyword which is short for 'remark'.

REM tells the computer to ignore the rest of the line when it executes the program. For example, to add comments to the following program:

10 PRINT "Can you give me a number ";
20 INPUT number
30 PRINT "The number you typed was ";number
40 PRINT number;" squared is "; number*number

type:

 5 REM Read in a value and assign it to number
25 REM Now print out the number given.
35 REM And its square

and then:

LIST

to display the complete program:

 5 REM Read in a value and assign it to number
10 PRINT "Can you give me a number ";
20 INPUT number
25 REM Now print out the number given.
30 PRINT "The number you typed was ";number
35 REM And its square
40 PRINT number;" squared is "; number*number

You may like to add further REM statements to underline comments or leave space above them to make them clearer:

 5 REM Read in a value and assign it to number
 6 REM ---------------------------------------
10 PRINT "Can you give me a number ";
20 INPUT number
24 REM
25 REM Now print out the number given
26 REM ------------------------------
30 PRINT "The number you typed was ";number
34 REM
35 REM And its square
36 REM --------------
40 PRINT number;" squared is "; number*number

Multiple statements

A line of BASIC can contain up to 238 characters and can be spread over several lines on the screen. In all the programs given so far, each line of BASIC contains a single statement. Several statements, however, may be placed on one line separated by colons (:). For example:

10 PRINT "Can you give me a number ";:INPUT number
30 PRINT "The number you typed was ";number: REM print out the number
40 PRINT number;" squared is "; number*number: REM and its square

Note that REM statements must only be placed at the end of a line since the whole of the rest of the line is ignored. If you alter the program so that line 30 reads as follows:

30 REM print out the number: PRINT "the number you typed was ";number

you will prevent the PRINT statement being executed.

The lines above illustrate that lines with more than one statement can overflow onto the next screen line very easily, making the program hard to read. You should therefore try to avoid too many multi-statement lines.

Saving and recalling programs

You can save a copy of the current program to disc at any time. This allows you to recall (load) it at a later date, without having to retype all the instructions. How you are running BASIC determines how you can save your program.

Saving and loading a program from Edit (RISC OS 3)

If you are writing your program in Edit, you can save it to disc at any stage, using the Save menu option (see the chapter on Edit in the Applications Guide). Once saved, the file can be loaded for editing by holding down Shift and double-clicking on its icon.

Saving a program from the command line

To save a program from the command line, type

SAVE "program_name"

The program will be saved to the currently-selected disc, with the name program_name. The name you use when saving a program can contain up to 10 characters. At this stage, you should confine your names to numbers and upper- and lower-case letters and digits. Other characters may be used but some have special meanings. See the RISC OS User Guide for further information on file naming.

After using SAVE, your program remains in memory and is unaltered in any way. You can still edit, LIST, RUN, and so on.

Another capability of the REM statement is that it allows you to give the program name for use by the SAVE command. The filename must be preceded by a > character, and the REM containing it must be the first line of the program. Thus, if the first line of the program is

10 REM >prog1

all you need to do is type the SAVE command (or its abbreviation SA) on its own, and the name prog1 will be used to save the program.

Loading a program from the command line

To load a program which you have previously saved, in this case prog1, type

LOAD "prog1"

The LOAD operation replaces the current program with the one from the disc (so you should be sure that you don't mind losing the current program before you load a new one). You can check this by listing the program currently in memory.

In addition to loading a program, you can add a program to the end of the current one using the APPEND command. The appended program is renumbered to ensure that its line numbers start after those of the initial program. The statements LIBRARY and OVERLAY may be used to add libraries of procedures and functions to the current program (see the chapter entitled Procedures and functions for details).

This edition Copyright © 3QD Developments Ltd 2015
Last Edit: Tue,03 Nov 2015