Practice your coding with the BASin coding teacher.
Try to code at least one program using the five codewords you have learned in this lesson, and any other codewords you have learned from the previous lessons.
The first four codewords in this lesson allow you to tell the computer to do something again and again. We call this making a “loop”, because the computer’s actions go around and around a loop of codewords, however many times you tell it to do so.
All of the codewords in this lesson are statements/commands.
FOR and TO You should learn about the FOR codeword and the TO codeword together, because FOR is always used together with TO. And these two codewords are normally used together with NEXT. Here is an example:
60 FOR c = 1 TO 8 : BORDER c
70 NEXT c
Line 60 means that the computer must first make the border on the screen blue, which is colour 1, as we know from the INK codeword.
Line 70 tells the computer to increase c to its next number, which is 2, and this means that the colour of the border will change to colour 2, which is red.
And the computer goes around and around this loop, increasing c by 1 each time and changing the colour of the border whenever c changes. When c becomes 8 and the colour of the border changes to colour 8, which is transparent, the computer has finished going around the loop.
STEP In the example we have just seen the colour number, which we have called “c” in these two lines of the program, gets bigger by 1 every time the computer goes around the loop from line 60 to line 70 and then back around again. But sometimes you might want the increase to be more than 1. We call the size of the increase a STEP, and we tell the computer this size by using the STEP codeword.
60 FOR c = 1 TO 8 STEP 2
70 NEXT c
The first time the program uses the size of c (we call this size the “value” of c) it is 1. Then after going around the loop once the value of c increases by a step of 2, so it becomes 3 (because 1 + 2 = 3). Then the next time it goes around the loop the value of c becomes 5, then it becomes 7, but then it cannot increase any more because another step of 2 would make it 9, which is too big for c because in line 60 of the program c can only increase up to 8. So when c is 7 the program stops going around the loop.
LET The LET codeword is one of the most useful codewords in BASIC. It tells the computer to give something a certain value. For example, the program line
60 LET x = 3
gives the variable called x the value 3.
You can use the LET command to tell the computer to do arithmetic in your program. Here are some examples:
10 LET a = a + 5
11 REM Line 10 adds 5 to the value of the variable a.
15 LET a = 10 : LET b = 20 : LET c = a + b
16 REM Line 15 adds the value of a, which is 10,
17 REM to the value of b, which is 20.
18 REM The result is 30 which is now the value of c.
20 LET d = d – 8
21 REM Line 20 subtracts 8 from the value of d.
25 LET a = 64 : LET b = 10 : LET c = a – b
26 REM Line 25 subtracts the value of b, which is 19,
27 REM from the value of a, which is 64.
28 REM The result is 45 which is now the value of c.
29 LET f = f * 5
30 REM In BASIC we use * as the “multiply” symbol.
31 REM Line 29 multiplies the value of f by 5.
35 LET a = 7 : LET b = 4 : LET c = a * b
36 REM Line 35 multiplies the value of a, which is 7,
37 REM by the value of b, which is 4,
38 REM The result is 28 which is now the value of c.
39 LET a = a / 2
40 REM Line 39 divides the value of a by 2.
41 REM If we write LET a = 2 / a it would divide 2 by a.
50 LET a = 8 : LET b = 3 : LET c = a / b
51 REM Line 50 divides the value of a, which is 8,
52 REM by the value of b, which is 3,
53 REM The result is 2.66667, which is now the value of c.
60 LET g = g ˄ 4
61 REM Line 60 raises g to the power 4
62 REM which changes it to be g * g * g * g
70 LET a = 2 : LET b = 5 : LET c = a ˄ b
71 REM Line 70 raises the value of a, which is 2,
72 REM to the power b, which is 5,
73 REM so the result is 2 * 2 * 2 * 2 * 2, which is 32.
74 REM So the value of c is now 32.
How to test for true or false
80 LET h = (7 = 9)
81 REM Line 80 sets h to 1 (which means true) if 7 is equal to 9
82 REM or to 0 (which means false) if 7 is not equal to 9
83 REM So the value of h is now 0 because 7 is not equal 9.
How to test variables for true or false
90 LET a = 6 : LET b = 6 : LET c = (a = b)
91 REM Line 90 tests to see if the value of a, which is 6,
92 REM is equal to the value of b, which is also 6,
93 REM and sets c to be 1 (true if they are equal).
94 REM So c is now 1.
This program draws a number of colour rectangles inside each other.
Line 20 selects the paper colour for the rectangle.
Lines 30 to 70 calculate where to draw the rectangle and how big it is.
In Line 90 to 130 we draw the rectangle and Line 140 to 150 loop around drawing all the rectangles.
10 CLS 11 REM Clear the screen 20 LET p=1 21 REM Start the paper colour at 1 (BLUE) 30 FOR k=20 TO 10 STEP -2 31 REM Count from 20 down to 10 in steps of 2 40 LET x=(32-k)/2 41 REM Calculate how many spaces along to start drawing the first rectangle 50 LET w=k+4 51 REM Calculate the width of the rectangle 60 LET y=(20-k)/2 61 REM Calculate how many spaces down to start 70 LET h=k 71 REM Calculate the height 80 PAPER p 81 REM Select the paper colour 90 FOR n=y TO y+h 91 REM Count from the starting vertical position down to the end 100 FOR j=x TO x+w 101 REM Count from the starting horizontal position along to the end 110 PRINT AT n,j;"#" 111 REM draw a # on the screen 120 NEXT j 121 REM Count to the next position along 130 NEXT n 131 REM Count to the next position down 140 LET p=p+1 141 REM select the next paper colour 150 NEXT k 151 REM Count to the next square