Program software for Windows with SmallBASIC
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’.
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.
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.
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.
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.
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.
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.