home *** CD-ROM | disk | FTP | other *** search
/ No Fragments Archive 10: Diskmags / nf_archive_10.iso / MAGS / ST_USER / 1991 / USERSP91.MSA / TEXT_BASIC.DOC < prev    next >
Encoding:
Text File  |  1991-07-16  |  9.9 KB  |  224 lines

  1.           Hello, and welcome to the ninth part of our on-going Basic 
  2.      Tutorial. This month, Peter Phillips is taking a detailed look at 
  3.      decision making with in Basic.
  4.  
  5.                             FIRST STEPS IN BASIC
  6.  
  7.           Before we embark on this months article proper, remember the 
  8.      problem I set at the end of last month? Why does B = 100, when its 
  9.      limiting value is just 10? Well, for those of you who haven't 
  10.      worked it out for yourselves, the answer is quite straightforward. 
  11.      The loop controlled by counter B is nested inside the loop 
  12.      controlled by the value of A, which is also 10. Thus the B loop is 
  13.      actually executed 10 times before the limiting value is exceeded, 
  14.      and 10 times 10 is 100.
  15.  
  16.           While this may seem a pointless little exercise, it does 
  17.      demonstrate the dangers of nesting loops. Unless you understand 
  18.      precisely what the effects of one loop on another may be, you can 
  19.      end up with some results you didn't expect.
  20.  
  21.                          DECISIONS, DECISIONS!
  22.  
  23.           We all make decisions all the time in our everyday lives. You 
  24.      made a decision to buy ST User, and another to read this column. 
  25.      These decisions are arrived at by careful consideration, and 
  26.      weighing up various factors.
  27.  
  28.           For instance, say you went to market and decided you wanted 
  29.      to buy some fruit. A simple decision between apples and pears 
  30.      could be made on price alone, you decide to buy the cheaper of the 
  31.      two. Apples are cheaper, so you decide to buy apples.
  32.  
  33.           This may seem a fairly simple process, and it would be easy 
  34.      to design a computer program to make the decision for you, based 
  35.      purely on price. Of course, computer don;t think like we do. 
  36.      Everything must be reduced to simple binary maths for them to be 
  37.      able to make any sort of sense out of a problem. Basic makes it a 
  38.      little easier for us by using the concepts of TRUE and FALSE.
  39.  
  40.           The ability to test the truth of a statement and thus decide 
  41.      on one course of action in preference to another forms the 
  42.      backbone of computer programming. In essence, all programs can be 
  43.      said to contain a set of simple true or false conditions. As an 
  44.      example:
  45.  
  46.           IF <condition> IS TRUE THEN <do this> ELSE <do this>
  47.  
  48.      This is called an IF... THEN... ELSE statement after the keywords 
  49.      it contains. We'll look at this statement in more detail later, 
  50.      but first lets have a look at how Basic works out the truth or 
  51.      falsehood of a statement.
  52.  
  53.      PROGRAM 1:
  54.  
  55.      10     A = 10
  56.      20     B = 20
  57.      30     PRINT "A equals B:"; A=B
  58.      40     PRINT "A does not equal B:"; A<>B
  59.      50     PRINT "A is bigger than B:"; A>B
  60.      60     PRINT "B is bigger than A:"; A<B
  61.  
  62.           Basic should respond with:
  63.  
  64.      A equals B: 0
  65.      A does not equal B: -1
  66.      A is bigger than B: 0
  67.      B is bigger than A: -1
  68.  
  69.           As you can see, the value 0 (zero) is taken to mean a 
  70.      statement is False, while a value of -1 (minus one) shows that the 
  71.      statement is True.
  72.  
  73.           This example raises one very important point. What are those 
  74.      little symbols and what do they actually mean? Well, the symbols 
  75.      are called Relational Operators, and they govern how Basic tests a 
  76.      statement for truth. A full list is given below.
  77.      
  78.      =     Equals                     A is the same as B
  79.      <>    Not Equals                 A is not the same as B
  80.      >     Greater than               A is more than B
  81.      <     Less than                  A is smaller than B
  82.      >=    Greater than or Equal to   A is more than or the same as B
  83.      <=    Less than or Equal to      A is less than or the same as B
  84.  
  85.           The value of A is always written to the left of the operator, 
  86.      while B is always to the right of the symbol.
  87.  
  88.           Many of you may already be familiar with most of these 
  89.      symbols. They are the same as those used in Algebra, except for 
  90.      the Not Equals symbol. Algebra uses a crossed out equals sign, 
  91.      which isn't available from the ST keyboard so '<>' was substituted 
  92.      instead.
  93.  
  94.                          YOU'RE A LIAR!
  95.  
  96.           In the example program above, Basic applied its built-in lie 
  97.      detector to each of the statements in turn. For the first 
  98.      statement, A=B, it already knew that A had a value of 10, while B 
  99.      was set to 20. So the statement that A equals B is quite clearly 
  100.      false, and Basic printed 0 to indicate this.
  101.  
  102.           However, A is indeed less than B, so the statement A<B is 
  103.      true and go a -1 result to indicate the it is true.
  104.  
  105.           This simple process of truth and falsehood is how basic 
  106.      arrives at all its conclusions. No matter how complex a logical 
  107.      expression may appear at first, Basic simply breaks it down into 
  108.      manageable chunks and then checks the validity of each chunk in 
  109.      turn.
  110.  
  111.           One interesting point to remember that while basic always 
  112.      treats a value of 0 to be false, any non-zero number is taken to 
  113.      be true.
  114.  
  115.  
  116.                          A TESTING TIME
  117.  
  118.           The actual decision making system itself is something of a 
  119.      milestone in basic programming. In any program you are ever likely 
  120.      to write, it essential that you are able to test the truth of 
  121.      expressions and variables.
  122.  
  123.           Take one of the most common types of programs, a game. One of 
  124.      the most mundane and boring jobs is writing the so-called "user 
  125.      interface". This is the part of the game that allows the player to 
  126.      move, say, their spaceship around the screen and fire their lasers 
  127.      to destroy the enemy.
  128.  
  129.           Without the ability to test conditions for truth, it is 
  130.      impossible to tell if the joystick has been moved or the fire 
  131.      button pressed. Or, on a more basic level, how do you check the 
  132.      values from an INPUT statement to see if they are within the 
  133.      required limits?
  134.  
  135.           Basic provides all the tools to check for truth in the 
  136.      misleadingly simple IF THEN ELSE statement, which appeared briefly 
  137.      earlier in the article. The syntax of the statement is:
  138.  
  139.           IF      <condition>      THEN      <statement>
  140.  
  141.           The ELSE portion of the statement is optional, but if it is 
  142.      used then the syntax is:
  143.  
  144.           IF      <condition>      THEN     <statement 1>
  145.           ELSE                              <statement 2>
  146.  
  147.           Before we go into any more detail about IF THEN ELSE 
  148.      statements, try this sample program, which combines the FOR... 
  149.      NEXT loop we learned last month with testing for truth
  150.  
  151.      PROGRAM TWO
  152.  
  153.      10     FOR A=1 TO 20
  154.      20     PRINT A
  155.      30     IF A=10 THEN PRINT "HALF WAY THROUGH!"
  156.      40     NEXT
  157.  
  158.           The operation of this program is quite simple. Every time the 
  159.      NEXT statement increments the counter A, the IF statement checks 
  160.      to see if A is equal to 10 (ie the condition is true). When A is 
  161.      equal to 10 then it prints the message. Now try altering the 
  162.      program so line 10 reads:
  163.  
  164.      10     FOR A=1 TO 20 STEP 2
  165.  
  166.           When this program is run, the message "HALF WAY THROUGH!" is 
  167.      never printed. This occurs because the value of A never exactly 
  168.      equals 10, so the condition is never met and the IF statement 
  169.      never becomes true.
  170.  
  171.           By now, you should be able to see how the IF statement works 
  172.      in its simplest form. When the line is executed, Basic tests the 
  173.      condition to see if it is satisfied. If it is, then a value of -1 
  174.      (true) is returned and everything after the THEN statement is 
  175.      executed. If the condition is not satisfied and 0 is returned, 
  176.      then execution of the program continues as normal.
  177.  
  178.           A more useful way of using IF is for checking the range of an 
  179.      input from the keyboard.  Try this as an example:
  180.  
  181.      PROGRAM THREE
  182.  
  183.      10     INPUT "Type any number up to 10";n
  184.      20     IF n>10 THEN PRINT "TOO BIG! TRY AGAIN.": GOTO 10
  185.      30     PRINT "YOU ENTERED ";n
  186.  
  187.           In this example, the IF statement is being used to ensure 
  188.      that the number typed in by the user conforms to the desired 
  189.      range. The GOTO statement after the IF statement simply forces the 
  190.      program to be run again. Because it is on the same line as the IF 
  191.      command, then it will only be executed if the condition is true.
  192.  
  193.           Program Three works perfectly well until you need a range of 
  194.      numbers, say from 0 to 10. When setting up a condition to test a 
  195.      range of numbers, it is important to remember that numbers can be 
  196.      positive as well as negative. If you wanted a positive number but 
  197.      got a negative, it could do strange things to your program.
  198.  
  199.           You could use a second IF statement to trap numbers that were 
  200.      to small, like this:
  201.  
  202.      25     IF n<0 THEN PRINT "TOO SMALL! TRY AGAIN.": GOTO 10
  203.  
  204.           This may work, but it is not very neat. Your program has just 
  205.      grown by a line, and it is not always necessary to explain the 
  206.      fault in detail. Fortunately, the Conditional Operators we saw 
  207.      earlier will allows us to make just one IF command to the whole 
  208.      thing. Replace line 20 with the following, and remove line 25.
  209.  
  210.      20     IF n< OR n>10 THEN PRINT "OUT OF RANGE!": GOTO 10
  211.  
  212.           Now the program works well and traps any numbers that are 
  213.      either too big or too small.
  214.  
  215.  
  216.                          BEFORE NEXT TIME...
  217.  
  218.           Yes, but what does that OR statement do..? This is a very 
  219.      good question, and next article I'll be looking at the True OR 
  220.      False table, and introducing more Logical Operators to go with it. 
  221.      In the mean time, try experimenting with the way IF THEN ELSE 
  222.      statements can be used.
  223.  
  224.