We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
2,862 Tutorials

How to program software

Get to grips with SmallBasic

Step 7. The GOTO instruction tells the PC to jump to a specified line number. SmallBasic also uses the IF statement, allowing you to have multiple conditions. For example, ‘IF N = 10 AND K = 0 THEN GOTO 50’. We’re also introducing the INPUT statement and END. Strictly speaking, all programs should end with END.

Program software in SmallBasic 7

Step 8. GOTO can generate loops, but FOR and NEXT provide a better solution. The code between the two statements is executed for each value of the variable in the FOR statement (our program displays numbers from 1 to 10 and their squares). You can also specify a step in FOR, such as ‘FOR N = 2 TO 100 STEP 2’.

Program software in SmallBasic 8

Step 9. In step 2 we saw a syntax error. This type of error is picked up before you try to run a program; others are encountered only when a program is run. The above program illustrates this – have a go yourself. It will work fine until 1 equals 0 and the program is unable to divide by zero.

Program software in SmallBasic 9

Step 10. Here’s another error. Run the program and enter ‘1’ when prompted. It will terminate properly. Run it again and enter ‘0.3’. The program won’t end because N never equals 10. Click BRK in the status bar to force it to stop. Noting that RUN has changed to BRK proves a program is running, possibly in an infinite loop.

Program software in SmallBasic 10

Step 11. Programs sometimes require data that can be read from an external file or included in the program as DATA statements and accessed using the READ statement. You can include as many values as you want on a DATA statement. This program would work the same way if all the values were in one long DATA statement.

Program software in SmallBasic 11

Step 12. Using DATA and READ saved us using lots of assignment statements (such as X = 108), but it doesn’t produce a very exciting or useful result. Change the first PRINT statement to ’20 SET X, Y COLOR 5’ and the second to ’50 LINE X, Y COLOR 5’. Check the Output tab matches the screenshot below.

Program software in SmallBasic 12

Step 13. Add ‘1 REM’ as the first line of the program and run it again. It’ll work exactly the same. REM (remark) statements are ignored, but useful for adding comments. Get into the habit of including REM statements – they make your programs far easier to understand if you want to change them later.

Program software in SmallBasic 13

Step 14. If you want to use values in the DATA statement more than once, use the RESTORE statement. This causes the next READ to return to the specified line (for example, RESTORE 100). Now try it: modify the program to draw two or more of these symbols, each offset by 10 pixels using a FOR/NEXT loop.

Program software in SmallBasic 14

IDG UK Sites

LG G4 Note UK release date and specification rumours: Samsung Galaxy Note 5 killer could be the LG 3......

IDG UK Sites

In defence of BlackBerrys

IDG UK Sites

Why we should reserve judgement on Apple ditching Helvetica in OS X/iOS for the Apple Watch's San...

IDG UK Sites

Retina 3.3GHz iMac 27in preview: Apple cuts £400 of price of Retina iMac with new model