home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / T_LESSON.ZIP / TP-50B < prev    next >
Encoding:
Text File  |  1989-05-08  |  45.3 KB  |  1,281 lines

  1.  
  2. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  25
  3.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  4.  
  5.  
  6.  
  7.           
  8.  
  9.           TURBO-LESSON 3.  PROGRAM STRUCTURE
  10.  
  11.           OBJECTIVES - In this lesson you will learn about:
  12.  
  13.           1.  Program structure
  14.           2.  How to compile a program
  15.           3.  How to run a program
  16.           4.  Write and WriteLn statements
  17.           5.  Comments - at beginning of program
  18.           6.  Comments - at end of statement line
  19.           7.  Comments - deactivate a section of code temporarily
  20.  
  21.  
  22.                                 1.  Program structure.
  23.  
  24.           PASCAL programs are written in a certain way to make them easier to
  25.           write and easier to read.  There are two parts to any PASCAL
  26.           program: the DECLARATIONS part and the PROGRAM BODY.
  27.  
  28.           The PROGRAM BODY is where the processing statements occur, and any
  29.           data item used there must first be defined in the DECLARATIONS
  30.           section.  The form of a PASCAL program is
  31.  
  32.           PROGRAM Demo;     (Program name - not required)
  33.  
  34.           USES              (Units used by the program are listed here)
  35.  
  36.           LABEL             (Labels, if used, go here)
  37.  
  38.           CONST             (Constants are defined here)
  39.  
  40.           TYPE              (Define your own data types here)
  41.  
  42.           VAR               (ALL variables must be defined here)
  43.  
  44.           
  45.  
  46.           BEGIN             (Processing statements occur between
  47.  
  48.           END.               BEGIN and END.)
  49.  
  50.           
  51.  
  52.           The simplest PASCAL program is:
  53.  
  54.           BEGIN
  55.           END.
  56.  
  57.           In the next section you will learn to compile and run a program
  58.           using this "smallest program".
  59.  
  60. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  26
  61.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  62.  
  63.  
  64.  
  65.                              2.  How to Compile a Program
  66.  
  67.           Before you can execute a program, it has to be translated from
  68.           Pascal to a form the computer can understand.  This is the process
  69.           called "compiling" the program.
  70.  
  71.           ##### DO:
  72.  
  73.           Type in the program:
  74.  
  75.           BEGIN
  76.           END.
  77.  
  78.           ##### DO:
  79.  
  80.           Press Alt-C to pull down the Compile menu window.  The highlight
  81.           should be on the menu option: [Compile   Alt-F9].  If it isn't,
  82.           move the highlight to that line.  Press Enter to start the compile
  83.           process.
  84.  
  85.           The box you see on the screen now lets you know how your compile
  86.           worked.  If nothing was wrong, the word "Success" appears in the
  87.           lower left corner and "Press any key" blinks for your attention.
  88.  
  89.           Let's see what happens with an incorrect program.  A missing period
  90.           after END in your program would cause a problem for Pascal since it
  91.           looks for the period to mark the end of your program.
  92.  
  93.           ##### DO:
  94.  
  95.           Remove the period after END and compile the program again.
  96.  
  97.           What happened?
  98.  
  99.           The compile box appeared briefly, then an error message appeared at
  100.           the top of the screen:
  101.  
  102.                Error 10: Unexpected end of file
  103.  
  104.           The Pascal compiler was still looking for the period to mark the
  105.           end of your program when it bumped into the end of the editor work
  106.           file where your program was stored.
  107.  
  108.           ##### DO:
  109.  
  110.           Put the period back in your program after "End".  The error message
  111.           disappears when you start editing.  Compile the program again to be
  112.           sure you have a correct program again.
  113.  
  114.           ##### DO:
  115.  
  116.           If you haven't tried the "Hot key", Alt-F9, try it now.  This lets
  117.           you bypass the compiler pull-down menu.  Many of the menu options
  118.           have hot keys listed to the right of the option in the menu.
  119.  
  120. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  27
  121.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  122.  
  123.  
  124.  
  125.           Next, you need to learn how to "Run" or "Execute" a program.
  126.  
  127.  
  128.                                3.  How to Run a Program
  129.  
  130.           In the last section you "compiled" your BEGIN END program.  Now
  131.           let's run it to see what it does.
  132.  
  133.           ##### DO:
  134.  
  135.           Press Alt-R to pull down the Run menu.  The highlight should be on
  136.           [Run     Ctrl-F9].  Press <Enter> to run the program.
  137.  
  138.           What did the program do?  Maybe you better try that again.
  139.  
  140.           ##### DO:
  141.  
  142.           Run the program again several times.  What did it do?
  143.  
  144.           Mine did the same thing--nothing!  Since there are no statements
  145.           between BEGIN and END nothing is exactly what it should do.
  146.  
  147.           Definitely not the most useful program, but it illustrates a point:
  148.           the MAIN BODY is always required and as much of the DECLARATIONS
  149.           section as needed to define the data used.
  150.  
  151.           Since this simple program uses no data,  the DECLARATIONS section
  152.           can be omitted.
  153.  
  154.           In the next section you start meeting the statements that are used
  155.           to build a Pascal program.
  156.  
  157.  
  158.                            4.  Write and WriteLn statements.
  159.  
  160.           These processing statements are introduced first, because they can
  161.           be used with messages which require no previous data declaration.
  162.           This allows you to experiment with some programming in the MAIN
  163.           BODY without any concern for the DECLARATIONS part.
  164.  
  165.           ##### DO:
  166.  
  167.           Insert the WriteLn statement as shown between BEGIN and END in your
  168.           program and run the program.
  169.                BEGIN
  170.                  WriteLn('HELLO');
  171.                END.
  172.  
  173.           To see the output from your program, you need to make "Output" the
  174.           window at the bottom of the screen.  The following DO's will help
  175.           you find your way around Turbo's windows.  Notice the Function key
  176.           helps at the bottom of the screen:  F6-Switch.
  177.  
  178.           ##### DO:
  179.  
  180. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  28
  181.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  182.  
  183.  
  184.  
  185.           Press F6 several times.
  186.  
  187.           Notice the active window (double line and highlighted window name)
  188.           switching between top and bottom window.
  189.  
  190.           ##### DO:
  191.  
  192.           Press F6 once more if necessary to leave the bottom window active.
  193.  
  194.           The bottom window is either "Watch" or "Output".  For now, you will
  195.           want "Output" to occupy the bottom window.
  196.  
  197.           ##### DO:
  198.  
  199.           With the bottom window active, press Alt-F6 several times.  Leave
  200.           the "Output" window as the bottom window.
  201.  
  202.           ##### DO:
  203.  
  204.           Press F6 to switch to Edit as the active window.
  205.  
  206.           ##### DO:
  207.  
  208.           Run the program several times and watch the results in the bottom
  209.           window.
  210.  
  211.           ##### DO:
  212.  
  213.           Change "HELLO" to "Hi" in your program and run the program again.
  214.  
  215.           ##### DO:
  216.  
  217.           If you haven't tried the Run "Hot Key", try it now: Ctrl-F9.
  218.  
  219.           You may have already discovered that the Turbo Environment "knows"
  220.           when a new compile is needed.  If you change your program and try
  221.           to run it before compiling, it's OK.  A compile will be done before
  222.           running the program if it is needed.
  223.  
  224.           ##### DO:
  225.  
  226.           Change the "Hi" to "Lo" and run (Ctrl-F9) without first compiling.
  227.  
  228.           Did you see the compile box?
  229.  
  230.           ##### DO:
  231.  
  232.           Change the "Lo" back to "Hi" and run the program several times as
  233.           you watch for the compile box.
  234.  
  235.           When did the compile happen?  Only the first time you ran the
  236.           program after changing it.
  237.  
  238. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  29
  239.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  240.  
  241.  
  242.  
  243.           Now let's explore the difference between the "Write" and WriteLn"
  244.           statement.
  245.  
  246.           ##### DO:
  247.  
  248.           Remove the "Ln" from your WriteLn statement.  The program should
  249.           appear:
  250.  
  251.                BEGIN
  252.                  Write('Hi");
  253.                END.
  254.  
  255.           ##### DO:
  256.  
  257.           Run the program several times.
  258.  
  259.           What do you think the "Ln" part of the WriteLn statement does?  If
  260.           you're not sure, check out the next DO.
  261.  
  262.           ##### DO:
  263.  
  264.           Put the Ln back--change "Write" to "WriteLn".  The statement should
  265.           be:  WriteLn('Hi');  Run the program several times.
  266.  
  267.           You're right!  The Ln adds a linefeed--moves the cursor to the left
  268.           side of the screen at the next line.
  269.  
  270.           NOTE:  The way I write WriteLn is not required--you could use
  271.           writeln or WRITELN or even wRiTeLn if you cared to.  I prefer
  272.           WriteLn because it provides a visual reminder of the two actions of
  273.           this statement: Write (print whatever is in the parenthesis) and
  274.           Ln, provide a Linefeed.
  275.  
  276.           ##### DO:
  277.  
  278.           Load PROG3 and use the editor to examine the program.  A part of
  279.           PROG3 is printed below:
  280.  
  281.           BEGIN
  282.             WriteLn('* * * * * * * * * * * * * * * * *');  {Note that     }
  283.             WriteLn('*                               *');  {comments      }
  284.             WriteLn('*        TURBO-LESSON 3         *');  {placed at the }
  285.             WriteLn('*  Edited, Compiled, Executed   *');  {end of a      }
  286.             WriteLn('*            by                 *');  {statement line}
  287.             WriteLn('*     (put your name here)      *');  {must be       }
  288.             WriteLn('*                               *');  {terminated on }
  289.             WriteLn('* * * * * * * * * * * * * * * * *');  {the same line.}
  290.           
  291.  
  292.           In this section of PROG3, the WriteLn statement is used to display
  293.           messages.
  294.  
  295.           ##### DO:
  296.  
  297. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  30
  298.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  299.  
  300.  
  301.  
  302.           RUN the program to see the messages displayed on the screen.  (Use
  303.           Alt-F5 to see the full screen of output.  Press Alt-F5 again to get
  304.           back to the split screen so you can edit the program.)
  305.  
  306.           ##### DO:
  307.  
  308.           Use the editor to insert your name in the line indicated and run
  309.           the program again.
  310.  
  311.           To illustrate the difference between WriteLn and Write statements,
  312.           another portion of the program is included below:
  313.  
  314.             WriteLn;
  315.             Write  ('Check carefully when ');
  316.             Write  ('you run this program. ');
  317.             Write  ('How many lines ');
  318.             WriteLn('are printed');
  319.             WriteLn('by this last set of Write and WriteLn statements?');
  320.  
  321.           The "Ln" in WriteLn may be viewed as a linefeed or "return the
  322.           cursor to the beginning of the next line".
  323.  
  324.           Any message, or other data item within the parentheses following
  325.           WriteLn is printed first, the "Write" part, then the cursor is
  326.           moved to the next line, the "Ln" part.
  327.  
  328.           Notice the first WriteLn in the program segment above.  There is
  329.           nothing in parentheses to print, so this statement is a way to
  330.           print a blank line.
  331.  
  332.           The next three Write statements write on the same line.  Then the
  333.           following WriteLn statement writes it's message and causes the
  334.           cursor to return to the beginning of the next line.
  335.  
  336.           The last WriteLn displays it's message on the line where the cursor
  337.           is positioned, and returns the cursor to the next line.
  338.  
  339.           ##### DO:
  340.  
  341.           Add some WriteLn statements after the BEGIN to insert several blank
  342.           lines at the top of the screen to push the boxed message down the
  343.           screen.  (Don't forget to put a semicolon after each WriteLn you
  344.           add).
  345.  
  346.           Run the program.  Did it work right?
  347.  
  348.  
  349.                       5.  Comments - at the beginning of program.
  350.  
  351.           There are two ways to indicate comments in a PASCAL program.  The
  352.           curly brackets, { }, may be used to enclose comments.  The original
  353.           comment delimiters, from standard PASCAL are (*  *).
  354.  
  355. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  31
  356.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  357.  
  358.  
  359.  
  360.           At the beginning of a program, or anywhere that multiple line
  361.           comments are needed, it is convenient to mark the whole block of
  362.           comments with the delimiters at the left margin.   This makes it
  363.           easy to insert or delete comments within the block.
  364.  
  365.           ##### DO:
  366.  
  367.           Add some comments at the beginning of the program. Try both types
  368.           of comment delimiters, { }, and (* *).  Be sure your comments are
  369.           not "inside" any comments which are already in the program.
  370.  
  371.  
  372.                     6.  Comments - at the end of a statement line.
  373.  
  374.           Look at the comments following the WriteLn statements in PROG3.
  375.           Short comments may be inserted to clarify the action of one or more
  376.           statements.  NOTICE CAREFULLY that each of these comments has to be
  377.           completed on the same line, if the next line is another active
  378.           processing statement.
  379.  
  380.           ##### DO:
  381.  
  382.           Add a short comment, {Blank Line}, after one of the WriteLn
  383.           statements which prints a blank line.
  384.  
  385.  
  386.                      7.  Comments - deactivate a section of code.
  387.  
  388.           Having two sets of comment delimiters makes it possible to use one
  389.           set for most comments, while reserving the second set for
  390.           "deactivating" sections of statements when debugging a program.
  391.  
  392.           I prefer to use the { } for most comments, and (*  *) for
  393.           deactivating code.  You should keep the delimiters used for
  394.           commenting out code in the left margin where they are very visible.
  395.  
  396.           ##### DO:
  397.  
  398.           Deactivate the three WriteLn statements following the one which
  399.           prints
  400.  
  401.           TURBO-LESSON 3.
  402.  
  403.           Since { } are in use at the end of these lines you should use the
  404.           other set of comment symbols, (* and *) to deactivate the
  405.           statements.
  406.  
  407.           Run the program to see the results.
  408.  
  409.           DON'T BE AFRAID TO EXPERIMENT WITH THE SAMPLE PROGRAMS.  YOU CAN
  410.           ALWAYS GET ANOTHER COPY, FROM YOUR BACKUP DISK (you did make one,
  411.           didn't you), FROM THE .BAK FILE PROVIDED BY TURBO, OR FROM THE
  412.           ORIGINAL FILE IF YOU HAVEN'T SAVED AN EDITED COPY.
  413.  
  414. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  32
  415.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  416.  
  417.  
  418.  
  419.           
  420.  
  421.           TURBO-LESSON 4.  DECLARATIONS, INPUT
  422.  
  423.           OBJECTIVES - In Lesson 4 you will learn about:
  424.  
  425.           1.  The DECLARATIONS part of a program
  426.           2.  VAR declaration
  427.           3.  Input using the ReadLn statement
  428.           4.  DEBUG: Tracing Statements
  429.           5.  DEBUG: Watching Variables
  430.           6.  Integer variables
  431.  
  432.  
  433.                         1.  The DECLARATIONS Part of a Program.
  434.  
  435.           You learned in the previous lesson that there are two main parts to
  436.           any PASCAL program: DECLARATIONS, and MAIN BODY.   The various
  437.           entries in the DECLARATIONS section define the data items used in
  438.           the processing in the MAIN BODY.  Not all declaration entries will
  439.           occur in every program, but only the ones needed to support the
  440.           processing.
  441.  
  442.           Standard PASCAL requires that declarations occur in the order
  443.           specified and each type of declaration may occur only once.
  444.  
  445.           TURBO is a little more forgiving in some respects than standard
  446.           PASCAL (declarations don't have to occur in the specified order,
  447.           and the same type of declaration  may occur more than once).
  448.  
  449.           The various types of declaration entries will be introduced as
  450.           needed in the sample programs.  Only the VAR entry will be used in
  451.           this program.
  452.  
  453.  
  454.                                  2.  VAR Declaration.
  455.  
  456.           All variables, (spelled A-L-L, no exceptions), must be defined
  457.           before they are referenced by processing statements.  The VAR entry
  458.           is used to define variables.  The form of the entry is:
  459.  
  460.           variable-name   :  type;
  461.  
  462.           The variable-name may be one or several variable-names separated by
  463.           commas.  The type may be a predefined type, such as Integer, or a
  464.           type you have constructed using the predefined types. The colon
  465.           must occur between the variable-name(s) and the type.
  466.  
  467.           Extra spaces are acceptable to allow more readable format.  Below
  468.           are some VAR entries:
  469.  
  470.           VAR
  471.                 i,j,k    : Integer;
  472.                 Inkey    : Char;
  473.  
  474. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  33
  475.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  476.  
  477.  
  478.  
  479.                 Rate     : Real;
  480.                 Count    : Integer;
  481.  
  482.           (The example above includes types not yet discussed, to illustrate
  483.           the form of the VAR entry.)
  484.  
  485.           ##### DO:
  486.  
  487.           Look at PROG4.
  488.  
  489.           (You know from earlier lessons how to load the program and use the
  490.           editor to look at the program.)  A segment of PROG4 is:
  491.  
  492.           VAR
  493.             Number   : Integer;
  494.  
  495.           The VAR entry above defines a variable called "Number" to be of
  496.           type "Integer".  This means the computer must set up a memory
  497.           location large enough to store an integer, which can be accessed by
  498.           referring to the variable name "Number".
  499.  
  500.           Notice the variable, Number, is later referenced in the processing
  501.           statements.
  502.  
  503.           ##### DO:
  504.  
  505.           Add an integer variable called "Age" to Prog4.  This will be used
  506.           later in this lesson.  You can either add the new variable to the
  507.           declaration of Number
  508.  
  509.                 Number, Age   : Integer;
  510.  
  511.           or add another line with the Age declaration
  512.  
  513.                 Number   : Integer;
  514.                 Age      : Integer;
  515.  
  516.           Compile the program to be sure you haven't made an error.
  517.  
  518.  
  519.                          3.  Input Using the ReadLn Statement.
  520.  
  521.           ReadLn is the statement used to input variables.  The form of the
  522.           statement is:
  523.  
  524.           ReadLn(var_1, var_2, . . . ,var_n);
  525.  
  526.           When the statement is executed, the computer will wait for you to
  527.           type values for the variables, separated by one or more spaces,
  528.           followed by depressing the enter key.
  529.  
  530.           Part of PROG4 follows:
  531.  
  532.           
  533.  
  534. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  34
  535.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  536.  
  537.  
  538.  
  539.           BEGIN
  540.             Write('Enter a number (no decimals please): ');
  541.             ReadLn(Number);
  542.             WriteLn;                    { Display one blank line }
  543.             WriteLn('Number: ',Number); { Display the number entered }
  544.           END.
  545.  
  546.           The Write statement provides a prompt before the ReadLn accepts
  547.           Number.  Notice that ReadLn does not provide a "?" or any other
  548.           prompt.  The programmer must provide any prompting required.
  549.  
  550.           The use of the Write statement for prompting, instead of the
  551.           WriteLn, keeps the cursor on the same line so the input will occur
  552.           right after the prompt message.
  553.  
  554.           The ReadLn accepts the number you type and stores it in the memory
  555.           location which the computer has set aside for the Integer variable,
  556.           Number.
  557.  
  558.           ##### DO:
  559.  
  560.           Compile and run PROG4.  When prompted, type 12 and enter.  Run it
  561.           again and enter -34.
  562.  
  563.           ##### DO:
  564.  
  565.           Using the statements of PROG4 as an example, expand the program to
  566.           do the following:
  567.  
  568.           (1)  Write a prompt to 'Enter your age'.
  569.  
  570.           (2)  Read a value for age to be stored in the integer variable,
  571.           Age, which you added to the VAR declarations in the previous
  572.           section.
  573.  
  574.           (3)  Write a message which prints the age entered, in a manner
  575.           similar to the way Number was printed.
  576.  
  577.           Run the program.
  578.  
  579.  
  580.                             4.  DEBUG - Tracing Statements
  581.  
  582.           Turbo Pascal 5.0 has a new DEBUG feature to help you find problems
  583.           in programs.  It is also an excellent way to learn about Pascal
  584.           programming.  You can execute your program one statement at a time
  585.           with TRACE to see what each statement does.
  586.  
  587.           ##### DO:
  588.  
  589.           Press F7 to begin TRACEing your program.  Note the highlighted line
  590.           in your program.  The highlighted statement will be executed when
  591.           you press F7 again.
  592.  
  593. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  35
  594.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  595.  
  596.  
  597.  
  598.           As you press F7 to trace each statement, first look at the
  599.           statement and predict what you think will happen.
  600.  
  601.           ##### DO:
  602.  
  603.           Continue pressing F7, watching the output in the bottom window.
  604.  
  605.           ##### DO:
  606.  
  607.           Trace through the program again to be sure you understand what the
  608.           TRACE feature is doing for you.
  609.  
  610.  
  611.                             5.  DEBUG -  Watching Variables
  612.  
  613.           Another of the DEBUG features allows you to WATCH the values stored
  614.           in the computer's memory.  TRACE and WATCH used together form a
  615.           very effective debugging tool.
  616.  
  617.           ##### DO:
  618.  
  619.           Press F6 to make the bottom window active.  Press Alt-F6 to switch
  620.           from Output window to Watch window.  Press F6 again to make the top
  621.           (Edit) window active again.
  622.  
  623.           Now let's add a variable to the Watch window.
  624.  
  625.           ##### DO:
  626.  
  627.           Press Alt-B to pull down the Break/Watch window.  Notice the "Hot
  628.           Key" for Adding a Watch is Ctrl-F7.
  629.  
  630.           ##### DO:
  631.  
  632.           With the option "Add Watch    Ctrl-F7" highlighted, press Enter.
  633.           The Add Watch box appears on the screen.  This is where you enter
  634.           the name of the variable you want to watch.
  635.  
  636.           ##### DO:
  637.  
  638.           Type Number in the box and press Enter.
  639.  
  640.           The variable, Number, should now appear in the Watch window at the
  641.           bottom of the screen.
  642.  
  643.           ##### DO:
  644.  
  645.           Trace the program.  Watch the variable Number as you Trace the
  646.           statement, ReadLn(Number).
  647.  
  648.           There is another variable in the program.  You added the variable,
  649.           Age, in an earlier exercise.  Let's add Age to the watch window.
  650.           Notice there is a shortcut.
  651.  
  652. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  36
  653.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  654.  
  655.  
  656.  
  657.           ##### DO:
  658.  
  659.           Place the cursor on the word Age in your program.  Press Ctrl-F7.
  660.           The word with the cursor appears in the Watch box.  You don't have
  661.           to type it, just press enter to accept it.
  662.  
  663.           ##### DO:
  664.  
  665.           Trace the program again, watching both variables, Age and Number.
  666.  
  667.  
  668.                                 6.  Integer Variables.
  669.  
  670.           Integers are counting numbers, with no decimal points.  They may be
  671.           positive or negative.  In TURBO the range of Integers permitted is:
  672.  
  673.                  -32768 to +32767
  674.  
  675.           Integers are used for subscripts, indexes, counting, input and
  676.           output of such things as counts, limits, menu choices.  Decimal
  677.           numbers (Real type, discussed later), are needed for such things as
  678.           dollar amounts and calculations.   When decimal numbers are not
  679.           actually needed, Integers should be used since they are easier to
  680.           use and take less memory.
  681.  
  682.           ##### DO:
  683.  
  684.           Run or Trace PROG4 again, this time entering data which will cause
  685.           errors.
  686.  
  687.           When prompted to enter a number, enter the letter A instead.
  688.  
  689.           What happened?
  690.  
  691.           A message appears to indicate the nature of the problem.
  692.  
  693.           Note that the cursor is positioned at the statement which caused
  694.           the error.  (In this case, it is not the statement which caused the
  695.           problem, but the type of data entered.)
  696.  
  697.           ##### DO:
  698.  
  699.           Run or trace the program again, entering too large a number, 88888.
  700.           Note that no error message appears but the result is wrong.  What
  701.           number appears in the watch window?  Not 88888.  Run it again with
  702.           too small a number, -55555.
  703.  
  704.           When you use Integer variables, you have to be sure they will be
  705.           within the acceptable range or errors will occur.
  706.  
  707.           NOTE:  IN THE LESSONS THAT FOLLOW, BE SURE TO MAKE USE OF TRACE AND
  708.           WATCH.  WHEN INSTRUCTED TO "RUN" A PROGRAM, YOU COULD TRACE IT.
  709.           TRACING TAKES A LITTLE LONGER, BUT YOU WILL PROBABLY LEARN MORE
  710.           THAT WAY.
  711.  
  712. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  37
  713.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  714.  
  715.  
  716.  
  717.           
  718.  
  719.           TURBO-LESSON 5.  INTEGER EXPRESSIONS
  720.  
  721.           OBJECTIVES - In lesson 5 you will learn about:
  722.  
  723.           1.  Assignment statements
  724.           2.  DEBUG: Removing Watch Variables
  725.           3.  Integer expressions
  726.           4.  Problems with expressions
  727.  
  728.  
  729.                               1.  Assignment Statements.
  730.  
  731.           A large part of computer processing is accomplished by storing
  732.           numbers, strings, and other data objects in memory locations in the
  733.           computer.
  734.  
  735.           There are several ways to store a value in memory in Pascal:
  736.  
  737.                      (1) use an Input statement (ReadLn),
  738.                      (2) use an Assignment statement,
  739.                      (3) use a Procedure or Function (in later lessons).
  740.  
  741.           The Assignment statement has the following form:
  742.  
  743.           Variable := Expression;
  744.  
  745.           An example:   A := B + C;
  746.  
  747.           The way it works:  The computer evaluates the expression on the
  748.           right side of the replacement operator, :=, and stores the
  749.           resulting value in the memory location named by the variable on the
  750.           left.
  751.  
  752.           In the example above, the computer would obtain whatever value is
  753.           currently stored in the memory location called B, add that value to
  754.           the value it finds in the memory location called C, and store the
  755.           sum in the memory location, A.  If B holds the value 3, and C holds
  756.           the value 4, then 7 would be stored in the memory location called
  757.           A.
  758.  
  759.           ##### DO:
  760.  
  761.           Use the editor (Alt-F, New) to enter the following short program
  762.           (omit the comments, if you like):
  763.  
  764.           PROGRAM ABC;
  765.           
  766.           VAR  A, B, C  : Integer;
  767.           
  768.           BEGIN
  769.             A := 6;      {Assign the value 6 to the memory location, A}
  770.             B :=7;       {Assign 7 to B }
  771.  
  772. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  38
  773.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  774.  
  775.  
  776.  
  777.             C := A + B;  {Add the values in A and B, Store in C }
  778.             WriteLn ('A=',A, ' B=',B, ' C=',C);
  779.           END.
  780.  
  781.  
  782.                          2.  DEBUG -  Removing Watch Variables
  783.  
  784.           There are some "Leftover" variables in the watch window.  You will
  785.           need to get rid of them and add this program's variables.
  786.  
  787.           ##### DO:
  788.  
  789.           Press Alt-B to get the Break/Watch menu.  Press R to "Remove all
  790.           watches".
  791.  
  792.           ##### DO:
  793.  
  794.           Use Ctrl-F7 to add A, B, and C to the watch window.
  795.  
  796.           ##### DO:
  797.  
  798.           Press F7 once to start tracing.  What values do the variables A, B,
  799.           and C have?
  800.  
  801.           Pascal doesn't preset variables to zero as some languages do.
  802.  
  803.           ##### DO:
  804.  
  805.           Trace the program, watching the values of A, B, and C.
  806.  
  807.           ##### DO:
  808.  
  809.           Trace the program, watching the variables and expressions.
  810.  
  811.           ##### DO:
  812.  
  813.           Change the statement  A:=6  to  A:=3 and run the program again.
  814.  
  815.           Note that you have just written a complete PASCAL program - from
  816.           here on, you will just be adding new features in each lesson to
  817.           enable you to write more complicated programs.  (It didn't run?
  818.           Use the error message to help correct any errors.  If the error
  819.           message doesn't make sense, try looking for misplaced or omitted
  820.           semicolons.  Also note that the assignment statement uses the
  821.           compound symbol, ":=" and not "=".).
  822.  
  823.  
  824.                                3.  Integer Expressions.
  825.  
  826.           Integer expressions are composed of integer variables, constants,
  827.           and operators.  The operators used with integer expressions are:
  828.  
  829.             +, -, *, div, mod.
  830.  
  831. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  39
  832.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  833.  
  834.  
  835.  
  836.           The + and - have their usual meaning, addition and subtraction.
  837.           The * indicates multiplication.  Division of integer numbers is
  838.           done with div and mod.
  839.  
  840.           ##### DO:
  841.  
  842.           Examine PROG5.
  843.  
  844.           Two numbers, which you enter, are added, subtracted, and one is
  845.           cubed using the multiply operator, "*".  WriteLn is used to print
  846.           out the results.
  847.  
  848.           Notice that expressions may be calculated in an assignment
  849.           statement,
  850.  
  851.                            I_Cubed := I * I * I;
  852.  
  853.           or calculated in a WriteLn statement,
  854.  
  855.                            WriteLn('I - J ', I - J);
  856.  
  857.           ##### DO:
  858.  
  859.           Remove the variables from the watch window (Alt-B, R).
  860.  
  861.           ##### DO:
  862.  
  863.           Add the following to the Watch window.  (Add them in reverse order
  864.           if you prefer to see them as listed below.)
  865.  
  866.                I
  867.                J
  868.                I + J
  869.                I - J
  870.                I_Cubed
  871.  
  872.           
  873.  
  874.           ##### DO:
  875.  
  876.           Trace the program.
  877.  
  878.           Note that the ReadLn statement will attempt to read 2 values.  The
  879.           values should be typed with a space between them, not a comma.
  880.  
  881.           Enter the values 2 and 5.
  882.  
  883.           Check all the results.  Are they all correct?
  884.  
  885.           ##### DO:
  886.  
  887.           Run the program again, this time entering -3 and 5.
  888.  
  889. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  40
  890.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  891.  
  892.  
  893.  
  894.           Is everything correct again?  Notice the negative cube of -3 is as
  895.           expected, -27.  (Some incorrect negative cubes will appear a little
  896.           later in this lesson.)
  897.  
  898.           The division operators, div and mod are not used in PROG5.  To see
  899.           how they work,
  900.  
  901.           ##### DO:
  902.  
  903.           Just before the END of PROG5, edit in the following statements:
  904.  
  905.           WriteLn('I div J = ', I div J);
  906.           WriteLn('I mod J = ', I mod J);
  907.  
  908.           Run the program, entering the values 5 and 3.
  909.  
  910.           Did you get the results expected?   Is 5 divided by 3 really 1 and
  911.           not 1.666?
  912.  
  913.           ##### DO:
  914.  
  915.           Add these two statements at the end of the program:
  916.  
  917.           WriteLn(I,' divided by ', J, ' = ', I div J);
  918.           WriteLn(' with a remainder of ', I mod J);
  919.  
  920.           Run the program with the values 5 and 3.
  921.  
  922.           Often, when working with integers, it is useful to know one or both
  923.           of these components of the division.  If you really want the
  924.           decimal result of the division, the slash, /, could be used with
  925.           integers.
  926.  
  927.           ##### DO:
  928.  
  929.           Add I/J to the watch window.
  930.  
  931.           Run the program with the values 5 and 3.
  932.  
  933.           Note the result of division using the slash is the usual result.
  934.  
  935.           Observe that you can watch expressions that are not in the program.
  936.           I and J have values in the program so you can look at I/J even
  937.           though the expression I/J does not occur in the program.
  938.  
  939.           ##### DO:
  940.  
  941.           Before going on, try to improve the "readability" of the output by
  942.           adding some blank lines between the different parts of the output.
  943.           Use Alt-F5 to see the full Output screen.  For example, you might
  944.           add a blank line just before printing the cube of the number.
  945.           Remember, whenever you want to print a blank line, use WriteLn
  946.           without any parentheses.
  947.  
  948. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  41
  949.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  950.  
  951.  
  952.  
  953.                             4.  Problems with Expressions.
  954.  
  955.           You should be aware of the possibilities for various types of
  956.           errors involving expressions.  If you find this section a little
  957.           heavy going, come back to it later.  It is not essential for
  958.           understanding the lessons that follow.  However, it will help you
  959.           later when you are debugging your own programs if you understand
  960.           the ideas presented here.
  961.  
  962.           First, an easy to detect error.
  963.  
  964.           ##### DO:
  965.  
  966.           Declare K to be an integer variable.  (Add K to the list of
  967.           integers in the VAR section.)
  968.  
  969.           Add this statement at the end of the program:
  970.  
  971.           K := I / J;
  972.  
  973.           Run the program.  How did it go?
  974.  
  975.           This error, "Type mismatch", is easy to find, since the compiler
  976.           finds it.  The reason for the type mismatch is that the result of
  977.           the division using the "/" is a real number (covered in a later
  978.           lesson).  The variable, K, is an integer.  A real number can't be
  979.           stored in an integer memory location.  (Why not?  One reason: a
  980.           real number takes more memory space than an integer.)
  981.  
  982.           A second type of error, is illustrated in the following:
  983.  
  984.           ##### DO:
  985.  
  986.           First, remove the statement that caused the error (K := I/J), then
  987.           run the program using the values 31 and 5.
  988.  
  989.           Check the results of the cube of I.  Is it correct?
  990.  
  991.           O.K., but notice the cube of I, 29791, is approaching the upper
  992.           limit of integer variables, 32767.
  993.  
  994.           What will happen if you enter 32 and 5?  (The correct cube of 32 is
  995.           32768, just 1 too large to fit as an integer.)
  996.  
  997.           Try it!
  998.  
  999.           ##### DO:
  1000.  
  1001.           Run the program using the values 32 and 5.
  1002.  
  1003.           The computer, known for its reliability, informs you:
  1004.  
  1005.                 The cube of I = -32768
  1006.  
  1007. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  42
  1008.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1009.  
  1010.  
  1011.  
  1012.           Now, you know the sign is wrong - positive numbers do not produce
  1013.           negative cubes.  But look at the number, -32768.  Correct number
  1014.           with the wrong sign?  (The cube of 32 is 32768.)
  1015.  
  1016.           ##### DO:
  1017.  
  1018.           Run the program again with the values 33 and 5.  (The correct cube
  1019.           of 33 is 35937.)
  1020.  
  1021.                The cube of I = -29599
  1022.  
  1023.           Wrong number!  Wrong sign!  So why does the computer go merrily on
  1024.           its way - giving you these wrong answers?
  1025.  
  1026.           The computer is very good at detecting errors in the format of
  1027.           program statements, missing declarations, wrong punctuation.
  1028.  
  1029.           There are other types of errors that are more difficult to detect.
  1030.  
  1031.           It is up to you, the programmer, to find ways to keep these errors
  1032.           from going unnoticed.  For now, you need to be aware that these
  1033.           problems can occur.
  1034.  
  1035. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  43
  1036.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1037.  
  1038.  
  1039.  
  1040.           
  1041.  
  1042.           TURBO-LESSON 6.  CONDITIONAL PROCESSING
  1043.  
  1044.           OBJECTIVES - You will learn, in this lesson, about:
  1045.  
  1046.           1.  Selection structures used for conditional processing
  1047.           2.  DEBUG: Goto Cursor
  1048.           3.  IF statement  (two-way selection)
  1049.           4.  IF statement  (one-way selection)
  1050.  
  1051.  
  1052.                  1.  Selection Structures for Conditional Processing.
  1053.  
  1054.           
  1055.  
  1056.           There are three types of statement sequencing used in PASCAL:
  1057.  
  1058.              1.  SIMPLE SEQUENCE.  One statement follows another with no
  1059.           branching.
  1060.  
  1061.              2.  SELECTION STRUCTURES.  Based on a condition, supplied by the
  1062.           programmer, the next statement to execute is chosen from two
  1063.           alternatives (IF statement) or chosen from many alternatives (CASE
  1064.           statement).
  1065.  
  1066.              3.  REPETITION STRUCTURES.  A group of program statements may be
  1067.           repeated more than once, dependent on a condition supplied by the
  1068.           programmer.  The repetition statements are WHILE, REPEAT, and FOR,
  1069.           included in later lessons.
  1070.  
  1071.           The Selection statement, IF .. THEN .. ELSE, is illustrated in this
  1072.           lesson,  the CASE statement appears in a later lesson.
  1073.  
  1074.  
  1075.                                2.  DEBUG -  Goto Cursor
  1076.  
  1077.           In previous lessons you traced through each line of the programs.
  1078.           As programs grow larger you probably won't want to trace the entire
  1079.           program.  You will often want to run a program without tracing up
  1080.           to a certain point (where you suspect a problem exists), then have
  1081.           the program pause to let you start tracing at that point.  The Goto
  1082.           Cursor debug feature let's you do just that.
  1083.  
  1084.           First, let's see what the first five statements of PROG6A do.
  1085.  
  1086.           ##### DO:
  1087.  
  1088.           Set the bottom screen to Output and trace the first 5 statements.
  1089.           Then use Ctrl-F2 to reset the program so you can start the program
  1090.           from the beginning again.
  1091.  
  1092. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  44
  1093.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1094.  
  1095.  
  1096.  
  1097.           To see how the Goto Cursor works, we'll assume you want to run the
  1098.           first five statements without tracing, then stop at the IF
  1099.           statement to begin tracing.
  1100.  
  1101.           ##### DO:
  1102.  
  1103.           Move the cursor to the first line of the IF statement.  (Anywhere
  1104.           in the line is OK.)
  1105.  
  1106.           This marks the place you want the program to pause so you can take
  1107.           control.
  1108.  
  1109.           ##### DO:
  1110.  
  1111.           Press F4 to run the program up to the statement with the cursor.
  1112.           Input values at the prompts.
  1113.  
  1114.           Notice the program stops at the statement where you positioned the
  1115.           cursor.  This statement will be highlighted.
  1116.  
  1117.           Study the next section to find out what to expect of the IF
  1118.           statement.  The DO activities will let you see the IF in action.
  1119.  
  1120.  
  1121.                          3.  IF Statement (Two-way Selection).
  1122.  
  1123.           The two-way IF causes one of two alternative statements to be
  1124.           executed, based on whether the condition is TRUE or FALSE.  The
  1125.           form of the statement is:
  1126.  
  1127.           IF condition THEN Statement_1 ELSE Statement_2;
  1128.  
  1129.           If the condition is TRUE, the statement following the THEN is
  1130.           executed.  If the condition is FALSE, the statement following the
  1131.           ELSE is executed.
  1132.  
  1133.           The condition is an expression or comparison which the computer can
  1134.           evaluate as TRUE or FALSE.  Examples of a condition:
  1135.  
  1136.              7 < 10        TRUE
  1137.  
  1138.              I < 10        TRUE, if the memory location named I holds a
  1139.                                  value less than 10, otherwise FALSE.
  1140.  
  1141.              NOT(7 < 10)   FALSE (7 < 10 is TRUE, but NOT reverses the
  1142.                                             value to FALSE).
  1143.  
  1144.           ##### DO:
  1145.  
  1146.           Look at PROG6A.
  1147.  
  1148.           The IF statement in PROG6A is a two-way IF.  A congratulations
  1149.           message is printed if the condition is true,  condolences if false.
  1150.  
  1151. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  45
  1152.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1153.  
  1154.  
  1155.  
  1156.           The condition is (Have >= Want).  This condition is true if the
  1157.           number you enter for computers owned is greater than or equal to
  1158.           the number you enter for computers you would like to have.
  1159.  
  1160.           ##### DO:
  1161.  
  1162.           Run the program several times, experimenting with various input
  1163.           values.
  1164.  
  1165.           ##### DO:
  1166.  
  1167.           Trace the IF statement action several times.  Try to predict which
  1168.           statement will be highlighted next based on your input values.
  1169.  
  1170.           ##### DO:
  1171.  
  1172.           Switch the bottom window to Watch and add the two variables, Have,
  1173.           and Want to the watch window.
  1174.  
  1175.           ##### DO:
  1176.  
  1177.           Watch the values of Have and Want as you trace the program again.
  1178.  
  1179.           You could also watch the CONDITION part of the IF statement.  This
  1180.           should convince you that the only values a condition can have are
  1181.           TRUE and FALSE.
  1182.  
  1183.           ##### DO:
  1184.  
  1185.           Add (Have >= Want) to the watch window.  Trace the program several
  1186.           times with input values chosen to make the condition sometimes
  1187.           TRUE, sometimes FALSE.
  1188.  
  1189.  
  1190.                          4.  IF Statement (One-way Selection).
  1191.  
  1192.           The one-way IF is really a special case of the two-way IF, where
  1193.           the second alternative is to do nothing, just go on to the next
  1194.           statement.  The form of the IF statement is:
  1195.  
  1196.           IF condition THEN statement;
  1197.  
  1198.           ##### DO:
  1199.  
  1200.           Observe PROG6B.
  1201.  
  1202.           The second IF statement is a one-way selection.  If the condition
  1203.           is true, the WriteLn statement will be executed.  If the condition
  1204.           is false, the WriteLn will be ignored.
  1205.  
  1206.           ##### DO:
  1207.  
  1208.           Run (or Trace) the program using 0 for the number of computers
  1209.           owned.
  1210.  
  1211. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  46
  1212.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1213.  
  1214.  
  1215.  
  1216.           Run it again with 1 for the number of computers owned.
  1217.  
  1218.           The "No Computer!" message should print for 0 computers owned, but
  1219.           not print for 1 computer owned.
  1220.  
  1221.           Remember you can use Alt-F5 to see your output screen when the
  1222.           bottom window is the Watch window.
  1223.  
  1224.           NOTE: At this point you know what the TRACE feature and Goto Cursor
  1225.           feature can do for you.  In the sections and lessons to follow I
  1226.           will usually tell you to "Run" a program.  Remember you can always
  1227.           stop to Trace parts of the program.  Tracing a step at a time while
  1228.           observing variables or expressions in the Watch window will usually
  1229.           provide extra learning opportunities for you.
  1230.  
  1231.           ##### DO:
  1232.  
  1233.           Examine the third IF statement, added in PROG6C.
  1234.  
  1235.           This is a one-way IF with a slightly more complicated condition.
  1236.           The condition contains an integer expression, (Want - Have).  The
  1237.           computer first evaluates the integer expression to get a number to
  1238.           compare with the 2 on the right side of the ">".  For example, if
  1239.           you want 3 more computers than you have, the "Greedy" message
  1240.           should print.
  1241.  
  1242.           ##### DO:
  1243.  
  1244.           Add the expression, (Want - Have) to the Watch window.
  1245.  
  1246.           ##### DO:
  1247.  
  1248.           Run the program several times with different input to see the
  1249.           effect of this IF.
  1250.  
  1251.           Also note the misspelled "Aren''t" in the message.  The double
  1252.           apostrophe is used in the message to represent a single apostrophe.
  1253.           If a single apostrophe were used, it would appear to be the end of
  1254.           the message.
  1255.  
  1256.           ##### DO:
  1257.  
  1258.           Try your hand at writing an IF statement to do the following:
  1259.  
  1260.           If the number of computers owned is more than the number of
  1261.           computers wanted, print  a  message  'Send extra computers to (put
  1262.           your name here?) '.
  1263.  
  1264.           *********************** SHAREWARE REMINDER ***********************
  1265.  
  1266.           Congratulations!  You have finished the first 6 of 16 lessons.  You
  1267.           should be able to decide now whether these lessons are helping you
  1268.           get started with Pascal.  If you decide to continue, remember that
  1269.           payment for the lessons is expected.  This is SHAREWARE, not
  1270.  
  1271. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  47
  1272.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1273.  
  1274.  
  1275.  
  1276.           freeware.  If you missed the "Newspaper" concept of primary value
  1277.           in "first use", please go back and read the SHAREWARE notice at the
  1278.           beginning of the lessons.
  1279.  
  1280.           Thank you for your support for TURBO-LESSONS and SHAREWARE.
  1281.