home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_BAS / PROG914.ZIP / PROGTEXT.DOC < prev    next >
Text File  |  1994-01-08  |  40KB  |  948 lines

  1. PROGTEXT.DOC - by:  Ira F. Kavaler - March and April, 1987
  2.  
  3.  
  4. Beginner's BASIC - Ira F. Kavaler - 3/14/85 - 1/8/94
  5.  
  6.  
  7. The BASIC Language.
  8. -------------------
  9.  
  10.      BASIC is the name of a high-level computer language that is
  11. furnished with the vast majority of personal computers.  BASIC stands
  12. for Beginners All-Purpose Symbolic Instruction Code.  BASIC is not a
  13. new language;  it has been in use for 25 years.  It is not an original
  14. language;  it was derived from the most popular language of all time,
  15. FORTRAN (FOrmula TRANslator).  Both of these languages were created for
  16. scientists and engineers;  however, since both have become very easy to
  17. use in the past decade, almost every field where computers can be
  18. beneficial, BASIC (and FORTRAN) can be used as a general purpose
  19. programming language.
  20.  
  21.      BASIC consists of a set of "commands".  A command is a word or  an
  22. abbreviation that tells the computer to perform a task.  Commands
  23. usually require some additional information called "arguments" which
  24. specify the object of the task.  An instruction or "statement" contains
  25. the total information, command and argument(s).  The statements are
  26. arranged in a logical sequence to produce a "program".
  27.  
  28.  
  29. Line Numbers.
  30. -------------
  31.  
  32.      If you have ever followed a set of instructions you have noticed
  33. that each instruction is given a number.  This number serves two
  34. purposes:
  35.  
  36. 1.   To determine the proper order (sequence) of the instructions, and
  37. 2.   To provide a reference to any specific instruction should that
  38.      become necessary.
  39.  
  40.      The computer program also uses such a numbering system for the
  41. same reasons;  each statement number is called a "line number" since
  42. each statement in the program is usually written one to a line.  "Line
  43. numbers" must be positive integers;  that is, zero, negative numbers,
  44. and non-whole numbers areNot allowed.
  45.  
  46.      You might want to number your lines starting with "1" and
  47. continuing, incrementing the number by one, until all lines have been
  48. numbered.  Although there is no reason why this method would not work,
  49. a much better practice is to start with line number "10" and continue
  50. incrementing each subsequent line number by ten.  The advantage is that
  51. if you have forgotten a statement in theMiddle of the program, you will
  52. have unused numbers in-between the existing line numbers.
  53.  
  54.      Nearly 90% of all computer programs can be written using only six
  55. easily understood elementary commands: LET, PRINT, INPUT, GOTO,
  56. IF...THEN, and END.  There are dozens of other commands that make
  57. writing a program a very simple and concise task; however, you will
  58. discover that they areSubstitutes for commonly encountered sequences of
  59. the six elementary commands.Numeric constants.
  60.  
  61.      For many years the art of computer programming was kept a deep,
  62. dark secret by the mathematicians of the world.  By the way, computer
  63. programmers were called mathematicians in the "olden days" (1930's
  64. through 1960's).  In order to retain the secret, many mysterious terms
  65. were either adopted or coined for the computer and programming
  66. vocabulary;  "constant" is such a term.  A "constant" is a number which
  67. represents a quantity or an amount.  In general, a "constant" is
  68. anything that is fixed in value.  To be more precise, this type of
  69. constant is called a "numeric constant", as it only pertains to
  70. quantities or amounts.  Constants are like the numbers you would enter
  71. into a calculator:
  72.  
  73.      the digits zero "0" through nine "9",
  74.      the decimal point ".",
  75.      the plus "+" and minus "-" signs
  76.           (which denote positive and negative Numbers), and
  77.      the letter "E" (exponent) is also used as a symbol to indicate
  78.           scientific notation (10 to a power).
  79.  
  80. On a scientific calculator the key is labelled either "EE" (enter
  81. exponent) or "EXP".
  82.  
  83.      1234567890 = 1.23456789 X 10 (to the 9th power)
  84.                 = 1.23456789E+9
  85.  
  86.      0.0002468 = 2.468 X 10 (to the minus 4th power)
  87.                = 2.468E-4
  88.  
  89.      There is another category called "string constants" that involves
  90. names, addresses, dates, etc.  "Strings" will be covered at a later
  91. time.
  92.  
  93.  
  94. Flow Charting.
  95. --------------
  96.  
  97.      The easiest way to start writing a program is to diagram it;  that
  98. is, diagram the steps necessary to solve the problem.  Over the years
  99. some symbols have been adopted by different disciplines for this
  100. purpose.  These symbols are not standardized, and are left to you to
  101. use what you feel confortable using.  The following are my set:
  102.  
  103. BASIC Command           Flow chart symbol
  104. -------------           -----------------
  105. LET                     a rectangle
  106. INPUT                   a rectangle with the upper left corner cut-off
  107.                         (so it looks like a computer punch card)
  108. PRINT                   a rectangle with a one cycle sine wave at the
  109. bottom
  110.                         (so it looks like a sheet of paper torn-off)
  111. STOP or END             an octagon
  112.                         (so it looks like a stop sign)
  113. IF...THEN               a diamond
  114. the program first line  an upside-down triangle
  115.                         (so it looks like a yield sign)
  116. GOTO                    a circle (see text)
  117.  
  118.      The flow chart symbols are interconnected with arrows showing the
  119. direction of progress from one command to the next.  Exclding the
  120. IF...THEN diamond, each symbol can only have one aroow leaving it, but
  121. can have one or more arrows poinmting to it.
  122.  
  123.      The IF...THEN diamond must have only two arrows leaving it, one if
  124. the logical expression is evaluated "true" and the other if the logical
  125. expression is evaluated "false".  It can have one or more arrows
  126. poinmting to it.
  127.  
  128.      The STOP or END octagon cannot have any leaving arrows.  (There is
  129. one very advacned condition, where a CONTINUE command is executed, that
  130. allows for a leaving arrow, but it is seldom used.)
  131.  
  132.      The GOTO is normally not symbolized, the arrows are just drawn to
  133. indicate the path of the GOTO;  however, if the destination of the GOTO
  134. is far away from the source of the GOTO, or it is on another sheet of
  135. paper, etc., the source GOTO is terminated in a circle which has a
  136. specific label;  the destination of the GOTO originates at a circle
  137. having the same label, with an arrow to the destination command.
  138.  
  139.      The REM command is not indicated on the flow chart.
  140.  
  141.  
  142. Numeric Variables.
  143. ------------------
  144.  
  145.      As some of the better calculators have memories which can hold
  146. numbers, the computer also has memories.  Usually the calculator's
  147. memories will be numbered:  M1, M2, M3, etc.  One problem is that you
  148. must remember which memory holds what constant.
  149.  
  150.      The computer does not number the memories, but instead allows you
  151. to give them names.  If you wanted the computer to hold the balance of
  152. your checking account, you could call it BALANCE.  The computer will
  153. automatically assign one of its memories for this purpose and use it
  154. anytime you specify BALANCE.  Similarly, you could hold the deposit you
  155. made to your account as DEPOSIT, and there would be no problem.  In
  156. fact, the computer has the ability to hold hundreds of memories,
  157. limited only by the "size" of your computer system.  These so-called
  158. memories that you can name for your specific purpose are called
  159. "variables", and since they hold "numeric constants", they are called
  160. "numeric variables".  But I now must clarify one point;  most computers
  161. only use the first two characters of the variable name, although your
  162. name may be upto 16 characters long.  The IBM and compatibles use all
  163. 16 characters.  Consider a program where you are a farmer and have to
  164. keep track of how many apples and apricots you have harvested.  If you
  165. use the variable names APPLES and APRICOTS, most computers will not
  166. keep the two constants in separate variables;  both "APPLES and
  167. APRICOTS start with the same two letters "AP", and if the computer only
  168. uses the first two characters when it assigns memory to the variable
  169. names.  You will have to choose a different name for one of the two
  170. names, such as "ap" for apples and "ar" for apricots.
  171.  
  172.      You can name your variables using one letter of the alphabet ("A"
  173. through "Z"), two letters ("AA" through "ZZ"), or a letter followed by
  174. a number ("A0" through "Z9").  No other characters found on the
  175. computer's keyboard are allowed in variable names, no punctuations, no
  176. symbols, no arithmetic operations, and no "spaces".
  177.  
  178.      If you calculate how many different variable names you can create
  179. fromThe above rules, you will get 962 (=26+676+260).Arithmetic
  180. operations.  As your calculator has arithmetic operations: addition,
  181. subtraction, multiplication, and division, so does the computer.  The
  182. symbols for multiplication and division are different on the computer;
  183. multiplication isIndicated by "*" (called an "asterisk" or "star"), and
  184. division is indicated by "/" (called a "slash")".
  185.  
  186.  
  187. The LET Command.
  188. ----------------
  189.  
  190.      The most often used command in any computer program is the LET
  191. command.  It performs three functions:
  192.  
  193. 1.   It initializes a variable to a specific constant.
  194.  
  195. The form of the command is:
  196.  
  197.      10 LET variable = constant
  198.  
  199. Notice that 10 is an arbitrary line number, the command LET follows,
  200. the variable name must be next, then an equal sign, followed by a
  201. constant.
  202.  
  203. 2.  It sets the current value in one variable to another variable.
  204.  
  205. The form is only slightly different:
  206.  
  207.      20 LET variable2 = variable1
  208.  
  209. This instruction would recall the value of variable1 and store that
  210. value in variable2.
  211.  
  212. 3.   It evaluates an arithmetic expression and store the answer in a
  213. variable.  The form now becomes:
  214.  
  215.      30 Let variable = arithmetic expression
  216.  
  217. Where the arithmetic expression is any combination of variables,
  218. constants, and arithmetic operations that represents a formula.  In all
  219. three forms of the LET command, only one variable name is allowed to
  220. the left of the equal sign, only one equal sign follows, and the
  221. remainder of the instruction is to the right of the equal sign.
  222.  
  223.      When aa arithmetic expression, a formula, is used you must follow
  224. a set on algebraic conventions used in mathematics.  These convenmtions
  225. are very similar to those you use when you are using a hand calculator.
  226.  
  227. Symbol   Name                         Purpose
  228. ------   ------------                 -------
  229.   +      Plus Sign                    Used for adding or
  230.                                       indicating a positive value.
  231.   -      Minus Sign                   Used for subtracting or
  232.                                       indication a negative number.
  233.   *      Star or Asterisk             Used for multiplying
  234.                                       (x and . cannot be used).
  235.   /      Slash or Fraction Bar        Used for dividing
  236.                                       (there's no line with two dots).
  237.   ^      Carat                        Used for raising to a power
  238.                                       (there's no superscript).
  239.   (      Left or Open Parenthesis     Used to start a priority
  240. calculation.
  241.   )      Right or Close Parenthesis   Used to end a priority
  242. calculation.
  243.  
  244.  
  245.      The order of arithmetic & logical operations (hierarchy) is:
  246.  
  247. 1.   Inner-most parentheses & functions
  248. 2.   Outer-most parentheses & functions
  249. 3.   Exponentiation
  250. 4.   Multiplication & division (left to right)
  251. 5.   Addition & subtraction (left to right)
  252. 6.   Logical comparative operations (=,<,>,etc.)
  253. 7.   Logical conjunctive operations (AND, OR, NOT)
  254.  
  255.      It is also important to note that the command LET is optional and
  256. thus is usually omitted from the statement:
  257.  
  258.      10 Velocity=88
  259.      20 X2=x1
  260.      30 Area=base*height/2
  261.  
  262. LET is the only command that is optional, all other commands must
  263. immediately follow the line number, as will be seen in later commands.
  264.  
  265.  
  266. Syntax.
  267. -------
  268.  
  269.      In describing the elements that comprise the various forms of the
  270. LET Command, I tried to stress their proper order.  This order can be
  271. related to the correct order of the parts of a sentence, referred to as
  272. grammar.  The grammar of a program statement is called "syntax"
  273. (sin'tacks).  If you have taken the time to sit down at the computer
  274. keyboard and typed in a program, you have probably encountered a
  275. message from the computer.  All it means is that the computer did not
  276. understand the syntax in line 40 when it tried to follow the statement.
  277. A syntax error can be caused by aNumber of things:
  278.  
  279. 1.   The command was misspelled or an unknown command was used,
  280. 2.   The command was accompanied by incorrect or missing punctuation,
  281. 3.   A variable had the same name as a command, or
  282. 4.   An arithmetic operator was used incorrectly.
  283.  
  284.  
  285. Housekeeping (simple editing).
  286.  
  287.      You are bound to make many mistakes, most will be typos. When you
  288. do, the line numbers which you have given to each statement will assist
  289. you in correcting the mistakes.  If you have to delete a line, just
  290. type the line number of that statement and press the ENTER key.  (On
  291. many computers this key is labelled RETURN.)  What you have done is to
  292. replace the statement with nothing;  the computer realizes this and
  293. merely deletes the statement.  For the time being, if you make a typo,
  294. just retype the statement correctly using the same line number.  The
  295. computer will automatically remove the old statement and replace it
  296. with the new statement you have just typed in.  When we discussed "line
  297. numbers" we said that we could insert new statements in between
  298. existing line numbers.  If you are asking yourself how does the new
  299. statement get "in between", relax.  The computer automatically keeps
  300. track of which line numbers have been used and what line numbers
  301. isBeing type in.  As soon as you press the ENTER key, the computerLooks
  302. to see where the new line number should go:
  303.  
  304.      delete the line,
  305.      replace the line,
  306.      place the new line at the current end of the program,
  307.      place the new line at the start of the current program, or
  308.      place the new line in between any of the existing lines.
  309.  
  310.      This automatic resequencing of program instructions is sometimes
  311. called "housekeeping", as the computer is cleaning-up the order in
  312. which you have entered your program.
  313.  
  314.  
  315. The PRINT Command.
  316. ------------------
  317.  
  318.      Unlike a calculator, whenever the computer does a LET command (the
  319. same as pressing "=" on the calculator) the computer does not show the
  320. answer on the video screen (also called a monitor, CRT, or video
  321. display).  The computer merely stores the answer in the variable name
  322. you have chosen.  In order to tell the program to display an answer
  323. (the current constant stored in a variable) you have to issue a PRINT
  324. command.  The general form of the command is:
  325.  
  326.      PRINT variable
  327.  
  328.      The term "print" is from the "olden days" when the only device
  329. that the computer had connected to it to display answers was a
  330. lineprinter or teletype.  For these devices, "print" was a good choice
  331. of command as both devices actually printed the answers.
  332.  
  333.      40 PRINT BALANCE
  334.  
  335.      In the above example, the current value of BALANCE is printed at
  336. the left edge of the next (lower) empty line on the screen.  If the
  337. screen was totally empty before the PRINT, the top line of the screen
  338. would show the answer.  If the screen was completely filled, everything
  339. on the screen would automatically move up one line (scroll up) thus
  340. emptying a line at the bottom of the screen where the answer could now
  341. be printed.  The former top line of the display would be lost (erased).
  342.  
  343.      The computer keeps track of the condition of the screen by use of
  344. an "pointer".  Usually the position of the pointer is not indicated on
  345. the screen;  however, when you have to type something into the
  346. computer, the computer shows you where it will appear on the screen by
  347. displaying its pointer.  When the pointer is visible on the screen it
  348. is called a "cursor".  On many computers the cursor is nothing but a
  349. flashing block of light, on other computers the cursor may appear as a
  350. steady underline.  If you see the cursor you can be sure that the
  351. computer is waiting for you to supply some information.
  352.  
  353.      It is possible to print more than one thing on a screen line.
  354. More than one variable can be shown after a PRINT command:
  355.  
  356.      50 PRINT BALANCE,DEPOSIT
  357.      60 PRINT AP;AR
  358.  
  359.      In both lines 50 an 60 two variables are printed on each line.  In
  360. line 50 the two variables (BALANCE and DEPOSIT) are separated by a
  361. comma ",".  The comma tells the PRTINT command to display the answers
  362. in columns (tabulate).  The positions on the screen that determine
  363. where the columns start is fixed within the computer system, depending
  364. on the width (number of characters) of the screen.  The IBM and
  365. compatibles computers have five columns per line as its width is 80
  366. characters.  Each column width is usually set so that the longest
  367. (highest accuracy) number will not normally span adjacent columns.  In
  368. line 60 the two variables (AP and AR) are separated by a semicolon ";".
  369. The semicolon tells the computer not to tabulate the answers into
  370. columns, but rather print one after another with a minimum number of
  371. spaces.
  372.  
  373.      The choice of whether to print one or more variables per line,
  374. whether to use commas or semicolons if you decide to print more than
  375. one per line, is up to you.  You will soon develope a habit and then a
  376. sense for what to do.  Many times you will choose one way, just to
  377. change it to another, or a third way, after you have seen the answers
  378. on the screen.  When you get a few hours of programming under your
  379. belt, you will find that you spend as much, or even more time changing
  380. the structure of the PRINT commands just to make to screen look pretty!
  381.  
  382.  
  383. You can "quote" me.
  384.  
  385.      Besides displaying the answers to calculations, the PRINT command
  386. canBe used to display messages on the screen.  These messages are used
  387. to give the person running the program additional information.  The
  388. form of the command becomes:
  389.  
  390.      PRINT "any message or group of characters"
  391.  
  392. Note that the message is enclosed in quotes (").  In fact, the quoted
  393. message is nothing more than a substitute for a variable name that
  394. would normally be found in a PRINT statement;  thus any number of
  395. quoted messages and/or variable names can be intermixed within the
  396. PRINT statement.  Commas and/or semicolons can be used at the
  397. programmers discretion to separate the arguments.
  398.  
  399.      10 PRINT "The balance is $";B
  400.      20 PRINT "The speed is";S;"mph"
  401.      30 PRINT W;"gallons of water"
  402.      40 PRINT "Time","Distance"
  403.  
  404. In line 10 the addition of the dollar sign "$" emphasizes the amount of
  405. money.  In line 20 the addition of "mph" gives the units of the speed.
  406. Line 40 showns
  407. that messages alone may be displayed.
  408.  
  409.  
  410. The INPUT Command.
  411. ------------------
  412.  
  413.      In order to make a computer program useful it is necessary to get
  414. different information into the computer each time the program is used.
  415. Remember, the program is nothing more than the set of instructions.
  416. Although the LET commands give constants to the calculations, they are
  417. not sufficient.  What we want is a command that will allow the program
  418. to ask the person questions pertaining to the specific application of
  419. the problem.  The INPUT command allows this flexibility.  The general
  420. form is:
  421.  
  422.      INPUT variable name
  423.  
  424.      When the computer performs this command it stops, displays a
  425. question mark "?" and the flashing cursor, and awaits a response from
  426. the person.  The response is understood by the computer to be the
  427. "constant" for that variable name.  Once the constant is typed onto the
  428. keyboard, the person signals to the computer that the constant is
  429. complete by pressing the ENTER key.  The computer will extinguish the
  430. cursor and continue the program with the new information.
  431.  
  432.      More than one constant may be requested by the INPUT command by
  433. simply having additional variable names, each separated by a comma,
  434. (semicolons are not permitted).  When multiple constants are required,
  435. the person enters each constant separated by a comma, pressing the
  436. ENTER key after the last constant.  If the ENTER key is pressed before
  437. all constants are entered, the computer will respond with two question
  438. marks "??", indicating that more information is required.  Conversely,
  439. if too many constants are entered (more than there are variable names
  440. in the INPUT statement), the program will continue without error;
  441. however, a warning message of "extras ignored" will be displayed.
  442.  
  443.      60 PRINT "How many checks"
  444.      70 INPUT NC
  445.  
  446. In line 60 the PRINT command asks the person a question.  Line 70
  447. allows the program to accept the response and stores the value in
  448. variable name NC.  Except for the question mark and flashing cursor,
  449. the INPUT command Imparts no information about the nature of the
  450. question.  It is up to the programmer to PRINT a message called a
  451. "prompt".
  452.  
  453.      Since this sequence of PRINT and INPUT commands is common, there
  454. is a version of the INPUT command that contains a place for the
  455. message, this version of the command is called a "prompted input":
  456.  
  457.      INPUT "prompt message";variable name(s)
  458.  
  459. The "prompt" must always immediately follow the INPUT command and be
  460. enclosed in quotes, then followed always by a semicolon, and then
  461. followed by any number of variable names, each separated by a comma:
  462.  
  463.      60 INPUT "How many checks";NC
  464.  
  465. This is how the previous two commands can be combined into a single
  466. "prompted input" command. (Line 70 is not required.)  The only
  467. difference would be that the prompt and response are displayed on the
  468. same line on the screen when the program is run.
  469.  
  470.      One of the most common errors occurs when you enter constants. You
  471. accidentally press a character, such as a letter, instead of a digit,
  472. decimal point, plus or minus sign, or the letter "E".  When this
  473. happens the computer will respond with a warning message "redo from
  474. start".  The computer will force you to reenter the all constant(s)
  475. correctly, and then continue the program.
  476.  
  477.  
  478. It's time to write a program.
  479.  
  480.      We are going to write a program that will determine the number of
  481. single rolls of wallpaper needed for a room, given the size of the
  482. room.  First we will list the steps that must be taken to solve the
  483. problem:
  484.  
  485. 1.   Ask the person the size of the room in feet by:
  486.      A.   Asking for the room length in feet,
  487.      B.   Asking for the room width in feet, and
  488.      C.   Asking for the room height in feet.
  489. 2.   Calculate the amount of wallpaper required by:
  490.      A.   Calculating the perimeter of the room in feet,
  491.      B.   Calculating the wall area in square feet, and
  492.      C.   Calculating the number of single rolls by dividing the wall
  493.           area by 30 square feet.
  494. 3.   Tell the person the number of single rolls required.
  495.  
  496.      Having listed the steps in the process, we can translate each step
  497. into a program statement:
  498.  
  499.      10 PRINT "Enter the size of the room."
  500.      20 INPUT "Length (in feet)";L
  501.      30 INPUT "Width (in feet)";W
  502.      40 INPUT "Height (in feet)";H
  503.      50 P=W+W+L+L
  504.      60 A=P*H
  505.      70 N=A/30
  506.      80 PRINT "You will need";N
  507.      90 PRINT "single rolls of wallpaper."
  508.  
  509. Where: L=length, W=width, H=height, P=perimeter, A=area, N=number of
  510. rolls.
  511.  
  512.  
  513. Direct and Immediate Commands.
  514. ------------------------------
  515.  
  516.      There are a few commands that usually do not appear in a program
  517. but are used to make the computer operate its various feature:
  518.  
  519.  
  520. The RUN Command.
  521. ----------------
  522.  
  523.      Once you have typed in the program this command tells the computer
  524. to start the program running.  When the computer finishes the last
  525. (highest numbered) line, the computer will automatically stop.  (STOP
  526. and END are commands that can be put anywhere in a program to make the
  527. program stop not necessarily at the last line).
  528.  
  529.  
  530. The LIST Command.
  531. -----------------
  532.      This command allows you to display the program on the screen,
  533. starting with the first line through the last line.  The screen will
  534. normally scroll fast.  To stop the scrolling it is necessary to press
  535. the CTRL (Control) and BREAK key simultaneously, or the ESC (Escape)
  536. key.
  537.  
  538.  
  539. The NEW Command.
  540. ----------------
  541.  
  542.      This command will erase the program from the computer.  Do not use
  543. this command unless you are sure you do not want the program.  The NEW
  544. command should only be used when you are ready to type in a completely
  545. new program.
  546.  
  547.  
  548. Continuing with the Program.
  549. ----------------------------
  550.  
  551.      With the program typed in the computer, reread it to be sure there
  552. are no typing errors.  Retype correctly any line that has an error.
  553. Now type the command RUN and press the ENTER key.  The program will
  554. display the first of the series of three questions dealing with the
  555. size of the room.  Answer each question with a number (constant),
  556. pressing the ENTER key after each response.  Immediately the computer
  557. will display the answer to the problem, and will then say "Ready",
  558. meaning that the program has "stopped running".  You may now type RUN
  559. again to get the answer for another room size.
  560.  
  561.  
  562. Making the Program even Better.
  563. -------------------------------
  564.  
  565.      You should realize that the room has doors and windows, and that
  566. you will need less wallpaper since the wall area is reduced.  Let's
  567. modify the program to include this feature.  We will have to add steps
  568. to the solution of the problem:
  569.  
  570. Change old step 3. To new step 6. And replace it with:
  571. 3.   Ask the person for the number of doors in the room.
  572. 4.   Ask the person for the number of windows in the room.
  573. 5.   Calculate the roll reduction by:
  574.      A.   Adding together the number of doors and windows,
  575.      B.   Reducing the numbers of rolls by subtracting 1/2 Roll
  576.           for each (door/window) opening.
  577. 6.   Tell the person the number of single rolls required.
  578.  
  579.      We can now add the extra program lines to the previous version of
  580. the program by typing in only these new lines:
  581.  
  582.      72 INPUT "How many doors";ND
  583.      74 INPUT "How many windows";NW
  584.      76 OP=ND+NW
  585.      78 N=N-0.5*OP
  586.  
  587. Where: ND=number of doors, NW=number of windows, and OP=number of
  588.        openings (Doors+windows).
  589.  
  590.      Notice that the rest of the program did not have to beRetyped.
  591.  
  592.      If you now type LIST (and press RETURN) you will see that the
  593. program contains the newly added lines in their proper position.  Now
  594. type RUN (and press RETURN), answer all the questions, and observe that
  595. the answer has been changed (from what would have been previously
  596. displayed) to account for the savings in wallpaper due to the door(s)
  597. and window(s) decreasing the wall area.
  598.  
  599.  
  600. Entangled in Red Tape.
  601. ----------------------
  602.  
  603.      After you type in a program you will wonder what is going to
  604. happen to that program once you turn off power to the computer.  Your
  605. program is going to be lost forever.  Not a very pleasant thought
  606. considering you have probably just invested a few hours getting it to
  607. run just the way you wanted.  There are a series of commands that give
  608. you the ability to save your programs on a disk (also called a diskette
  609. or floppy disk) for future use, or to exchange them with a friend who
  610. has the same computer system.  IBM and compatibles can use the same
  611. diskettes.
  612.  
  613.  
  614. The SAVE Command.
  615. -----------------
  616.  
  617.      To save a program to a disk, insert the disk into the floppy drive
  618. by holding the disk by the label end, and inserting it into the drive
  619. with the label up.  Type in the command:
  620.  
  621.      SAVE "a:filename"              or       SAVE "b:filename"
  622.  
  623. Where "a:" is the designation for your first floppy drive, and
  624. "filename" is any name you want to give your program, enclosed in
  625. quotes.  This name allows the computer to find the program at a later
  626. time.  If you have a second floppy drive its designation is "b:".
  627.  
  628.      Since you are starting fresh out in BASIC programming I would
  629. suggested that you save the program in the ASCII format so that it can
  630. be reloaded into an editor.  You do this by appending a comma and the
  631. letter "a" (for ASCII) to the end of the SAVE command:
  632.  
  633.      SAVE "a:filename",a            or       SAVE "b:filename",a
  634.  
  635.  
  636. The LOAD Command.
  637. -----------------
  638.  
  639.      When you are ready to put the program back into the computer from
  640. the previously recorded on disk, just type in the command:
  641.  
  642.      LOAD "a:filename"              or       LOAD "b:filename"
  643.  
  644. Where "a:" is the designation for your first floppy drive, and
  645. "filename" is the same filename as you used when the program was
  646. originally saved.  If you have a second floppy drive its designation is
  647. "b:".
  648.  
  649.      If you don't remember or don't know the filename that your friend
  650. used, you can type in the FILES command, and the computer will display
  651. a listing of filenames previously saved on the diusk.  The format for
  652. this command is:
  653.  
  654.      FILES "a:"                     or       FILES "b:"
  655.  
  656.  
  657. The REM Command.
  658. ----------------
  659.  
  660.      REM is short for "remark".  The REM command is totally unnecessary
  661. in any computer program.  (Then why teach it?)  The REM command allows
  662. the programmer to put notes within the body of the program that he/she
  663. can refer to, but that the computer will ignore.  These notes can
  664. include the program title, the author, the date of creation (for
  665. copyright purposes), explanations of certain commands or formulae, etc.
  666. No matter what is typed after the REM command (on that line), it
  667. becomes part of the "statement" and the computer will bypass it as if
  668. it was not present.
  669.  
  670.  
  671. The END and STOP Commands.
  672. --------------------------
  673.  
  674.      Both of these commands serve the same purpose.  Both commands tell
  675. the computer to "stop" running.  Both commands cause the "Ok" message
  676. to be displayed along with the "flashing cursor".  (Then why are there
  677. two different commands?)  The STOP command additionally displays the
  678. line number that contained the STOP command, as: "Break in 150".  A
  679. complicated program may have more than one way to "stop".  The
  680. programmer may want to know which STOP command caused the program to
  681. terminate.
  682.  
  683.      In the programs shown we did not include an END nor STOP command
  684. because is was not necessary.  Most computers understand that when they
  685. have no more commands to process (at the bottom of the program) an END
  686. command is automatically assumed.  An END or STOP command may
  687. optionally be included at the bottom of a program.
  688.  
  689.  
  690. The GOTO Command.
  691. -----------------
  692.  
  693.      The GOTO command is used whenever you want a program to "go to" a
  694. different line other than the next line;  the statement contains the
  695. line number where the program is to go.  This command gives the program
  696. the ability to go back and repeat sections, or to go forward and jump
  697. over sections of a program.  (Some programmers call this action
  698. jumping, branching or transfering.)
  699.  
  700.  
  701. Another Program.
  702. ----------------
  703.  
  704.      A teacher wants to write a program to grade each student's test.
  705. The number of questions is to be entered only once, and then the number
  706. of correctAnswers for that student.  The program is to calculate the
  707. student's grade in percent.
  708.  
  709.      10 REM Grades - 4/14/84
  710.      30 INPUT "How many questions";Q
  711.      40 PRINT
  712.      50 INPUT "How many correct answers";A
  713.      60 G=100*A/Q
  714.      70 PRINT "Grade =";G;"%"
  715.      80 GOTO 40
  716.  
  717.      You will want to "stop" this program after entering the last
  718. student.  The program has no END nor STOP command.  The "assumed end"
  719. at the bottom of the program is negated by the GOTO in line 80.  On
  720. most computers, pressing the "Break" or "Escape" key(s) will termimate
  721. the program.  Let's enhance the program by having the computer
  722. calculate the class average in percent.  In order to simplify the
  723. process, we will show theAverage of the all students entered to that
  724. point.  The average is calculated by keeping a running total or sum of
  725. all grades and dividing it by the number of students.
  726.  
  727.      10 REM Grades - version 2 - 4/14/84
  728.      12 S=0
  729.      14 N=1
  730.      30 INPUT "How many questions";Q
  731.      40 PRINT
  732.      50 INPUT "How many correct answers";A
  733.      60 G=100*A/Q
  734.      70 PRINT "Grade =";G;"%"
  735.      72 S=S+G
  736.      74 AV=S/N
  737.      76 PRINT "Average =";AV;"%"
  738.      78 N=N+1
  739.      80 GOTO 40
  740.  
  741. Where lines 12, 14, 72, 74, 76, and 78 have been added.
  742.  
  743.  
  744. The IF...THEN Command.
  745. ----------------------
  746.  
  747.      The real power of the computer is released when we allow the
  748. computer to make decisions.  The computer makes decisions by making
  749. comparisons, such as:  does one variable equal another variable or
  750. constant, is a variable negative, is one variable greater than another
  751. variable or constant, etc.  The IF...THEN commands gives us this
  752. decision making capability. The general form of the command is:
  753.  
  754.      IF logical expression THEN line number or command
  755.  
  756. The statement is interpreted as:  "IF the logical expression is true,
  757. THEN either goto that line, or do that command indicated after the
  758. THEN.  Conversely, IF the logical expression is false, then proceed to
  759. the next line, bypassing the remainder of the statement after the
  760. "then".
  761.  
  762.  
  763. Logical Operators.
  764. ------------------
  765.  
  766.      The usual logical comparisons are allowed within the IF logical
  767. expression portion of the statement.  They include the following
  768. symbols slightly modified for computer usage, and are called "logical
  769. operators":
  770.  
  771.      =       Equal to
  772.      >       Greater than
  773.      <       Less than
  774.      >=      Greater than or equal to, => is also allowable
  775.      <=      Less than or equal to,    <=  "   "    "   "
  776.      <>      Not equal to,             ><  "   "    "    "
  777.  
  778.  
  779. Further Enhancing the Program.
  780. ------------------------------
  781.  
  782.      We can use the IF...THEN command in a number of ways to improve
  783. the "Grade" program:
  784.  
  785. 1.   Make sure that there is at least one test question,
  786. 2.   Make sure that there aren't more answers than questions, and
  787. 3.   Allow the program to stop if a negative number of answers are
  788. entered.
  789.  
  790. To incude these features in the program add the following lines:
  791.  
  792.      35 IF Q<1 THEN 100
  793.      100 PRINT "Improper number of questions"
  794.      110 PRINT "please reenter..."
  795.      120 GOTO 30
  796.      55 IF A>Q THEN 150
  797.      150 PRINT "Too many correct answers"
  798.      160 PRINT "please reenter..."
  799.      170 GOTO 50
  800.      57 IF A<0 THEN END
  801.  
  802.      Each group of new lines corresponds to each newly added feature.
  803. Lines 35 and 55 have line numbers after the "then" while line 57 has an
  804. END command after the "then".
  805.  
  806.  
  807. Comparison Conjuctions.
  808. -----------------------
  809.  
  810.      It is possible to make more than one comparison within the logical
  811. expression portion of the IF...THEN command.  The conjuctions are:
  812.  
  813.      AND  both comparisons must be true in order for the entire logical
  814.           expression to be true, and
  815.      OR   either one (or both) must be true in order for the entire
  816.           logical expression to be true.
  817.  
  818. The only stipulation is that each comparison must be able to stand
  819. alone as a separate logical expresion.
  820.  
  821.  
  822. Improper statements:
  823.  
  824.      1000 IF X ABD Y = 0 THEN PRINT "both are zero"
  825.      1100 IF P OR Q <= 10 THEN 600
  826.  
  827.  
  828. Correct statements:
  829.  
  830.      1000 IF X=0 AND Y=0 THEN PRINT "both are zero"
  831.      1100 IF P<=10 OR Q<=10 THEN 600
  832.  
  833. There is one additional logical operator which permits the logical
  834. expression to be negated;  it is called NOT.  Placing a NOT before a
  835. logical expression will change its value to "false" if it was "true",
  836. and vice-versa.
  837.  
  838.      440 IF NOT(Z=6) THEN 330
  839.      450 IF NOT(A+B>5) THEN 980
  840.  
  841. In line 440 the computer will go to line 330 only when Z does NOT equal
  842. 6.  In line 450 the computer will go to line 980 when the sum of A+B is
  843. NOT greater than 5;  that is, when the sum is less than or equal to 5.
  844.  
  845.      A logical expression can always be rewritten to eliminate the NOT;
  846. however, the "logic" may be difficult to envision.  Remmember that
  847. logical operators have the lowest level of hierarchy, thus the addition
  848. in line 450 is always performed before the logical comparison is made.
  849. If you are unsureOf the sequence of logical comparisons, insert
  850. parentheses to force the desired sequence.
  851.  
  852.  
  853. Exponentiation.
  854. ---------------
  855.  
  856.      Besides the four arithmetic operators ("+" addition, "-"
  857. subtraction, "*" multiplication, and "/" division) there is
  858. exponentiation (carat, "^"), which means "raise to the power".  If you
  859. wanted to write Q raised to the 5th power, it would be:
  860.  
  861.      75 P=Q^5      which is the same as     75 P=Q*Q*Q*Q*Q
  862.  
  863. In the hierarchy of operations, exponentiation is performed before the
  864. other four arithmetic operations.
  865.  
  866.  
  867. Parentheses.
  868. ------------
  869.      I previously said that there was an order to the way arithmetic
  870. operations would be performed in a LET command (calculation).  If you
  871. have to change the order of the arithmetic operation, such as
  872. performing an addition before a multiplication, then parentheses "( )"
  873. must be used in sets.  "(" is called the "left" or "open" parenthesis
  874. and ")" is called the "right" or "close" parenthesis;  there must be a
  875. ")" for every "(", and vice-versa.  Whenever the computer sees a set of
  876. parentheses it goes to the left-most, inner-most set and performs the
  877. enclosed calculations, then progressively works to the outer-most set,
  878. then it proceeds to the next (towards the right) inner-most set, and so
  879. on.
  880.  
  881.      Consider a fraction consisting of A+B in the numerator (top part)
  882. and C+D in the denominator (bottom part).  If the LET command was
  883. writen:
  884.  
  885.      35 X=A+B/C+D
  886.  
  887. The fractional part of the calculation would only be B/C, A and D would
  888. be simply added to the fraction and not be part of it.  If we enclose
  889. the numerator within parentheses, giving the revised LET command:
  890.  
  891.      35 X=(A+B)/C+D
  892.  
  893. Now the fractional part of the calculation would still not be correct
  894. since the variable D would be added to the fraction rather than part of
  895. the denominator.  The correct way to write the LET command is:
  896.  
  897.      35 X=(A+B)/(C+D)
  898.  
  899.      On the computer you will find other symbols called "brackets, [ ]"
  900. and "braces, { }";  they cannot be used in place of, or in addition to
  901. parentheses.
  902.  
  903.  
  904. Mathematical Functions.
  905. -----------------------
  906.  
  907.      Have you thought that you have bought one of the most expensive
  908. four function programmable calculators.  Don't get mad, you bought a
  909. full, and I mean full, function scientific calculator, and more!
  910.  
  911.      But you say, "I don't see any keys with scientific functions!"
  912. They're there, but you'll have to use simple abbreviations to activate
  913. them.  Suppose you wanted to take the square root of X and store it in
  914. Y, you'd write:
  915.  
  916.      225 Y=SQR(X)
  917.  
  918. Where SQR( ) is the function abbreviation (name) and the parentheses
  919. enclose the expression to be acted upon.  The following is a list of
  920. other common functions:
  921.  
  922. Function name           Form of function
  923. -------------           ----------------
  924. ABSolute value          Y=BAS(X)
  925. INTeger value           Y=INT(X)
  926. SiGNum (sign)           Y=SGN(X)
  927. natural LOG (ln)        Y=LOG(X)
  928. natural EXPonential     Y=EXP(X)
  929. SINe (trig.)            Y=SIN(X)     where X is in radians
  930. COSine (trig.)          Y=COS(X)       "   "  "  "    "
  931. TANgent (trig.)         Y=TAX(X)       "   "  "  "    "
  932. ArcTaNgent (trig.)      Y=ATN(X)       "   Y  "  "    "
  933.  
  934.      The parentheses are part of each function name and may not be
  935. omitted.  They are equivalent to normal parentheses (using the same
  936. symbols "( )") in that the order of arithmetic operations considers
  937. functions to be evaluated whenever their set of parentheses are
  938. encountered.
  939.  
  940.      Order of arithmetic & logical operations (hierarchy):
  941.  
  942. 1.   Inner-most parentheses & functions
  943. 2.   Outer-most parentheses & functions
  944. 3.   Exponentiation
  945. 4.   Multiplication & division (left to right)
  946. 5.   Addition & subtraction (left to right)
  947. 6.   Logical comparative operations (=,<,>,etc.)
  948. 7.   Logical conjunctive operations (AND, OR, NOT)