We have so far learned how to print stuff onto the screen and store numbers and text in variables. We are still a way off developing our version of “Angry Birds”, but we’re not far off…
One of the most important commands that a computer has is the “IF” statement. This allows the computer to make a decision.
By the way, have you noticed that BASIC can be read out like English; you can usually work out what a program does by just reading it. This skill will become more important as your quest continues to become a computer games developer.
The IF statement can be summarised like this:
IF something is true THEN do something about it
OK, let’s break this down. What do we mean by “something is true”. Well, in English I could say something like this:
IF I feel hungry THEN eat a biscuit
According to this statement I would only eat the biscuit if I feel hungry. Well, actually in real life I would eat the biscuit anyway, but don’t worry about that. It only confuses the issue.
So, how does this translate to computer speak? How about this:
10 LET A = 123 20 IF A = 124 THEN PRINT "A is equal to 124"
This program doesn’t actually output anything, as the PRINT statement is only executed IF A is equal to 124. We have actually given it the value 123 in line 10. If you want to prove this, then edit line 10 to change the value of A to 124, then run it again…
You can do any comparison, provided that the answer is either a YES (True) or NO (False). The computer will then only run the command if the answer is YES.
Here are some example comparisons:
> Greater Than (i.e. IF A > 1) < Less Than (i.e. IF A < 100) = Equal To (i.e. IF A = 123) <> Not Equal To (i.e. IF A <> 0) >= Greater Than or Equal To (i.e. IF A >= 10) <= Less Than or Equal To (i.e. IF A <= 10000)
Note that you can also compare strings, so you can check IF A$ = “YES”. However strings and numbers are treated differently and you cannot compare apples with oranges. More on that later…
OK. We’ve got variables and decisions, now we’ll work on looping and I think we’ve got the basics cracked…