      
      
                           SINCLAIR ZX SPECTRUM
      
      
      
      
      
      CONTENTS
      
      
      Chapter 1:      Introduction
      A guide to the ZX Spectrum  keyboard  and a description of  the
      display.
      
      Chapter 2:      Basic programming concepts
      Programs, line numbers, editing programs using ,  and ED
      IT, RUN, LIST, GO TO, CONTINUE, INPUT, NEW, REM, PRINT, STOP in
      INPUT  data, BREAK
      
      Chapter 3:      Decisions
      IF, STOP, =, <, >, <=, >=, <>
      
      Chapter 4:      Looping
      FOR, NEXT, TO, STEP. Introducing FOR-NEXT loops
      
      Chapter 5:      Subroutines
      GO SUB, RETURN
      
      Chapter 6:      READ, DATA, RESTORE
      
      Chapter 7:      Expressions
      Mathematical expressions using +, -,  *, /, scientific notation
      and variable names.
      
      Chapter 8:      Strings
      Handling strings and slicing.
      
      Chapter 9:      Functions
      User-definable functions and others  readily  available on  the
      ZX Spectrum using DEF, LEN, STR$, VAL, SGN, ABS, INT, SQR, FN
      
      Chapter 10:     Mathematical functions
      Including simple trigonometry: ,  PI,  EXP, LN, SIN, COS,  TAN,
      ASN, ACS, ATN
      
      Chapter 11:     Random numbers
      Using RANDOMIZE and RND.
      
      Chapter 12:     Arrays
      Strings and numeric arrays - DIM.
      
      Chapter 13:     Conditions
      Logical expressions: AND, OR, NOT.
      
      Chapter 14:     The character set
      A look at the ZX  character  set  including graphics and how to
      construct your own graphic characters:  CODE, CHR$, POKE, PEEK,
      USR, BIN
      
      Chapter 15:     More about PRINT and INPUT
      Some more complicated uses of  these commands using separators:
      , ; ' , TAB, AT, LINE and CLS.
      
      Chapter 16:     Colours
      INK, PAPER, FLASH, BRIGHT, INVERSE, OVER, BORDER
      
      Chapter 17:     Graphics
      PLOT, DRAW, CIRCLE, POINT
      
      Chapter 18:     Motion
      Animated graphics using PAUSE, INKEY$ and PEEK.
      
      Chapter 19:     BEEP
      The sound capabilities of the ZX Spectrum using BEEP.
      
      Chapter 20:     Tape Storage
      How to  store  your  programs  on  cassette  tape:  SAVE, LOAD,
      VERIFY, MERGE
      
      Chapter 21:     The ZX Printer
      LLIST, LPRINT, COPY
      
      Chapter 22:     Other equipment
      Connecting the ZX Spectrum to other machines and devices.
      
      Chapter 23:     IN and OUT
      Input/Output ports and their uses: IN, OUT
      
      Chapter 24:     The memory
      A look at the internal workings of the ZX Spectrum: CLEAR
      
      Chapter 25:     The system variables
      
      Chapter 26:     Using machine code
      Introducing USR with a numeric argument
      _______________________________________________________________
      
      APPENDIX A:     The character set
      
      APPENDIX B:     Reports
      
      APPENDIX C:     A description of the ZX Spectrum for reference
      
      APPENDIX D:     Example programs
      
      APPENDIX E:     Binary and hexadecimal
      
      
      
      CHAPTER 1
      Introduction
      
      
        Whether  you  read  the  Introductory  book  first,  or  came
      straight here, you should  be  aware  that  commands are obeyed
      straight away, and instructions  begin  with  a line number and
      are stored away for later.  You  should  also  be aware of  the
      commands: PRINT, LET,  and  INPUT  (which  can  be  used on all
      machines that use BASIC), and BORDER, PAPER and BEEP (which are
      used on the Spectrum).
      
        This BASIC manual starts  by  repeating  some things given in
      the introductory  booklet, but in much more detail, telling you
      exactly what you can and  cannot  do.  You  will also find some
      exercises at the end of each  chapter. Don't ignore these; many
      of them illustrate points that are  hinted at in the text. Look
      through them, and do any  that  interest  you,  or that seem to
      cover ground that you don't understand properly.
      
        Whatever else you do, keep  using  the  computer. If you have
      the question "what does it do if I tell it such and such?" Then
      the answer is easy: type  it  in  and  see. Whenever the manual
      tells you to  type  something  in,  always  ask yourself, "what
      could I type instead?", and try  out  your replies. The more of
      your own programs you write, the better you will understand the
      computer.
      
        At the end of  this  programming  manual are some appendices.
      These include sections on the way  the memory is organised, how
      the computer  manipulates  numbers,  and  a  series  of example
      programs illustrating the power of the ZX Spectrum.
      
      The keyboard
      
      
        ZX Spectrum characters comprise  not  only the single symbols
      (letters,  digits,  etc.),   but   also   the  compound  tokens 
      (keywords,  function  names,   etc.)   and   all    these   are 
      entered  from the keyboard rather than being  spelled  out.  To 
      obtain  all these  functions  and  commands,  some   keys  have 
      five or more distinct meanings, given partly  by  shifting  the 
      keys  (i.e. pressing  either the CAPS SHIFT key or  the  SYMBOL 
      SHIFT key at the same time as the required one ) and  partly by 
      having the machine in different modes.
      
        The mode is indicated by  the  cursor, a flashing letter that
      shows where the   next  character  from  the  keyboard  will be
      inserted:
      
       K (for keywords) mode automatically  replaces  L mode when the
      machine is expecting a  command  or  program  line (rather than
      INPUT data), and from its   position  on  the  line it knows it
      should expect a  line  number  or  a  keyword.  This  is at the
      beginning of the  line,  or  just  after  THEN,  or just after: 
      (except  in a  string).  If  unshifted,  the  next  key will be 
      interpreted as either a  keyword  (written  on  the keys), or a 
      digit.
      
         L (for letters) mode normally occurs  at all other times. If
      unsolved, the next key will be   interpreted as the main symbol 
      on that key, in  lower case for letters.
      
         In both K and  L  modes,  SYMBOL  SHIFT  and  a  key will be
      interpreted as the subsidiary red character on the key and CAPS
      SHIFT with a  digit  key  will  be  interpreted  as the control
      function written in white above the  key  CAPS SHIFT with other
      keys does not affect the keywords in  K mode, and in L  mode it
      converts lower case to capitals.
      
         C (for capitals) mode is a  variant  of  L mode in which all
      letters appear as capitals. CAPS  LOCK  causes  a change from L
      mode to C mode or back again.
      
         E  (for  extended)  mode  is   used  for  obtaining  further
      characters, mostly tokens. It occurs  after both shift keys are
      pressed together, and lasts  for  one  key  depression only. In
      this mode, a letter  gives  one  character  or  token (shown in
      green above it) if unshifted,  and  another (shown in red below
      it) if pressed with either shift. A  digit key gives a token if
      pressed with SYMBOL SHIFT; otherwise  it gives a colour control
      sequence.
      
          G for graphics) mode occurs  after GRAPHICS (CAPS SHIFT and
      9) is pressed, and lasts  until  it  is  pressed  again or 9 is
      pressed on its own. A  digit  key  will  give a mosaic graphic,
      quit GRAPHICS or DELETE, and  each   of  the  letter keys apart 
      from V, W, X, Y and Z, will give a user-defined graphic.
      
        If any key is held down for  more  than about 2 or 3 seconds,
      it will start auto-repeating.
      
        Keyboard input appears in the bottom half of the screen as it
      is typed, each  character  (single  symbol  or  compound token)
      being inserted just before the cursor.  The cursor can be moved
      left with CAPS SHIFT and 5, or right with CAPS SHIFT and 8. The
      character before the cursor  can  be  deleted with DELETE (CAPS
      SHIFT and 9). (Note: the  whole  line  can be deleted by typing
      EDIT (CAPS SHIFT and 1) followed by ENTER.)
      
        When ENTER is pressed, the line is executed, entered into the
      program, or  used  as  INPUT  data  as  appropriate,  unless it
      contains a syntax error. In this case a flashing ? appears next
      to the error.
      
        As program lines are entered,  a  listing is displayed in the
      top half of the  screen.  The  last  line entered is called the
      current line and is indicated  by   the  symbol  >; this can be
      moved by using the keys   (CAPS  SHIFT  and 6) and  (CAPS SHIFT
      and 7). If EDIT (CAPS SHIFT and 1) is pressed, the current line
      is  brought down to the bottom  part  of  the screen and can be
      edited.
      
        When a command  is  executed  or  a  program  run,  output is
      displayed in the top  half  of  the  screen and remains until a
      program line is entered,  or  ENTER  is  pressed  with an empty
      line, or   is pressed. In   the   bottom  part appears a report 
      giving a code (digit or letter)  referred to in Appendix B. The
      report remains on  the  screen  until  a  key  is  pressed (and
      indicates K mode).
      
        In certain circumstances, CAPS SHIFT  with the SPACE key acts
      as a BREAK, stopping the computer  with  report D or L. This is
      recognised
      
       (i) at the end of a statement while a program is running, or
       (ii) while the  computer  is  using  the  cassette recorder or
      printer.
      
      The television screen
      
        This has 24 lines, each  32  characters  long, and is divided
      into two parts. The top part  is  at most 22 lines and displays
      either a listing or program out  put.  When printing in the top
      part has reached the bottom,  it  all  scrolls  up one line; if
      this would involve losing a line that you have not had a chance
      to see yet, then the  computer  stops with the message scroll?.
      Pressing the keys N, SPACE or  STOP  will make the program stop
      with report D BREAK - CONT repeats;  any other key will let the
      scrolling continue.  The  bottom  part  is  used  for inputting
      commands,  program  lines,  and   INPUT   data,  and  also  for 
      displaying
      g reports. The bottom part starts  off  as two lines (the upper
      one blank), but  it  expands  to  accommodate whatever is typed
      in. When it reaches the current print position in the top half,
      further expansions will make the top half scroll up.
      
      
      
      CHAPTER 2
      Basic Programming Concepts
      
      
      Summary
      
          Programs
          Line numbers
          Editing programs using ,, and EDIT
          RUN, LIST
          GO TO, CONTINUE, INPUT, NEW, REM, PRINT
          STOP in INPUT data
          BREAK
      
      
        Type in these two lines  of  a  computer program to print out
      the sum of two numbers:
      
              20 PRINT a
              10 LET a=10
      
      
      
        As you already know, because  these lines began with numbers,
      they were not obeyed  immediately  but  stored away, as program
      lines. You will also have  noticed  here  that the line numbers
      govern the order of the lines  within the program: this is most
      important when the program is run,  but it is also reflected in
      the order of the lines in the  listing  that you can see on the
      screen now.
      
        So far you have only entered one number, so type
      
       15 LET b=15
      
      and in it goes. It  would  have  been impossible to insert this
      line between the  first two if  they  had been numbered 1 and 2
      instead of 10  and  20  (line  numbers  must  be  whole numbers
      between 1 and 9999), so  that  is  why,  when first typing in a
      program, it is good  practice  to  leave  gaps between the line
      numbers.
      
        Now you need to change line 20 to
      
              20 PRINT a+b
      
        You could type out the replacement  in full, but it is easier
      to use the EDIT facility described in the introductory booklet.
      The > by line 15 is called  the program cursor, and the line it
      points to is the current line.   This  is usually the last line 
      that you entered, but you  can  use  the   or  keys to move the
      program cursor down or up. (Try  it, leaving the program cursor
      eventually at line 20.)
      
        When you press the EDIT key, then  a copy of the current line
      will be displayed at the bottom of the screen - in your case, a
      copy of line 20. Hold down the  key until the L cursor moves to
      the end of the line, and then type
      
              +b       (without ENTER)
      
        The line at the bottom should now read
      
              20 PRINT a+b
      
        Type ENTER and it will  replace  the  old line 20.   Run this
      program using RUN and ENTER and the sum will be displayed.
      
        Run the program again and then type
      
              PRINT a, b
      
        The variables are still  there,  even  though the program has
      finished.
      
        There is a useful method using EDIT  to get rid of the bottom
      part of the screen. Type in  a  load of rubbish (without ENTER)
      and then decide that you don't  want  it  after all. One way to
      delete it is to hold  the  DELETE  key  down  until the line is
      gone; but another way is as follows. If you press EDIT, the rub
      bish at the bottom of the screen  will be replaced by a copy of
      the current line. If you now press ENTER, the current line will
      be put back in the  program  unaltered, leaving the bottom part
      of the screen clear.
      
        If you enter a line by mistake, say
      
              12 LET b=8
      
      it will go  up  into  the  program  and  you  will realise your
      mistake. To delete this unnecessary line, type
      
              12       (with ENTER of course)
      
        You will notice with  surprise  that  the  program cursor has
      gone. You should imagine it as being hidden in between lines 10
      and 15, so if you press  it  will  move up to line 10, while if
      you press  it will move down to line 15.
      
        Type
      
              12       (and ENTER)
      
        Again, the program cursor will be hidden between lines 10 and
      15. Now press   EDIT  and  line  15  will  come  down: when the
      program cursor is hidden  between  two  lines, EDIT brings down
      the next line following  the  new  line  number.  Type ENTER to
      clear the bottom part of the screen.
      
        Now type
      
              30      (and ENTER)
      
        This time, the program cursor is  hidden after the end of the
      program: and if you press  EDIT,  then  line 20 will be brought
      down.
      
        Lastly, type
      
              LIST 15
      
       You will now see on the screen
      
              15 LET b=15
              20 PRINT a+b
      
        Line 10 has vanished from the screen. but it is still in your
      program - which  you  can  prove  by  pressing  ENTER. The only
      effects of LIST 15 are to produce a listing that starts at line
      15. and to put the program cursor at line 15.
       If you have a very long program,  then LIST will probably be a
      more useful way of moving the program cursor than  and .
      
        This illustrates another use  of  line  numbers:  they act as
      names for the program  lines  so  that  you  can refer to them,
      rather like the way in which variables have names.
      
        LIST on its own makes the listing start at the beginning of a
      program.
      
        Another command seen in the introductory booklet is:
      
              NEW
      
        This erases any old programs  and  variables in the computer.
      Now carefully type in  this  program,  which changes Fahrenheit
      temperatures to Centigrade.
      
              10 REM temperature conversion
              20 PRINT "deg F", "deg C"
              30 PRINT
              40 INPUT "Enter deg F", F
              50 PRINT F,(F-32)*5/9
              60 GO TO 40
      
        You will need to type the words in line 10. Also, although GO
      TO has a space in it, it is really all one keyword (on G).
      
        Now run it. You will see  the  headings printed on the screen
      by line 20,  but   what  happened  to  line  10? Apparently the
      computer has completely ignored it.
       Well, it has. REM in line  10  stands for remark, or reminder,
      and is there solely to remind  you of what the program does. A
      REM command consists of REM followed  by anything you like, and
      the computer will ignore it right up to the end of the line.
      
        By now, the computer has got to  the INPUT command on line 40
      and is waiting  for you to type in a value for the variable F -
      you can tell this because  where  you  might  have expected a K
      cursor there is instead an  L  cursor. Enter a number; remember
      ENTER. Now the  computer  has   displayed   the   result and is 
      waiting for another number. This is  because  of line 60, GO TO 
      40, which means exactly  what   it   says.  Instead  of running 
      out of program and stopping, the computer  jumps  back  to line 
      40 and starts again. So,  enter  another  temperature.  After a 
      few more of these you  might  be  wondering if the machine will 
      ever get bored with  this,  it  won't.  Next   time it asks for 
      another number, type STOP. The   computer  comes  back  with  a 
      report H STOP in INPUT in  line  40:1, which tells  you  why it 
      stopped, and where (in the first command of line 40).
      
        If you want to continue the program type
      
              CONTINUE
      
      and the computer will ask you for another number.
      
        When CONTINUE is used the  computer remembers the line number
      in the last report that it sent  you,  as  long as it was not 0
      OK, and jumps back to  that  line:  in  our case, this involves
      jumping to line 40, the INPUT command.
      
        Replace line 60 by GO  TO  31  -  it will make no perceptible
      difference to the  running of  the  program. If the line number
      in a GO TO command refers to a non-existent line, then the jump
      is to the next line after  the  given number. The same goes for
      RUN; in fact RUN on its own actually means RUN 0.
      
        Now type in numbers  until  the  screen  starts getting full.
      When it is full, the computer  will  move  the whole of the top
      half of the  screen  up  one  line  to   make  room, losing the
      heading off the top. This is called scrolling.
      
        When you are tired of this,  stop  the program using STOP and
      get the listing  by pressing ENTER.
      
        Look at the PRINT statement  on  line  50. The punctuation in
      this -  the  comma   (,)  is  very  important,  and  you should
      remember that it  follows  much  more  definite  rules than the
      punctuation in English.
      
        Commas are used to make the printing start either at the left
      hand margin, or in the middle of the screen, depending on which
      comes next. Thus in line 5  0,  the comma causes the centigrade
      temperature to be printed in  the  middle  of  the line. With a
      semicolon, on the other  hand,  the  next  number  or string is
      printed immediately after the preceding  one.  You can see this
      in line 50, if the comma is replaced by a semicolon.
      
        Another punctuation mark  you  can  use  like  this  in PRINT
      commands is the apostrophe (').  This makes whatever is printed
      next appear at the beginning of the next line on the screen but
      this happens anyway at the  end  of  each PRINT command, so you
      will not need the apostrophe very  much.  This is why the PRINT
      command in line 50 always  starts  its  printing on a new line,
      and it is also why  the  PRINT  command  in  line 30 produces a
      blank line.
      
        If you want to inhibit this,  so that after one PRINT command
      the next one carries on the  same  line, you can put a comma
      or semicolon at the end of  the  first.  To see how this works,
      replace line 50 in turn by each of
      
              50 PRINT F,
              50 PRINT F;
      and
              50 PRINT F
      
      and run each version - for good measure you could also try
      
              50 PRINT F'
      
        The one with the comma spreads everything out in two columns,
      that  with  the   semicolon  crams  everything  together,  that
      without either allows a line for  each  number and so does that
      with the apostrophe - the  apostrophe  gives  a new line of its
      own, but inhibits the automatic one.
      
       Remember the difference between commas and semicolons in PRINT
      commands; also, do not confuse  them  with  the colons (:) that
      are used to separate commands  in a single line.
      
       Now type in these extra lines:
      
              100 REM this polite program remembers your name
              110 INPUT n$
              120 PRINT "Hello ";n$;"!"
              130 GO TO 110
      
        This is a separate program  from  the  last  one, but you can
      keep them both in  the computer  at  the  same time. To run the
      new one, type
      
              RUN 100
      
        Because this program inputs a string  instead of a number, it
      prints out two  string quotes - this  is a reminder to you, and
      it usually saves you some typing as  well. Try it once with any
      alias you care to make up for yourself.
      
        Next time round, you will  get  two  string quotes again. But
      you don't have to use them if  you don't want to. Try this, for
      example. Rub them out (with  and DELETE twice), and type
      
              n$
      
        Since there are no string quotes,  the computer knows that it
      has to do some  calculation: the calculation in this case is to
      find the value of  the  string   variable  called  n$, which is
      whatever name you happen to have  typed  in last time round. Of
      course, the INPUT statement acts  like  LET n$=n$, so the value
      of n$ is unchanged.
      
        The next time round, for comparison, type
      
              n$
      
      again, this time without  rubbing  out  the string quotes. Now,
      just to confuse you, the variable n$ has the value "n$".
      
        If you want to use STOP for string input, you must first move
      the cursor back to the beginning of the line, using .
      
        Now look back at that RUN  100  we  had earlier on. That just
      jumps to line 100, so couldn't we  have said GO TO 100 instead?
      In this case, it so happens  that  the answer is yes; but there
      is a difference. RUN 100 first  of all clears all the variables
      and the screen, and after that works just like GO TO 100. GO TO
      100 doesn't clear anything. There  may  well be occasions where
      you want to run a program  without clearing any variables; here
      GO TO would be necessary  and RUN could be disastrous, so it is
      better not to get into the habit of automatically typing RUN to
      run a program.
      
        Another difference is that you  can  type  RUN without a line
      number, and it starts off at the  first line in the program. GO
      TO must always have a line number.
      
        Both these programs stopped  because  you  typed  STOP in the
      input line; sometimes - by mistake  -  you write a program that
      you can't stop and won't stop itself. Type
      
              200 GO TO 200
              RUN 200
      
        This looks all set to go on for ever unless you pull the plug
      out; but there is a less  drastic remedy. Press CAPS SHIFT with
      the SPACE key, which has  BREAK  written  above it. The program
      will stop, saying L BREAK into program.
      
        At the end of every  statement,  the  program looks to see if
      these keys are pressed; and  if  they  are,  then it stops. The
      BREAK key can also be used when  you are in the middle of using
      the cassette recorder or the printer,  or various other bits of
      machinery that you can attach  to  the  computer - just in case
      the computer is waiting for  them  to  do something but they're
      not doing it.
      
        In these cases there is  a  different  report, D BREAK - CONT
      repeats. CONTINUE in this case (and in fact in most other cases
      too) repeats the statement where  the  program was stopped; but
      after  the  report  L  BREAK  into  program,  CONTINUE  carries
      straight on with  the  next  statement  after  allowing for any
      jumps to be made.
      
        Run the name program again  and  when  it  asks you for input
      type
      
              n$       (after removing the quotes)
      
       n$ is an undefined variable  and  you  get  an error report 2:
      Variable not found.
      
        If you now type
      
              LET n$="something definite"
      
      (which has its own report of 0 OK, 0:1) and
      
              CONTINUE
      
      you will find that you  can  use  n$  as input data without any
      trouble.
      
        In this case CONTINUE does  a  jump  to  the INPUT command in
      line 110. It  disregards  the  report  from  the  LET statement
      because that said 'OK', and  jumps  to  the command referred to
      in the previous report, the first command in line 11 0. This is
      intended to be useful. If a  program stops over some error then
      you  can do all sorts of  things  to  fix it, and CONTINUE will
      still work afterwards.
      
        As we  said  before,  the  report  L  BREAK  into  program is
      special, because after it CONTINUE  does not repeat the command
      where the program stopped.
      
        The automatic listings (the ones that are not the result of a
      LIST command but occur after entering a new line) may well have
      you puzzled. If you type in  a  program  with 50 lines, all REM
      statements,
      
               1 REM
               2 REM
               3 REM
                :    :
                :    :
              49 REM
              50 REM
      
      then you will be able to experiment.
      
        The first thing to remember is that the current line (with >)
      will always appear on the screen, and usually near the middle.
      
        Type
      
              LIST       (and ENTER of course)
      
      and when it asks scroll? (Because  it has filled up the screen)
      press n for 'No'. The computer  will  give the report D BREAK -
      CONT repeats as though you had  typed  BREAK. You might at some
      stage find out what happens if  you  press you instead of n; n,
      SPACE and STOP count  as  No,  while  everything else counts as
      Yes.
      
        Now press ENTER again  to  get  an  automatic listing and you
      should see lines 1 to 22 on the screen. Now type
      
              23 REM
      
      and you get lines 2 to 23 on the screen; type
      
              28 REM
      
      and you get lines 7  to  28.  (In  both  cases, by typing a new
      line, you have moved the program  cursor  so that a new listing
      has been made.)
      
        Maybe this looks a little  arbitrary  to  you. It is actually
      trying to give  you  exactly  what  you  want, although, humans
      being unpredictable creatures, it doesn't always guess right.
      
        The computer keeps a record not only of the current line, the
      one that has to appear on the  screen, but also the top line on
      the screen. When it tries to make a listing, the first thing it
      does is compare the top line  to  the  current line. If the top
      line comes after, then there is no point in starting the re, so
      it uses the current  line  for  a  new  top  line and makes its
      listing. Otherwise, its method is  to  start making the listing
      from the top line, and carry on until it has listed the current
      line, scrolling if necessary.
      
        However, it first does a  rough  calculation  to see how long
      this would take,  and if the  answer  is much too long, then it
      moves the top line down to be a lot closer to the current line.
      Now, having worked out its  top  line,  it start s listing from
      there. If, when it reaches the end of the program or the bottom 
      of the screen,  the  current  line  has  been listed, then it
      stops. Otherwise, it scrolls until  the  current line is on the
      screen, and for each extra line that  it lists it moves the top
      line  down  one  so  that   the   top   line  drifts  into  the
      neighbourhood of the current line.
      
        Experiment with moving the current line about by typing
      
              line number REM
      
        LIST moves  the  current  line  but  not  the  top  line,  so
      subsequent listings might be different. For instance, type
      
              LIST
      
      to get the LIST listing and then press ENTER again to make line
      0 the top line. You should  have  lines  1 to 22 on the screen.
      Type
      
              LIST 22
      
      which gives you lines 22 to 43; when you press ENTER again, you
      get back lines 1 to 22. This  tends to be more useful for short
      programs than for long ones.
      
        Using the program full of REMs above, type
      
              LIST
      
      and then n when it asks you scroll?. Now type
      
              CONTINUE
      
       CONTINUE is a bit quirky here,  because the bottom part of the
      screen goes blank; but  you  can  restore normality with BREAK.
      The reason is that LIST was  the  first command in the line, so
      CONTINUE  repeats  this  command.   Unfortunately,   the  first
      command in the line is now CONTINUE itself so the computer just
      sits there doing CONTINUE over  and  over  again until you stop
      it.
      
       You can vary this by replacing LIST with
      
              : LIST
      
      for which CONTINUE gives 0  OK  (because  CONTINUE jumps to the
      second command in the line, which is taken to be its end) or
      
              :: LIST
      
      for which CONTINUE  gives  N  Statement  lost (because CONTINUE
      jumps to  the  third  command  in  the  line,  which  no longer
      exists).
      
       You have now seen the statements PRINT, LET, INPUT, RUN, LIST,
      GO TO, CONTINUE, NEW and REM,  and  they can all be used either
      as direct commands or in program lines - this is true of almost
      all commands in ZX Spectrum BASIC.  RUN, LIST, CONTINUE and NEW
      are not usually of much use in a program, but they can be used.
      
      Exercises
      
      1. Put a LIST statement in a  program, so that when you run it,
      it lists itself.
      
      2. Write a program to input  prices  and  print out the tax due
      (at 15 per cent). Put in  PRINT statements so that the computer
      announces what it is going to do,  and asks for the input price
      with extravagant politeness. Modify the program so that you can
      also input the tax rate  (to  allow  for zero ratings or future
      changes).
      
      3. Write a program  to  print  a  running  total of numbers you
      input. (Suggestion : have two variables called total - set to 0
      to begin with - and item.  Input  item,  add it to total, print
      them both, and go round again.)
      
      4. What would CONTINUE and NEW  do  in a program? Can you think
      of any uses at all for this?
      
      
      
      CHAPTER 3
      Decisions
      
      Summary
      
           IF, STOP
           =, <, >, <=, >=, <>
      
      
        All the  programs  we  have  seen  so  far  have  been pretty
      predictable - they went straight  through the instructions, and 
      then went back to the beginning again. This is not very useful.
      In practice the computer  would  be  expected to make decisions
      and act accordingly. The instruction used has the form . . . IF
      something is true, or not true THEN do something else.
      
        For example, use NEW to  clear  the previous program from the
      computer and type in  and  run  this  program. (This is clearly
      meant for two people to play!)
      
              10 REM Guess the number
              20 INPUT a: CLS
              30 INPUT "Guess the number", b
              40 IF b=a THEN PRINT "That is correct": STOP
              50 IF b<a THEN PRINT "That is too small, try again"
              60 IF b>a THEN PRINT "That is too big, try again"
              70 GO TO 30
      
        You can see that an IF statement takes the form
      
      IF condition THEN . . .
      
      where the '. . .' stands  for a sequence of commands, separated
      by colons in the usual way.  The condition is something that is
      going to be worked out as either true or false: if it comes out
      as true then the statements in the  rest of the line after THEN
      are executed, but  otherwise  they  are  skipped  over, and the
      program executes the next instruction.
      
        The simplest conditions compare  two  numbers or two strings:
      they can test whether two numbers  are  equal or whether one is
      bigger than the other; and  they  can  test whether two strings
      are  equal,  or  (roughly)  one   comes  before  the  other  in
      alphabetical order. They use the relations =, <, >, <=, >=
      and <>.
      
        = means 'equals'. Although it is the  same symbol as the = in
      a LET command,  it is used in quite a different sense.
      
        < (SYMBOL SHIFT with R) means 'is less than' so that
      
       1 < 2
       -2 < -1
      - 3 < 1
      
      are all true, but
      
       1 < 0
       0 < -2
      
      are false.
      
        Line 40 compares a and b. If  they are equal then the program
      is halted by the STOP command. The  report at the bottom of the
      screen 9 STOP, statement, 30:3  shows that the third statement,
      or command, in line 30 caused the program to halt, i.e. STOP.
      
        Line 50 determines whether  b  is  less  than  a, and line 60
      whether b is greater than a. If one of these conditions is true
      then the appropriate comment is  printed, and the program works
      its way to line 70  which  tells  the   computer to  go back to 
      line
      30 and start all over again.
      
        The CLS, clear screen, command  in  line  20  was to stop the
      other person seeing what you put in.
      
        So > (SYMBOL SHIFT with  T)  means  'is greater than', and is
      just like < but the other way  round. You can remember which is
      which, because the  thin  end  points  to  the  number  that is
      supposed to be smaller.
      
        <= (SYMBOL SHIFT with Q - do not  type it as < followed by =)
      means 'is less  than or equal to',  so that it is like < except
      that it is true even if the two numbers are equal: thus 2<=2 is
      true, but 2<2 is false.
      
        >= (SYMBOL SHIFT with E) means  'is greater than or equal to'
      and is similarly like >.
      
        <> (SYMBOL  SHIFT  with  W)  means  'is  not  equal  to', the
      opposite in meaning to =.
      
        Mathematicians usually write <=, >= and  <>  as £, and . They
      also write things like '2<3<4' to  mean '2<3 and 3<4', but this
      is not possible in BASIC.
      
       Note: in some versions of BASIC - but not on the ZX Spectrum -
      the IF statement can have the form
      
      IF condition THEN line number
      
      This means the same as
      
      IF condition THEN GO TO line number
      
      
      Exercises
      
      1. Try this program:
      
              10 PRINT "x": STOP: PRINT V
      
       When you run it, it will display x and stop with report 9 STOP
      statement, 10:2. Now type
      
              CONTINUE
      
       You might expect this  to  jump  back  to  the  STOP command -
      CONTINUE usually  repeats  the  statement  referred  to  in the
      report. However, here this  would  not  be very useful, because
      the computer  would  just  stop  again  without  displaying  y.
      Therefore, things are arranged so  that after report 9 CONTINUE
      jumps to the  command  after  the  STOP  command  -  so  in our
      example, after CONTINUE, the computer  prints V and reaches the
      end of the program.
      
      
      
      CHAPTER 4
      Looping
      
      Summary
      
           FOR, NEXT
           TO, STEP
      
      
        Suppose you want to input five numbers and add them together.
      One way (don't type this in  unless you are feeling dutiful) is
      to write
      
               10 LET total=0
               20 INPUT a
               30 LET total=total+a
               40 INPUT a
               50 LET total=total+a
               60 INPUT a
               70 LET total=total+a
               80 INPUT a
               90 LET total=total+a
              100 INPUT a
              110 LET total=total+a
              120 PRINT total
      
       This method is not good  programming  practice. It may be just
      about controllable for five  numbers,  but  you can imagine how
      tedious a program like this to add ten numbers would be, and to
      add a hundred would be just impossible.
      
       Much better is to set up a variable  to count up to 5 and then
      stop the program, like this (which you should type in):
      
              10 LET total=0
              20 LET count=1
              30 INPUT a
              40 REM count=number of times  that  a has been input so
      far
              50 LET total=total+a
              60 LET count=count+1
              70 IF count<=5 THEN GO TO 30
              80 PRINT total
      
        Notice how easy it would be  to  change  line 70 so that this
      program adds ten  numbers, or even a hundred.
      
        This sort of counting is so useful that there are two special
      commands to make  it  easier:  the  FOR  command  and  the NEXT
      command. They  are  always  used  together.  Using  these,  the
      program you have just typed in does exactly the same as
      
              10 LET total=0
              20 FOR c=1 TO5
              30 INPUT a
              40 REM c=number of times that a has been input so far
              50 LET total=total+a
              60 NEXT c
              80 PRINT total
      
        (To get this program from the  previous one, you just have to
      edit lines 20,  40, 60, and 70. TO is SYMBOL SHIFT with F.)
      
        Note that we have changed count to c. The counting variable -
      or control variable of a  FOR  -  NEXT  loop must have a single
      letter for its name.
      
        The effect of this program is  that c runs through the values
      1 (the initial  value), 2, 3, 4 and 5 (the limit), and for each
      one, lines 30,  40  and  50  are   executed.  Then,  when c has
      finished its five values, line 80 is executed.
      
        An extra subtlety to this  is  that the control variable does
      not have to go up by  1  each  time:  you  can change this 1 to
      anything you like by using a STEP  part in the FOR command. The
      most general form for a FOR  command  is FOR control variable =
      initial value TO limit STEP step  where the control variable is
      a single letter, and the initial  value, limit and step are all
      things that the computer can  calculate  as  numbers - like the
      actual numbers themselves, or  sums,  or  the  names of numeric
      variables. So, if you replace line 20 in the program by
      
              20 FOR c=1 TO 5 STEP 3/2
      
      then c will run through the  values  1,  2.5 and 4. Notice that
      you don't have to restrict yourself  to whole numbers, and also
      that the control value does not have to hit the limit exactly -
      it carries on looping as long  as  it  is less than or equal to
      the limit.
      
        Try this program, to print out  the  numbers  from 1 to 10 in
      reverse order.
      
              10 FOR n=10 TO 1 STEP -1
              20 PRINT n
              30 NEXT n
      
        We said before that the program carries on looping as long as
      the control variable is less than or equal to the limit. If you
      work out what this would mean  in  this case. You will see that
      it gives nonsense. The normal  rule  has  to  be modified: when
      the step is negative, the program carries on looping as long as
      the control variable is greater than or equal to the limit.
      
        You must be careful if you  are  running two FOR - NEXT loops
      together, one inside the other.  Try this program, which prints
      out the numbers for a complete set of six spot dominoes.
      
              10 FOR m=0 TO 6         }
              20 FOR n=0 TO m         }               }
              30 PRINT m;":";n;" ";   }       n-loop  }      m-loop
              40 NEXT n               }               }
              50 PRINT                                }
              60 NEXT m                               }
      
       You can see that the  n-loop  is  entirely inside the m-loop -
      they are properly nested. What  must  be  avoided is having two
      FOR - NEXT loops  that  overlap  without  either being entirely
      inside the other, like this:
      
             5 REM this program is wrong
            10 FOR m=0 TO 6              }
            20 FOR n=0 TO m              }    m-loop     }
            30 PRINT m;":";n;" ";        }               }
            40 NEXT m                    }               }     n-loop
      50 PRINT                                     }
            60 NEXT n                                    }
      
        Two FOR - NEXT loops must either  be one inside the other, or
      be completely separate.
      
        Another thing to avoid is jumping into  the middle of a FOR -
      NEXT loop from  the outside.  The  control variable is only set
      up properly when its FOR  statement  is  executed, and if you
      miss this out the NEXT statement will confuse the computer. You
      will probably get an error  report  saying  NEXT without FOR or
      variable not found.
      
        There is nothing whatever to stop you using FOR and NEXT in a
      direct command. For example, try:
      
              FOR m=0 TO 10: PRINT m: NEXT m
      
        You can sometimes use this as  a (somewhat artificial) way of
      getting round the restriction  that  you  cannot GO TO anywhere
      inside a command - because a  command  has no line number. For
      instance,
      
              FOR m=0 TO 1 STEP 0: INPUT a: PRINT a: NEXT m
      
        The step  of  zero  here  makes  the  command  repeat  itself
      forever.
      
        This sort of thing is  not  really recommended, because if an
      error crops up then you have lost  the command and will have to
      type it in again - and CONTINUE will not work.
      
      Exercises
      
      1. A control variable has not just  a name and a value, like an
      ordinary   variable, but also a limit,  a step, and a reference
      to  the  statement  after   the  corresponding  FOR  statement.
      Persuade yourself that when the  FOR  statement is executed all
      this information is available (using  the  initial value as the
      first value the variable takes), and also that this information
      is enough for  the  NEXT  statement  to  know  by  how  much to
      increase the value, whether to  jump  back,  and if so where to
      jump back to.
      
      2. Run the third program above and then type
      
              PRINT c
      
         Why is the answer 6, and not 5?
      
         (Answer: the NEXT command in line 60 is executed five times,
      and each time 1 is added to c.  The last time, c becomes 6; and
      then the NEXT command decides  not  to  loop back, but to carry
      on, c being past its limit.)
      
         What happens if you put STEP 2 in line 20?
      
      3. Change the third  program  so  that instead of automatically
      adding five numbers, it asks you  to input how many numbers you
      want adding. When you run  this   program,  what happens if you
      input 0, meaning that you want no numbers adding? Why might you
      expect this to cause problems for  the computer, even though it
      is clear what you mean? (The computer  has to make a search for
      the command NEXT c, which  is  not  usually necessary.) In fact
      this has all been taken care of.
      
      4. In line 10 of the fourth program above, change 10 to 100 and
      run the program. It will print  the  numbers  from 100 to 79 on
      the screen, and then say scroll? at the bottom. This is to give
      you a chance to see the  numbers  that are about to be scrolled
      off the top. If you press n, STOP or the BREAK key, the program
      will stop with the report D  BREAK-  CONT repeats. If you press
      any other key, then it will print  another 22 lines and ask you
      again.
      
      5. Delete line 30 from the fourth program. When you run the new
      curtailed program, it will print the first number and stop with
      the message 0 OK. If you type
      
              NEXT n
      
        the program will go  once  round  the  loop, printing out the
      next number.
      
      
      
      CHAPTER 5
      Subroutines
      
      
      Summary
      
           GO SUB, RETURN
      
        Sometimes different parts  of  the  program  will have rather
      similar jobs to do, and you  will find yourself typing the same
      lines in two or more times; how ever this is not necessary. You
      can type the lines in once, in  a  form known a s a subroutine,
      and then use,  or  call,  them  anywhere  else  in  the program
      without having to type them in again.
      
        To do this, you use the  statements GO SUB (GO to SUBroutine)
      and RETURN. This takes the form
      
              GO SUB n
      
      where n is the line number of the first line in the subroutine.
      It is just like  GO  TO  n  except  that the computer remembers
      where the GO SUB statement was  so  that it can come back again
      after doing the subroutine. It  does  this  by putting the line
      number and the statement number within the line (together these
      constitute the return address) on top  of   a pile of them (the 
      GO
      SUB stack);
      
              RETURN
      
      takes the top return address off the  GO SUB stack, and goes to
      the statement after it.
      
        As an example,  let's  look  at  the  number guessing program
      again. Retype it a s follows:
      
              10 REM "A rearranged guessing game"
              20 INPUT a: CLS
              30 INPUT "Guess the number ",b
              40 IF a=b THEN PRINT "Correct": STOP
              50 IF a<b THENGO SUB 100
              60 IF a>b THEN GO SUB 100
              70 GO TO 30
              100 PRINT "Try again"
              110 RETURN
      
        The GO TO statement  in  line  70  is  very important because
      otherwise the program will run on into the subroutine and cause
      an error (7 RETURN without GO SUB) when the RETURN statement is
      reached.
      
        Here is another rather silly  program illustrating the use of
      GO SUB.
      
              100 LET x=10
              110 GOSUB 500
              120 PRINT s
              130 LET x=x+4
              140 GO SUB 500
              150 PRINT s
              160 LET x=x+2
              170 GO SUB 500
              180 PRINT s
              190 STOP
              500 LET s=0
              510 FOR y=1 TO x
              520 LET s=s+y
              530 NEXT y
              540 RETURN
      
        When this program is run,  see  if  you  can work out what is
      happening. The subroutine starts at line 500.
      
        A subroutine can  happily  call  another,  or  even itself (a
      subroutine that calls itself is  recursive), so don't be afraid
      of having several layers.
      
      
      
      CHAPTER 6
      READ, DATA, RESTORE
      
      
      Summary
      
           READ, DATA, RESTORE
      
       In some previous programs  we  saw  that information, or data,
      can be entered  directly  into  the  computer  using  the INPUT
      statement. Sometimes this can be  very tedious, especially if a
      lot of the data is repeated every  time the program is run. You
      can save a lot of  time  by  using  the  READ, DATA and RESTORE 
      commands. For example:
      
              10 READ a,b,c
              20 PRINT a,b,c
              30 DATA 10,20,30
              40 STOP
      
       A READ statement consists of  READ  followed  by a list of the
      names of variables, separated by  commas.  It works rather like
      an INPUT statement, except that  instead of getting you to type
      in the values to give to  the  variables, the computer looks up
      the values in the DATA statement.
      
       Each DATA statement is  a  list  of  expressions  - numeric or
      string expressions  separated  by  commas.  You  can  put  them
      anywhere you like in  a  program,  because the computer ignores
      them except when it  is  doing  a  READ.  You  must imagine the
      expressions from all  the  DATA  statements  in  the program as
      being put together to form  one  long  list of expressions, the
      DATA list. The first time the computer goes to READ a value, it
      takes the first expression from  the  DATA list; the next time,
      it takes the  second;  and  thus  as  it  meets successive READ
      statements, it works its  way  through  the  DATA  list. (If it
      tries to go past the end  of  the  DATA  list, then it gives an
      error.)
      
        Note that it's a waste of  time  putting DATA statements in a
      direct  command,  because  READ   will   not  find  them.  DATA
      statements have to go in the  program.  Let's see how these fit
      together in the program you've  just  typed  in. Line 10  tells
      the computer to read three  pieces  of  data  and give them the
      variables a, b and c. Line  20 then says PRINT these variables.
      The DATA statement in line 30 gives  the  values of a, b and c.
      Line 40 stops the program.  To  see  the  order in which things
      work change line 20 to:
      
              20 PRINT b,c,a
      
       The information in DATA can be part of  a FOR . . . NEXT loop.
      Type in
      
              10 FOR n=1 TO 6
              20 READ D
              30 DATA 2,4,6,8,10,12
              40 PRINT D
              50 NEXT n
              60 STOP
      
       When this program is RUN you can see the READ statement moving
      through the DATA list. DATA  statements can also contain string
      variables. For example:
      
              10 READ d$
              20 PRINT "The date is",d$
              30 DATA "June 1st, 1982"
              40 STOP
      
        This is the simple way of  fetching expressions from the DATA
      list: start at  the beginning and  work through until you reach
      the end. However, you can make  the  computer jump about in the
      DATA list, using  the  RESTORE  statement.  This   has RESTORE,
      followed by a line number, and makes subsequent READ statements
       start getting their data from  the  first DATA statement at or
      after the given   line  number.  (You  can  miss  out  the line
      number, in which case it is  as  though  you had typed the line
      number of the first line in the program.)
      
        Try this program:
      
              10 READ a,b
              20 PRINT a,b
              30 RESTORE 10
              40 READ x,y,z
              50 PRINT x,y,z
              60 DATA 1,2,3
              70 STOP
      
        In this program the data  required  by  line  10 made a=1 and
      b=2. The  RESTORE  10  instruction  reset  the  variables,  and
      allowed x, y and z to be READ starting from the first number in
      the DATA statement. Rerun this program  without line 30 and see
      what happens.
      
      
      
      CHAPTER 7
      Expressions
      
      
      Summary
      
           Operations: +, -, *, /
           Expressions, scientific notation, variable names
      
        You have already  seen  some  of  the  ways  in  which the ZX
      Spectrum can calculate with  numbers.  It  can perform the four
      arithmetic operations +, -, * and  /   (remember that * is used
      for multiplication, and / is  used  for  division), and  it can
      find the value of a variable, given its name.
      
        The example:
      
              LET tax=sum*15/100
      
      gives just  a  hint  of  the  very  important  fact  that these
      calculations  can  be  combined.   Such   a  combination.  Like
      sum*15/100, is called an expression: so an expression is just a
      short-hand  way  of  telling   the   computer   to  do  several
      calculations,  one  after  the  other.   In  our  example,  the
      expression sum*15/100 means 'look up  the value of the variable
      called "sum", multiply it by 15, and divide by 100'.
      
        If you haven't  yet  done  so,  we  recommend  that  you look
      through the introductory booklet  to  see  how  the ZX Spectrum
      handles  numbers,  and  the   order   in   which  it  evaluates
      mathematical expressions.
      
       To recap:
      
        Multiplications and  divisions  are  done  first.  They  have
      higher priority than addition and subtraction. Relative to each
      other, multiplication  and  division  have  the  same priority,
      which means that the multiplications and divisions  are done in
      order from  left  to  right.  When  they  are  dealt  with, the
      additions and subtractions come  next  -  these  again have the
      same priority as each other, so  we  do them in order from left
      to right.
      
        Although all you really need to know is whether one operation
      has a higher or lower priority  than another, the computer does
      this by having a  number  between  1  and  16  to represent the
      priority of each operation: * and /  have priority 8, and + and
      - have priority 6.
      
        This order of calculation  is  absolutely  rigid, but you can
      circumvent it  by   using  brackets:  anything  in  brackets is
      evaluated first and then treated as a single number.
      
        Expressions are  useful  because,  whenever  the  computer is
      expecting a number  from  you,  you  can  give it an expression
      instead and it will work out the answer. The exceptions to this
      rule are so few that they  will  be stated explicitly in every
      case.
      
        You can add together as many strings (or string variables) as
      you like in a  single expression, and if you want, you can even
      use brackets.
      
        We really ought to tell you  what  you  can and cannot use as
      the names of variables. As we have  already said, the name of a
      string variable has to be  a  single  letter followed by $; and
      the name of the control variable of  a FOR-NEXT  loop must be a
      single letter; but the names  of ordinary numeric variables are
      much freer. They can use any  letters  or digits as long as the
      first one is easier. You can put  spaces  in as well to make it
      easier to read, but they won't count as part of the name. Also,
      it doesn't make any difference to  the name whether you type it
      in capitals or lower case letters.
      
        Here are some examples  of  the  names  of variables that are
      allowed:
      
       x
       t42
       this name is so long that I shall never be able to type it out
      again   without making a mistake
      
      now we are six       ( these last two names are  considered the
      nOWWeaReSiX               same, and refer to the same variable]
      
        These are not allowed to be the names of variables:
      
       2001       [it begins with a digit]
       3 bears    [begins with a digit]
       M*A*S*H    [* is not a letter or a digit]
       Fotherington-Thomas   [- is not a letter or a digit]
      
        Numerical expressions can  be  represented  by  a  number and
      exponent: again refer  to  the  introductory  booklet.  Try the
      following to prove the point:
      
                      PRINT 2.34e0
                      PRINT 2.34e1
                      PRINT 2.34e2
      
      and so on up to
      
              PRINT 2.34e15
      
        You will see that  after  a  while  the  computer also starts
      using  scientific  notation.  This  is  because  no  more  than
      fourteen characters can be used  to  write a number. Similarly,
      try
      
                      PRINT 2.34e-1
                      PRINT 2.34e-2
      
      and so on.
      
         PRINT gives only eight significant digits of a number. Try
      
                      PRINT 4294967295, 4294967295-429e7
      
        This  proves  that  the  computer  can  hold  the  digits  of
      4294967295, even though it is not  prepared to display them all
      at once.
      
        The ZX Spectrum uses  floating  point arithmetic, which means
      that it keeps separate the  digits  of  a number (its mantissa)
      and the position  of  the  point   (the  exponent.  This is not 
      always exact, even for whole numbers. Type
      
                      PRINT 1e10+1-1e10,1e10-1e10+1
      
        Numbers are held to about nine and a half digits accuracy, so
      1 e10 is too  big  to  be  held  exactly  right. The inaccuracy
      (actually about 2) is  more  than  1,  so  the numbers 1e10 and
      1e10+1 appear to the computer  to  be  equal.  For an even more
      peculiar example, type
      
              PRINT 5e9+1-5e9
      
        Here the inaccuracy in 5e9 is only  about  1, and the 1 to be
      added on in fact gets rounded  up  to  2. The numbers 5e9+1 and
      5e9+2 appear to the computer to  be equal.
      
        The  largest  integer  (whole   number)   that  can  be  held
      completely accurately is 1 less than 32 2's multiplied together
      (or 4,294,967,295).
      
        The string "" with no characters  at  all is called the empty
      or null string.  Remember  that  spaces  are significant and an
      empty string is not  the  same  as  one  containing nothing but
      spaces.
      
        Try:
      
              PRINT "Have you finished "Finnegans Wake" yet?"
      
        When you press ENTER, you will get the flashing question mark
      that shows there is a mistake  somewhere  in the line. When the
      computer finds the double quotes at the beginning of "Finnegans
      Wake", it imagines that these mark  the end of the string "have
      you finished", and it then can't work out what 'Finnegans Wake'
      means.
      
        There is a special device to get over this: whenever you want
      to write a string quote symbol  in  the middle of a string, you
      must write it twice, like this:
      
              PRINT "Have you finished ""Finnegans Wake"" yet?"
      
        As you can see  from  what  is  printed  on  the screen, each
      double quote is only really there  once;  you just have to type
      it twice to get it recognized.
      
      
      
      CHAPTER 8
      Strings
      
      Summary
      
        Slicing. using TO. Note  that  this  notation is not standard
      BASIC.
      
        Given  a  string,  a  substring   of   it  consists  of  some
      consecutive  characters  from  it,   taken  in  sequence.  Thus
      "string" is a substring of  "bigger  string", but "b sting" and
      "big reg" are not.
      
        There is a notation called slicing for describing substrings,
      and this can be  applied  to  arbitrary string expressions. The
      general form is string expression (start TO finish)
      
      so that, for instance,
      
                      "abcdef"(2 TO 5)="bcde"
      
        If you omit the start,  then  1  is  assumed; if you omit the
      finish then the length of the string is assumed. Thus
      
              "abcdef"( TO 5)="abcdef"(1 TO 5)="abcde"
              "abcdef"(2 TO )="abcdef"(2 TO 6)="bcdef"
              "abcdef"( TO )="abcdef"(1 TO 6)="abcdef"
      
      (You can also write this last  one as "abcdef"(), for what it's
      worth.)
      
        A slightly different form misses out  the TO and just has one
      number.
      
              "abcdef"(3)="abcdef"(3 TO 3)="c"
      
        Although  normally  both  start  and  finish  must  refer  to
      existing parts of  the   string,  this  rule  is  overridden by
      another one: if the start  is  more  than  the finish, then the
      result is the empty string. So
      
              "abcdef"(5 TO 7)
      
      gives error 3 subscript wrong  because the string only contains
      6 characters and 7 is too many, but
      
              "abcdef"(8 TO 7)=""
      
      and
      
              "abcdef"(1 TO 0)=""
      
        The start and finish must not be negative, or you get error B
      integer out  of  range.  This  next  program  is  a  simple one
      illustrating some of these rules.
      
                      10 LET a$="abcdef"
                      20 FOR n=1 TO 6
                      30 PRINT a$(n TO 6)
                      40 NEXT n
                      50 STOP
      
        Type NEW when this program  has  been  run and enter the next
      program:
      
                      10 LET a$="ABLE WAS 1"
                      20 FOR n=1 TO 10
                      30 PRINT a$(n TO 10),a$((10-n) TO 10)
                      40 NEXT n
                      50 STOP
      
        For string variables, we can not only extract substrings, but
      also assign to them. For instance, type
      
                      LET a$="l'm the ZX Spectrum"
      
      and then
      
                      LET a$(5 TO 8)="******"
      
      and
      
                      PRINT a$
      
        Notice  how  since  the  substring  a$(5  TO  8)  is  only  4
      characters long, only the first four stars have been used. This
      is a characteristic of  assigning  to substrings: the substring
      has to be exactly the same  length afterwards as it was before.
      To make sure this happens, the string that is being assigned to
      it is cut off on the  right  if  it  is too long, or filled out
      with spaces if it is  too  short  -  this is called Procrustean
      assignment after the  inn-keeper  Procrustes  who  used to make
      sure that his guests fitted  the  bed by either stretching them
      out on a rack or cutting their feet off.
      
        If you now try
      
                      LET a$()="Hello there"
      
      and
      
                      PRINT a$;"."
      
      you will see that the same  thing has happened again (this time
      with spaces put in) because a$() counts as a substring.
      
                      LET a$="Hello there"
      
      will do it properly.
      
        Complicated string expressions will  need brackets round them
      before they can be sliced. For example,
      
                      "abc"+"def"(1 TO 2)="abcde"
              ["abc"+"def")(1 TO 2)="ab"
      
      Exercise
      
      1. Try writing a program to print out the day of the week using
      string     slicing.     Hint:     let     the     string     be
      SunMonTuesWedThursFriSat.
      
      
      
      CHAPTER 9
      Functions
      
      Summary
      
           DEF
           LEN, STR$. VAL, SGN. ABS, INT, SQR
           FN
      
        Consider the sausage machine. You  put  a  lump of meat in at
      one end, turn a handle, and  out  comes  a sausage at the other
      end. A lump of pork gives a pork  sausage, a lump of fish gives
      a fish sausage, and a load of beef a beef sausage.
      
        Functions  are  practically  indistinguishable  from  sausage
      machines but there  is a  difference:  they work on numbers and
      strings instead of  meat.  You  supply  one  value  (called the
      argument), mince it up by  doing  some  calculations on it, and
      eventually get another value, the result.
      
              Meat in      Sausage Machine     Sausage out
      
              Argument in     Function     Result out
      
        Different  arguments  give  different  results,  and  if  the
      argument is completely inappropriate the function will stop and
      give an error report.
      
        Just as you can  have  different  machines  to mak