Programming is the process of writing instructions that tell computer hardware what to do. It’s fundamental to computing, yet to most people it’s an impenetrable and arcane art. It needn’t be a mystery, though. It would be ridiculous for us to suggest that non-programmers could soon be writing software to rival Microsoft Word or Excel, but understanding the basic principles isn’t as difficult as you might think.

If you’ve never dabbled in computer programming, here’s your opportunity to gain an appreciation of what’s involved. In the first of a two-part series, we’ll teach you the fundamentals so you can start writing programs for your PC.

Many programming languages are available; here, we’ve used Basic (short for beginners’ all-purpose symbolic instruction code). We chose this language for its relative simplicity. All you need to know before you begin is the definition of a program: a list of instructions that is carried out sequentially and tells the computer what to do.

One drawback with Basic is that it’s an old language. As its name suggests, it’s also rather basic. In the 47 years since Basic was introduced, many variations have been produced. SmallBasic, which we use in the following workshop, is just one of them (don’t confuse it with Microsoft’s same-name product: Small Basic).

SmallBasic contains plenty of instructions that weren’t in the original Basic. With the exception of graphics, we’ve avoided using most of these, instead helping you get to grips with the instructions that will be familiar to all versions.

Because the original Basic didn’t include facilities for graphics programming, graphics statements are proprietary in all versions of Basic. The way graphical output is produced in SmallBasic is thus different from many other versions.

Using SmallBasic to program software

Step 1. Before writing any software we’ll run a sample program in SmallBasic. Choose File, Open and select plasma.bat from C\Program Files (x86)\SBW32\FLTK_0.10.7\graphics. The program will appear in a tab labelled plasma.bas. Click Run in the bottom status bar, then select the Output tab.

Program software in SmallBasic 1

Step 2. Select the plasma.bas tab and change ‘const’ to ‘cons’ on line 10. This will go from red to black, since it’s no longer a Basic’s keyword (more on this later). Click Run and you’ll see the error message ‘Undefined sub/func code: cons’ with the status line indicating it’s on line 10. Get used to seeing these error messages.

Program software in SmallBasic 2

Step 3. Select File, Close to get rid of the sample program. Next, select the untitled.bas tab, enter the code shown above and click Run. ‘HELLO WORLD’ will appear in the Output tab. You’ve written your first program. It’s not compulsory in SmallBasic, but we’ll use line numbers so that it’s easy to add commands later.

Program software in SmallBasic 3

Step 4. PRINT is a Basic keyword, and tells the PC to perform an action. Basic allows you to create and name variables that contain a value. We’ll illustrate this using ‘N’. Begin a new program and enter the code shown above, then click Run. The three ‘PRINT N’ statements produce different results because N’s value changes in lines 3 and 5.

Program software in SmallBasic 4

Step 5. The statement ‘N = N + 1’ is an instruction that tells the computer to change the value held in N to one greater than it was previously. Try the program in the screenshot above to see some other arithmetic functions, namely subtract (-), multiply (*), divide (/) and ‘raise to the power of’ (^).

Program software in SmallBasic 5

Step 6. Create a new program and enter the code shown above. Work out what you think the answer will be, then click Run. You probably added 10 and 2, which makes 12, then multiplied it by 4, resulting in 48. In fact, the answer is 18. Basic completes multiplications and divisions before additions and subtractions.

Program software in SmallBasic 6

>> Continues

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