Hello, and welcome to the ninth part of our on-going Basic 
     Tutorial. This month, Peter Phillips is taking a detailed look at 
     decision making with in Basic.
                            FIRST STEPS IN BASIC
          Before we embark on this months article proper, remember the 
     problem I set at the end of last month? Why does B = 100, when its 
     limiting value is just 10? Well, for those of you who haven't 
     worked it out for yourselves, the answer is quite straightforward. 
     The loop controlled by counter B is nested inside the loop 
     controlled by the value of A, which is also 10. Thus the B loop is 
     actually executed 10 times before the limiting value is exceeded, 
     and 10 times 10 is 100.
          While this may seem a pointless little exercise, it does 
     demonstrate the dangers of nesting loops. Unless you understand 
     precisely what the effects of one loop on another may be, you can 
     end up with some results you didn't expect.
                         DECISIONS, DECISIONS!
          We all make decisions all the time in our everyday lives. You 
     made a decision to buy ST User, and another to read this column. 
     These decisions are arrived at by careful consideration, and 
     weighing up various factors.
          For instance, say you went to market and decided you wanted 
     to buy some fruit. A simple decision between apples and pears 
     could be made on price alone, you decide to buy the cheaper of the 
     two. Apples are cheaper, so you decide to buy apples.
          This may seem a fairly simple process, and it would be easy 
     to design a computer program to make the decision for you, based 
     purely on price. Of course, computer don;t think like we do. 
     Everything must be reduced to simple binary maths for them to be 
     able to make any sort of sense out of a problem. Basic makes it a 
     little easier for us by using the concepts of TRUE and FALSE.
          The ability to test the truth of a statement and thus decide 
     on one course of action in preference to another forms the 
     backbone of computer programming. In essence, all programs can be 
     said to contain a set of simple true or false conditions. As an 
          IF <condition> IS TRUE THEN <do this> ELSE <do this>
     This is called an IF... THEN... ELSE statement after the keywords 
     it contains. We'll look at this statement in more detail later, 
     but first lets have a look at how Basic works out the truth or 
     falsehood of a statement.
     PROGRAM 1:
     10     A = 10
     20     B = 20
     30     PRINT "A equals B:"; A=B
     40     PRINT "A does not equal B:"; A<>B
     50     PRINT "A is bigger than B:"; A>B
     60     PRINT "B is bigger than A:"; A<B
          Basic should respond with:
     A equals B: 0
     A does not equal B: -1
     A is bigger than B: 0
     B is bigger than A: -1
          As you can see, the value 0 (zero) is taken to mean a 
     statement is False, while a value of -1 (minus one) shows that the 
     statement is True.
          This example raises one very important point. What are those 
     little symbols and what do they actually mean? Well, the symbols 
     are called Relational Operators, and they govern how Basic tests a 
     statement for truth. A full list is given below.
     =     Equals                     A is the same as B
     <>    Not Equals                 A is not the same as B
     >     Greater than               A is more than B
     <     Less than                  A is smaller than B
     >=    Greater than or Equal to   A is more than or the same as B
     <=    Less than or Equal to      A is less than or the same as B
          The value of A is always written to the left of the operator, 
     while B is always to the right of the symbol.
          Many of you may already be familiar with most of these 
     symbols. They are the same as those used in Algebra, except for 
     the Not Equals symbol. Algebra uses a crossed out equals sign, 
     which isn't available from the ST keyboard so '<>' was substituted 
                         YOU'RE A LIAR!
          In the example program above, Basic applied its built-in lie 
     detector to each of the statements in turn. For the first 
     statement, A=B, it already knew that A had a value of 10, while B 
     was set to 20. So the statement that A equals B is quite clearly 
     false, and Basic printed 0 to indicate this.
          However, A is indeed less than B, so the statement A<B is 
     true and go a -1 result to indicate the it is true.
          This simple process of truth and falsehood is how basic 
     arrives at all its conclusions. No matter how complex a logical 
     expression may appear at first, Basic simply breaks it down into 
     manageable chunks and then checks the validity of each chunk in 
          One interesting point to remember that while basic always 
     treats a value of 0 to be false, any non-zero number is taken to 
     be true.
                         A TESTING TIME
          The actual decision making system itself is something of a 
     milestone in basic programming. In any program you are ever likely 
     to write, it essential that you are able to test the truth of 
     expressions and variables.
          Take one of the most common types of programs, a game. One of 
     the most mundane and boring jobs is writing the so-called "user 
     interface". This is the part of the game that allows the player to 
     move, say, their spaceship around the screen and fire their lasers 
     to destroy the enemy.
          Without the ability to test conditions for truth, it is 
     impossible to tell if the joystick has been moved or the fire 
     button pressed. Or, on a more basic level, how do you check the 
     values from an INPUT statement to see if they are within the 
     required limits?
          Basic provides all the tools to check for truth in the 
     misleadingly simple IF THEN ELSE statement, which appeared briefly 
     earlier in the article. The syntax of the statement is:
          IF      <condition>      THEN      <statement>
          The ELSE portion of the statement is optional, but if it is 
     used then the syntax is:
          IF      <condition>      THEN     <statement 1>
          ELSE                              <statement 2>
          Before we go into any more detail about IF THEN ELSE 
     statements, try this sample program, which combines the FOR... 
     NEXT loop we learned last month with testing for truth
     10     FOR A=1 TO 20
     20     PRINT A
     40     NEXT
          The operation of this program is quite simple. Every time the 
     NEXT statement increments the counter A, the IF statement checks 
     to see if A is equal to 10 (ie the condition is true). When A is 
     equal to 10 then it prints the message. Now try altering the 
     program so line 10 reads:
     10     FOR A=1 TO 20 STEP 2
          When this program is run, the message "HALF WAY THROUGH!" is 
     never printed. This occurs because the value of A never exactly 
     equals 10, so the condition is never met and the IF statement 
     never becomes true.
          By now, you should be able to see how the IF statement works 
     in its simplest form. When the line is executed, Basic tests the 
     condition to see if it is satisfied. If it is, then a value of -1 
     (true) is returned and everything after the THEN statement is 
     executed. If the condition is not satisfied and 0 is returned, 
     then execution of the program continues as normal.
          A more useful way of using IF is for checking the range of an 
     input from the keyboard.  Try this as an example:
     10     INPUT "Type any number up to 10";n
     20     IF n>10 THEN PRINT "TOO BIG! TRY AGAIN.": GOTO 10
     30     PRINT "YOU ENTERED ";n
          In this example, the IF statement is being used to ensure 
     that the number typed in by the user conforms to the desired 
     range. The GOTO statement after the IF statement simply forces the 
     program to be run again. Because it is on the same line as the IF 
     command, then it will only be executed if the condition is true.
          Program Three works perfectly well until you need a range of 
     numbers, say from 0 to 10. When setting up a condition to test a 
     range of numbers, it is important to remember that numbers can be 
     positive as well as negative. If you wanted a positive number but 
     got a negative, it could do strange things to your program.
          You could use a second IF statement to trap numbers that were 
     to small, like this:
     25     IF n<0 THEN PRINT "TOO SMALL! TRY AGAIN.": GOTO 10
          This may work, but it is not very neat. Your program has just 
     grown by a line, and it is not always necessary to explain the 
     fault in detail. Fortunately, the Conditional Operators we saw 
     earlier will allows us to make just one IF command to the whole 
     thing. Replace line 20 with the following, and remove line 25.
     20     IF n< OR n>10 THEN PRINT "OUT OF RANGE!": GOTO 10
          Now the program works well and traps any numbers that are 
     either too big or too small.
                         BEFORE NEXT TIME...
          Yes, but what does that OR statement do..? This is a very 
     good question, and next article I'll be looking at the True OR 
     False table, and introducing more Logical Operators to go with it. 
     In the mean time, try experimenting with the way IF THEN ELSE 
     statements can be used.


(C) Marko, Suomen Atari-sivut / ArkiSTo 2003