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

  1.  
  2. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  48
  3.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  4.  
  5.  
  6.  
  7.           
  8.  
  9.           TURBO-LESSON 7.  REPEAT STATEMENT
  10.  
  11.           OBJECTIVES - In lesson 7 you should learn about:
  12.  
  13.           
  14.  
  15.           1.  CHARacter variables
  16.           2.  BOOLEAN variables
  17.           3.  REPEAT statement
  18.  
  19.           
  20.  
  21.  
  22.                                1.  CHARacter Variables.
  23.  
  24.           The reserved word, CHAR, is used to declare a variable of character
  25.           type.  A variable of type CHAR can be used to refer to or store a
  26.           single character.
  27.  
  28.           VAR
  29.              Alpha : CHAR;
  30.  
  31.           
  32.  
  33.           Alpha is declared to be a variable which can be used to store any
  34.           of the characters in the character set.  This includes the upper
  35.           and lower case alphabet, the digits, 0 to 9, special characters
  36.           such as #, $, %, *, and the rest of the 256 characters in the PC's
  37.           character set.
  38.  
  39.           ##### DO:
  40.  
  41.           Examine PROG7.
  42.  
  43.           A variable named Response, of type CHAR, is used to store the
  44.           character entered in response to a multiple choice question.
  45.  
  46.           ##### DO:
  47.  
  48.           Set the lower window to Output and run the program several times,
  49.           entering wrong responses, and the correct response, D.  Also try
  50.           lower case d.
  51.  
  52.           ##### DO:
  53.  
  54.           Study the first IF statement in PROG7.
  55.  
  56.           IF (Response = 'D') OR (Response = 'd')
  57.               THEN . . .
  58.  
  59. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  49
  60.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  61.  
  62.  
  63.  
  64.           The character 'D' must be enclosed in single quotes in the program.
  65.           Note that you enter the character as input data without quotes when
  66.           running the program.
  67.  
  68.           Notice the compound condition using OR to combine two simple
  69.           conditions.  This condition will be true if either  or both of the
  70.           simple conditions are true.  The correct response, D, is checked in
  71.           both upper and lower case to make responding easier.
  72.  
  73.           ##### DO:
  74.  
  75.           Modify the IF statement to accept A or B as the right response.
  76.           Assume that the correct answer is A or B.  (Ignore lower case
  77.           responses to keep the statement shorter.)
  78.  
  79.  
  80.                                 2.  BOOLEAN Variables.
  81.  
  82.           BOOLEAN variables can have only two values, TRUE or FALSE.   The
  83.           value of a condition, TRUE or FALSE, may be stored in a BOOLEAN
  84.           variable for later use.
  85.  
  86.           VAR
  87.             Correct_Response : Boolean;
  88.  
  89.           In PROG7, the Boolean variable, Correct_Response, is used to store
  90.           the truth value (TRUE or FALSE) of the condition in the first IF
  91.           statement.  If the correct character, 'D' or 'd', is entered, TRUE
  92.           is stored in Correct_Response.  If anything else is entered, FALSE
  93.           is stored.
  94.  
  95.           Actually, TRUE and FALSE are stored as 1 and 0 to take up less
  96.           space, but you can always view a BOOLEAN variable as having a value
  97.           of TRUE or FALSE.
  98.  
  99.           ##### DO:
  100.  
  101.           Identify the condition in the second IF statement in PROG7.
  102.  
  103.           Since BOOLEAN variables can only have two values, TRUE or FALSE,
  104.           and conditions always evaluate to the same two values, a BOOLEAN
  105.           variable may be substituted for a condition.
  106.  
  107.           If would be permissible, but unnecessary to write the IF statement:
  108.  
  109.           IF Correct_Response = TRUE
  110.              THEN . . .
  111.  
  112.           ##### DO:
  113.  
  114.           Modify the IF statement as indicated above and run the program to
  115.           verify that the IF still works exactly as before.
  116.  
  117. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  50
  118.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  119.  
  120.  
  121.  
  122.           There is another way to assign the correct value to the variable,
  123.           Correct_Response.
  124.  
  125.           ##### DO:
  126.  
  127.           Replace the FIRST IF statement in PROG7 with the following
  128.           statement: Correct_Response := (Response = 'D') OR (Response =
  129.           'd');
  130.  
  131.           Run the program.
  132.  
  133.           How does the program change when you run it?  (If it doesn't do
  134.           exactly as before, maybe you typed it wrong, or replaced the second
  135.           IF instead of the first?)
  136.  
  137.           Since the condition on the right of the := must be evaluated by the
  138.           computer and assigned a value of TRUE or FALSE, this value can be
  139.           stored directly in a BOOLEAN variable without using the IF
  140.           statement.
  141.  
  142.           ##### DO:
  143.  
  144.           Add Response and Correct_Response to the Watch window and trace the
  145.           program.
  146.  
  147.           When does Correct_Response receive it's correct value?
  148.  
  149.  
  150.                                  3.  REPEAT Statement.
  151.  
  152.           In a previous lesson, you learned that there are three ways to
  153.           sequence the execution of statements in PASCAL: SIMPLE SEQUENCE,
  154.           SELECTION STRUCTURES, and REPETITION STRUCTURES.
  155.  
  156.           One of the statements used for REPETITION is REPEAT . . UNTIL.  The
  157.           form of the REPEAT statement is:
  158.  
  159.           REPEAT
  160.             Statement 1;
  161.             Statement 2;
  162.               .
  163.               .
  164.               .
  165.             Statement n
  166.           UNTIL condition;
  167.  
  168.           Statements 1, 2, . . . , n will be executed repeatedly until the
  169.           condition becomes true.  This implies that the condition is
  170.           checking something that can be changed by the statements 1 to n.
  171.           If this is not so, the statements will be repeated forever!
  172.  
  173.           In PROG7, you are prompted to respond to the multiple choice
  174.           question.  A REPEAT statement controls the block of statements
  175.           which prompt for a response, and then check the response.  The
  176.  
  177. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  51
  178.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  179.  
  180.  
  181.  
  182.           block of statements will be repeated until a correct response makes
  183.           the UNTIL condition true.
  184.  
  185.           ##### DO:
  186.  
  187.           Change the condition in the UNTIL in PROG7 to:
  188.                UNTIL 'A' = 'B';
  189.  
  190.           Run or Trace the program.
  191.  
  192.           Does the program correctly identify a correct response?  What
  193.           happens then.
  194.  
  195.           (Use ctrl-c or ctrl-Scroll-Lock to stop the program.)
  196.  
  197.           ##### DO:
  198.  
  199.           Change the condition again to:
  200.                UNTIL 'A' = 'A';
  201.  
  202.           How many times is the Repeat loop executed?
  203.  
  204.           Notice that the statements in a REPEAT structure are ALWAYS
  205.           executed at least once.  Even if the UNTIL condition is TRUE before
  206.           entering the REPEAT, the condition is not checked until the end of
  207.           the statements in the REPEAT block.
  208.  
  209. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  52
  210.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  211.  
  212.  
  213.  
  214.           
  215.  
  216.           TURBO-LESSON 8.  CASE STATEMENT
  217.  
  218.           OBJECTIVES - In lesson 8 you will learn about:
  219.  
  220.           1.  Block statements
  221.           2.  CASE statement
  222.  
  223.  
  224.                                  1.  Block Statement.
  225.  
  226.           As noted in an earlier lesson, the form of the IF statement is:
  227.                IF condition THEN statement_1 ELSE statement_2;
  228.  
  229.           IF the condition is true, statement_1 is executed, otherwise
  230.           statement_2 is executed.  However, a single statement may not
  231.           always get the job done.
  232.  
  233.           The Block statement (also called a Compound statement) allows you
  234.           to substitute a multiple statement block anywhere a simple
  235.           statement is acceptable.  The form of the Block statement is:
  236.                BEGIN Statement_1; Statement_2 END;
  237.  
  238.           You can include as many statements as you like between the BEGIN
  239.           and END.
  240.  
  241.           Notice that the main body of a Pascal program could be viewed as a
  242.           single statement!  That single statement is a block statement:
  243.  
  244.           BEGIN
  245.             Statement_1;
  246.             Statement_2;
  247.               .
  248.               .
  249.               .
  250.             Statement_n;
  251.           END.
  252.  
  253.           To illustrate the use of a Block statement in an IF statement,
  254.           consider the following problem:
  255.  
  256.           If the value of I is greater than the value of J, swap the two in
  257.           memory (a common problem when sorting data).
  258.  
  259.           If you happen to have a single statement to swap the values of two
  260.           memory locations, the IF statement might be:
  261.  
  262.           IF I > J
  263.             THEN SWAP(I,J);
  264.  
  265.           Later, you will learn how to make up your own "statements" (by
  266.           creating functions and procedures), but for now, the way to swap I
  267.           and J is:
  268.  
  269. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  53
  270.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  271.  
  272.  
  273.  
  274.           IF I > J
  275.             THEN
  276.               BEGIN
  277.                 Temp := I;
  278.                 I := J;
  279.                 J := Temp;
  280.               END;
  281.           
  282.  
  283.           Notice that the form of this IF statement is still correct:
  284.                IF condition THEN statement;
  285.  
  286.           The statement, in this case, is a Block statement, rather than a
  287.           simple statement.
  288.  
  289.           ##### DO:
  290.  
  291.           Take a look at the program called TEST1.
  292.  
  293.           This program is provided to make it quicker for you to test new
  294.           statements and concepts.  TEST1 has some integer variables and
  295.           character variables declared and the main BEGIN END.  All you need
  296.           to do to test a statement, or group of statements, is edit them
  297.           into the test program and run the program.
  298.  
  299.           ##### DO:
  300.  
  301.           Insert the following statements between the BEGIN and END of
  302.           program TEST1:
  303.  
  304.           Write('Enter two numbers ');
  305.           ReadLn(I, J);
  306.           WriteLn('I=', I, ' J=', J);
  307.  
  308.           ##### DO:
  309.  
  310.           Set the lower window to Output and run the program.
  311.  
  312.           ##### DO:
  313.  
  314.           Between the ReadLn and WriteLn statements you just entered, add the
  315.           IF statement to swap I and J if I is larger:
  316.  
  317.           IF I > J
  318.             THEN
  319.               BEGIN
  320.                 Temp := I;
  321.                 I    := J;
  322.                 J    := Temp;
  323.               END;
  324.  
  325.           Run the program several times using several pairs of input numbers
  326.           to test the program.  Does the "swap" work right?
  327.  
  328. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  54
  329.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  330.  
  331.  
  332.  
  333.           ##### DO:
  334.  
  335.           Switch the lower window to Watch.  Add the following to the watch
  336.           window: I, J, Temp and (I > J).  Trace the program.
  337.  
  338.           It may not be clear why three statements are needed to "Swap" two
  339.           numbers.  Why not just use
  340.  
  341.                I := J;
  342.                J := I;
  343.  
  344.           ##### DO:
  345.  
  346.           Remove the statement Temp:=I.  Change the statement J:=Temp to
  347.           J:=I;  Trace the program several times with different values until
  348.           you see why this doesn't work.
  349.  
  350.           Tracing while watching variables will often help you uncover weak
  351.           logic and false assumptions in your programs.
  352.  
  353.           ##### DO:
  354.  
  355.           Restore the IF statement to it's original form with the 3-statement
  356.           swap using Temp.  Run it to make sure it works right before going
  357.           on.
  358.  
  359.           What would happen if you forgot the BEGIN and END to enclose
  360.           several statements that belong to an IF statement?
  361.  
  362.           ##### DO:
  363.  
  364.           Remove the BEGIN and END in the IF statement and run the program
  365.           several times.
  366.  
  367.           Does the "swap" still work right?
  368.  
  369.           ##### DO:
  370.  
  371.           Run the program with 7 and 22 as input.  (These don't need
  372.           swapping.)
  373.  
  374.           Did the swap still work right?  What happened?
  375.  
  376.           Without the BEGIN and END to make the three statements appear as
  377.           one,  the statements "appear" as follows to Pascal:
  378.                
  379.  
  380.           IF I > J
  381.             THEN
  382.               Temp := I;
  383.           I := J;
  384.           J := Temp;
  385.           
  386.  
  387. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  55
  388.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  389.  
  390.  
  391.  
  392.           Only the statement, Temp := I, is controlled by the IF condition.
  393.           The two statements, I:=J; and J:=Temp; are executed for all values
  394.           of I and J.  They are no longer under the control of the IF.  When
  395.           your IF statements don't perform quite right, check to be sure
  396.           which statements are controlled by the IF.
  397.  
  398.           ##### DO:
  399.  
  400.           Trace the program.  Watch the variables carefully as each statement
  401.           is traced.
  402.  
  403.  
  404.                                   2.  CASE Statement.
  405.  
  406.           First, a bit of review from an earlier lesson:
  407.  
  408.           Program sequencing is done in Pascal with
  409.  
  410.             (1) Simple Sequence,   one statement follows another,
  411.           
  412.             (2) Selection Structures,
  413.                   IF  statement for one-way and two-way selection,
  414.                   CASE statement for many-way selection,
  415.           
  416.             (3) Repetition Structures,
  417.                   REPEAT statement,
  418.                   WHILE statement,
  419.                   FOR statement.
  420.  
  421.           The CASE statement is useful when there are more than two actions
  422.           or statement sequences needed.  The form of the CASE statement:
  423.  
  424.           CASE variable OF
  425.               value_1  : Statement_1;
  426.               value_2  : Statement_2;
  427.                 .
  428.                 .
  429.                 .
  430.               value_n  : Statement_n;
  431.             ELSE
  432.               Statement;
  433.           END; {CASE}
  434.  
  435.           Note the following:
  436.  
  437.           The variable must be a simple type such as Integer, CHAR, BOOLEAN.
  438.           (REAL is not allowed).
  439.  
  440.           The values used to determine which Statement to execute must be of
  441.           the same type as the variable.
  442.  
  443.           The values may be a constant, a list of constants, or a subrange
  444.           such as 1..10 (all integers from 1 to 10).
  445.  
  446. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  56
  447.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  448.  
  449.  
  450.  
  451.           How it works :
  452.  
  453.           (Refer to the "form of the CASE statement" above.)
  454.  
  455.           If the variable has a value of "value_1" then Statement_1 is
  456.           executed.  If "value_2" then Statement_2, . . .
  457.  
  458.           If the value of the variable matches none of the values, the
  459.           statement following the ELSE is executed.
  460.  
  461.           There must be an END to mark the end of the CASE statement. (It's a
  462.           good idea to add the comment {CASE} after the END).
  463.  
  464.           ##### DO:
  465.  
  466.           Use the editor to examine PROG8.
  467.  
  468.           This is the same problem as in the previous lesson, with a bit more
  469.           programming flexibility derived from the CASE statement and the
  470.           block statements.
  471.  
  472.           Notice that the list of variables in the CASE statement allow
  473.           appropriate responses for acceptable responses: A, a, B, b, C, c
  474.                                        correct responses: D, d
  475.                                   unacceptable responses: anything else.
  476.  
  477.           ##### DO:
  478.  
  479.           Run the program with the Output window to see how it works.
  480.  
  481.           ##### DO:
  482.  
  483.           Switch the lower window to Watch.  Remove any watch variables left
  484.           from earlier programs (Alt-B, R).  Add Response and
  485.           Correct_Response as watch variables.
  486.  
  487.           ##### DO:
  488.  
  489.           Trace the program.  Watch the action of the CASE statement.  (You
  490.           may need to move the Cursor down in the program to force the CASE
  491.           statement up in the Edit window.  This will allow you a better view
  492.           of the action of the CASE statement.)
  493.  
  494.           ##### DO:
  495.  
  496.           Modify the CASE statement in PROG8 to accept C as the best answer.
  497.  
  498.           Run the program.  Did it work?
  499.  
  500. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  57
  501.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  502.  
  503.  
  504.  
  505.           
  506.  
  507.           TURBO-LESSON 9.  FOR STATEMENT
  508.  
  509.           OBJECTIVES - In lesson 9, you will learn about:
  510.  
  511.           1.  Controlling field width in Write statements
  512.           2.  FOR statement
  513.  
  514.  
  515.                    1.  Controlling Field Width in Write Statements.
  516.  
  517.           Spacing of output in previous lessons has been done using spaces in
  518.           string constants, such as, 'I= '.  The space after the = will be
  519.           printed as part of the string.
  520.  
  521.           There is another way to add spaces.  When listing the items to
  522.           write in a Write or WriteLn statement, ":n" can be added to specify
  523.           how many characters the item is to occupy in the output.
  524.  
  525.           For example:
  526.  
  527.           WriteLn(I:4);
  528.  
  529.           The value of I would be printed in a space 4 characters wide.
  530.  
  531.           ##### DO:
  532.  
  533.           Withe the bottom window set to Output, run Program TEST1 several
  534.           times with the following statements in the main part of the
  535.           program:
  536.  
  537.           Write('Enter a Number: ');
  538.           ReadLn(I);
  539.           WriteLn('[', I:4, ']');
  540.  
  541.           Look closely at the output.  Are the numbers "left-justified" or
  542.           "right-justified"?
  543.  
  544.           If I has a value of 23, and 23 prints in columns 1 and 2 of the 4
  545.           column field, it is left-justified.  If the 23 appears in columns 3
  546.           and 4 of the 4 column field, it is right-justified.
  547.  
  548.           What happens if you enter a number that is too large to fit the
  549.           field specified?
  550.  
  551.           ##### DO:
  552.  
  553.           Run the program several times using the following values for I:
  554.  
  555.           123,   1234,   -1234,   12345
  556.  
  557. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  58
  558.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  559.  
  560.  
  561.  
  562.                                   2.  FOR Statement.
  563.  
  564.           In the previous lesson, you were reminded of the various ways to
  565.           control the sequencing of program actions.  One of the REPETITION
  566.           structures is the FOR statement.  The forms of the statement are:
  567.  
  568.           FOR variable := lowvalue TO highvalue DO
  569.                Statement;   {May be a block statement with BEGIN END}
  570.  
  571.           FOR variable := highvalue DOWNTO lowvalue DO
  572.                Statement;
  573.  
  574.           ##### DO:
  575.  
  576.           Run PROG9 several times to see how it works.
  577.  
  578.           Experiment with both positive and negative numbers.
  579.  
  580.           What happens if High is smaller than Low?
  581.  
  582.           ##### DO:
  583.  
  584.           Run PROG9 using 5 for Low (first number entered), 3 for High?
  585.  
  586.           What was the result?
  587.  
  588.           Also try 5 for both values.  How many times is the loop repeated if
  589.           Low = High?
  590.  
  591.           What results would you expect if you entered 3.4 for Low and 5.3
  592.           for High?
  593.  
  594.           ##### DO:
  595.  
  596.           Run the program with the values 3.4 and 5.3.
  597.  
  598.           Were you correct in predicting the outcome?
  599.  
  600.           Can constants be used instead the variables Low and High?
  601.  
  602.           ##### DO:
  603.  
  604.           Edit the first FOR statement in PROG9 as follows:
  605.  
  606.           FOR Index := 1 to 5 DO
  607.  
  608.           Run the Program.
  609.  
  610.           Is it possible to use expressions for Low and High?
  611.  
  612.           ##### DO:
  613.  
  614.           Change the FOR statement to:
  615.  
  616. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  59
  617.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  618.  
  619.  
  620.  
  621.           FOR Index := Low + 1 TO High DO
  622.  
  623.           Run the Program.
  624.  
  625.           ##### DO:
  626.  
  627.           Trace the program with High, Low, and Index in the watch window.
  628.  
  629. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  60
  630.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  631.  
  632.  
  633.  
  634.           
  635.  
  636.           TURBO-LESSON 10.  WHILE STATEMENT
  637.  
  638.           OBJECTIVES - In lesson 10 you will learn about:
  639.  
  640.           1. CONSTant declaration
  641.           2. WHILE statement
  642.           3. Delay timing loop
  643.  
  644.  
  645.                                1.  CONSTant Declaration.
  646.  
  647.           In the declaration section of the Pascal programs in previous
  648.           lessons, only VAR and PROGRAM have appeared.
  649.  
  650.           CONST is used to declare constants and assign values to them.
  651.  
  652.           The form is:
  653.  
  654.           CONST  Constant_Name_1   =  Value_1;
  655.                  Constant_Name_2   =  Value_2;
  656.                          .
  657.                          .
  658.                          .
  659.                  Constant_Name_n   =  Value_n;
  660.           
  661.  
  662.           Some examples:
  663.  
  664.           CONST  PI            =  3.14159;
  665.                  Fahr_Freeze   =  32;
  666.                  Cels_Freeze   =  0;
  667.                  Message_1     =  'This is a string constant';
  668.  
  669.           ##### DO:
  670.  
  671.           Run PROG10 a time or two with the Output window to see what it
  672.           does.
  673.  
  674.           ##### DO:
  675.  
  676.           Edit the constant declaration, Wobble_Size = 5;
  677.                                    to    Wobble_Size = 10;
  678.  
  679.           Run the program.  What effect did you see from the change?
  680.  
  681.           ##### DO:
  682.  
  683.           Change the value of the constant Left_Edge to 20 and run the
  684.           program.
  685.  
  686.           To understand how Left_Edge and Wobble_Size work, look at the
  687.           WriteLn statement:
  688.  
  689. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  61
  690.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  691.  
  692.  
  693.  
  694.           WriteLn(': ':Left_Edge,  First:Index,  Last:2,  Message);
  695.  
  696.             1.  ': ' is printed in the right 2 columns of a field whose
  697.                  width is determined by the value of the constant, Left_Edge.
  698.  
  699.             2. First initial is printed in the rightmost column of a field
  700.                 of width, Index.  Note that Index is changing in the FOR
  701.                 loop, so the width of this field changes.
  702.  
  703.             3. Last initial is printed next in the rightmost column of a
  704.                 2-character wide field.
  705.  
  706.             4. The string constant, Message, is printed immediately to the
  707.                 right of Last initial.
  708.  
  709.           ##### DO:
  710.  
  711.           ##### DO:
  712.  
  713.           In the WriteLn statement described above, change Last:2  to
  714.           Last:Index.  The statement should read:
  715.           
  716.             WriteLn(':  ':Left_Edge, First:Index, Last:Index, Message);
  717.  
  718.           ##### DO:
  719.  
  720.           Run the program to see the effect of the change you just made.
  721.  
  722.           ##### DO:
  723.  
  724.           Run the program several times with the values of the constants
  725.           reset to various values.  (LIVE DANGEROUSLY - try some wild values:
  726.           50 for Wobble_Size or 70 for Left_Edge.  Your CRT won't explode!
  727.           At least mine hasn't - yet!)
  728.  
  729.  
  730.                                  2.  WHILE Statement.
  731.  
  732.           The three Pascal REPETITION Structures are:
  733.             (1)  WHILE
  734.             (2)  REPEAT
  735.             (3)  FOR
  736.           
  737.  
  738.           The form of the WHILE statement is:
  739.              WHILE condition DO statement;
  740.  
  741.           Note that the statement is often a block statement and appears:
  742.  
  743.           WHILE condition DO
  744.             BEGIN
  745.               Statement_1;
  746.               Statement_2;
  747.                  .
  748.  
  749. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  62
  750.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  751.  
  752.  
  753.  
  754.                  .
  755.                  .
  756.               Statement_n;
  757.             END;
  758.  
  759.           The WHILE is executed "while" the condition is true.
  760.  
  761.           Contrast the REPEAT which is executed while the condition is false,
  762.           UNTIL the condition becomes true.
  763.  
  764.           ##### DO:
  765.  
  766.           Run PROG10 again, using 0 as the number "less than 8".  (You may
  767.           want to load a fresh copy of PROG10 if you have made a lot of
  768.           changes to the program.)
  769.  
  770.           How many times was the loop executed?
  771.  
  772.           You could substitute REPEAT for WHILE in PROG10.
  773.  
  774.           ##### DO:
  775.  
  776.           Replace:     WHILE Count > 0 DO
  777.              with:     REPEAT
  778.  
  779.               add:     UNTIL Count < 1;   after:   END; {WHILE}
  780.  
  781.           (Notice that you don't have to remove the BEGIN END when you change
  782.           the loop from WHILE to REPEAT.  They are not needed in the REPEAT
  783.           statement, but a block statement is always acceptable wherever a
  784.           simple statement can be used.)
  785.  
  786.           Run the program several times to verify that it still works the
  787.           same as with the WHILE loop.
  788.  
  789.           ##### DO:
  790.  
  791.           Run the program with 0 for the "number less than 8".
  792.  
  793.           How many times was the loop executed?
  794.  
  795.           Notice that the WHILE loop may be executed 0 times, but the REPEAT
  796.           loop is always executed at least once, since the condition is not
  797.           checked until the end of the loop.
  798.  
  799.           NOTE: YOU SHOULD BE ON THE LOOKOUT FOR SUCH CONTRASTS BETWEEN
  800.           "INTERCHANGEABLE" STATEMENTS.  CHOOSING THE BEST STATEMENT TO USE
  801.           IN EACH CASE IS BASED ON THIS KIND OF AWARENESS.
  802.  
  803.           ##### DO:
  804.  
  805.           Change the REPEAT loop back to the original WHILE loop.(An easy way
  806.           would be to load a fresh copy of the original PROG10.)
  807.  
  808. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  63
  809.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  810.  
  811.  
  812.  
  813.           Change the WHILE statement to accept only Last initials less than
  814.           'G'.
  815.  
  816.                WHILE Last < 'G' DO
  817.  
  818.           Run the program.   Try it once with H (uppercase) for a last
  819.           initial.  Then try it with A (uppercase).  How did it work?
  820.  
  821.           The WHILE loop didn't terminate at all when you used an A for the
  822.           last initial - you had to abort the program, right?  (If it is
  823.           still executing, use Ctrl-C to stop it.)
  824.  
  825.           One of the things needed in controlling loops: something in the
  826.           loop must be related to the condition.
  827.  
  828.           In the original WHILE loop, the Count was being decreased in the
  829.           loop.
  830.  
  831.           When the condition was changed to (Last < 'G'), the loop no longer
  832.           has any effect on the condition.
  833.  
  834.           Either the loop will not be executed at all, if Last is = or > 'G',
  835.           or it will execute indefinitely if Last is < 'G'.
  836.  
  837.           ##### DO:
  838.  
  839.           Change the WHILE statement to:
  840.  
  841.                WHILE (Last < 'G') AND (Count > 0) DO
  842.  
  843.           Run the program several times using last initials before and after
  844.           G in the alphabet.
  845.  
  846.           ##### DO:
  847.  
  848.           Run the program with last initial of 'a' (lower-case a).
  849.  
  850.           How many times did the loop execute?
  851.  
  852.           Why?
  853.  
  854.           The ASCII value of 'G' is 71.  The ASCII value of 'a' is 91.  So
  855.           'a' is > 'G'.
  856.  
  857.  
  858.                                 3.  Delay Timing Loop.
  859.  
  860.           You may have noticed the "wobble" proceeds faster in one direction
  861.           than the other.  This is due to a timing delay in the first WHILE
  862.           loop.  (You may need to use a number larger than 5000 in the delay
  863.           statement for faster computers.  Notice that the variable, Delay,
  864.           is declared to be a "longint" type.  This is an integer variable
  865.           which can store larger numbers.)
  866.  
  867. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  64
  868.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  869.  
  870.  
  871.  
  872.           FOR Delay := 1 to 5000 DO; {Do nothing, except loop}
  873.  
  874.           This looks a little strange?   Shouldn't there be a statement after
  875.           the DO?
  876.  
  877.           Pascal has a "null" statement for just such times as this.  When a
  878.           statement is required, but none is present, Pascal just substitutes
  879.           its "null" or "do-nothing" statement.
  880.  
  881.           ##### DO:
  882.  
  883.           Change the delay in the loop from 5000 to 20000 and run the
  884.           program.
  885.  
  886.           ##### DO:
  887.  
  888.           Add a timing loop to the other WHILE loop and run the program.
  889.  
  890.           How did it work?
  891.  
  892.           One last little twist on timing loops:
  893.  
  894.           ##### DO:
  895.  
  896.           Change the Wobble_Size to 10.
  897.  
  898.           Change both delay statements to:
  899.  
  900.                FOR Delay := 1 to 5000 * Index DO;
  901.  
  902.           Run the program.  Note that delays don't have to be constant.  This
  903.           delay varies as the FOR loop progresses!
  904.  
  905. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  65
  906.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  907.  
  908.  
  909.  
  910.           
  911.  
  912.           TURBO-LESSON 11.  INTRODUCTION TO FUNCTIONS
  913.  
  914.           OBJECTIVES - In this lesson, you will learn about:
  915.  
  916.           1.  Pascal Subprograms
  917.           2.  FUNCTION declaration
  918.           3.  How a FUNCTION Works
  919.           4.  Providing Input to the FUNCTION
  920.  
  921.  
  922.                                 1.  Pascal Subprograms.
  923.  
  924.           Why bother with subprograms?  Doesn't that just complicate
  925.           programs?  You will find that subprograms are used to simplify,
  926.           rather than complicate programs.
  927.  
  928.           The example programs in earlier lessons have been rather small.  As
  929.           programs grow in size and complexity, it is essential that you
  930.           utilize the appropriate tools to simplify the programming effort.
  931.  
  932.           The subprogram is one of the most powerful tools for simplifying a
  933.           complex program.  Subprograms may be viewed as "building blocks"
  934.           for constructing programs.
  935.  
  936.           As an illustration of the building block approach, consider a
  937.           program to enter and sort names for a phone directory, and then
  938.           print the sorted directory.
  939.  
  940.           The main program might be:
  941.  
  942.                BEGIN
  943.                  EnterNames;
  944.                  SortDirectory;
  945.                  PrintDirectory;
  946.                END.
  947.  
  948.           This shows the "big picture",  what the 3 main steps are.
  949.  
  950.           The subprograms, EnterNames, SortDirectory, and PrintDirectory may
  951.           each contain up to a page of program statements.  If all of this
  952.           processing were done in the main program, it might be difficult to
  953.           understand the program as a whole.
  954.  
  955.           As you learn to write Pascal programs, you will find Pascal
  956.           subprograms rather easy to write - they are almost identical to
  957.           programs!
  958.  
  959.           There are two types of subprograms in Pascal: FUNCTIONS and
  960.           PROCEDURES.   Both types may be either user-defined (written by
  961.           you) or pre-defined as a part of Pascal.
  962.  
  963.           In this lesson you will work with user-defined FUNCTIONS.
  964.  
  965. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  66
  966.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  967.  
  968.  
  969.  
  970.           Most of what you learn about FUNCTIONS also applies to PROCEDURES.
  971.  
  972.  
  973.                                2.  FUNCTION Declaration.
  974.  
  975.           Below is a simplified structure of a Program, Function, and
  976.           Procedure:
  977.  
  978.           PROGRAM         FUNCTION       PROCEDURE
  979.           LABEL           LABEL          LABEL
  980.           CONST           CONST          CONST
  981.           TYPE            TYPE           TYPE
  982.           VAR             VAR            VAR
  983.  
  984.           FUNCTION(s)     FUNCTION(s)    FUNCTION(s)  (0 or more FUNCTIONS)
  985.           PROCEDURE(s)    PROCEDURE(s)   PROCEDURE(s) (   and PROCEDURES  )
  986.           
  987.           BEGIN           BEGIN          BEGIN
  988.           (processing     (processing    (processing
  989.            statements)     statements)    statements)
  990.           END.            END;           END;
  991.           
  992.  
  993.           Notice that they look very similar.
  994.  
  995.           One difference, visible here, is the END of the processing block: A
  996.           period follows the END of a PROGRAM block, but a semi-colon follows
  997.           the END of a FUNCTION or PROCEDURE block.
  998.  
  999.  
  1000.                               3.  User-defined Function.
  1001.  
  1002.           We start with a very simple function to illustrate several key
  1003.           concepts.
  1004.  
  1005.           ##### DO:
  1006.  
  1007.           Look at PROG11.  The Function declaration is:
  1008.  
  1009.                FUNCTION GiveMe5:Integer;
  1010.  
  1011.           The significance of each part of this declaration is:
  1012.  
  1013.           FUNCTION       Type of subprogram, a FUNCTION, not a PROCEDURE.       
  1014.                          The two don't work in quite the same way.
  1015.  
  1016.           GiveMe5        The NAME of the FUNCTION.  This gives you a way to     
  1017.                          refer to this function.  The statement,                
  1018.                          WriteLn(GiveMe5), in the main program uses the name,
  1019.                          GiveMe5, to use this function.
  1020.  
  1021.           INTEGER        The name, GiveMe5, is associated with a memory         
  1022.                          location which can store an Integer.  Somewhere        
  1023.  
  1024. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  67
  1025.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1026.  
  1027.  
  1028.  
  1029.                          within the BEGIN END block of the FUNCTION, GiveMe5    
  1030.                          must be given an Integer value to store.
  1031.  
  1032.           First, observe how the program sequences the execution of
  1033.           statements.
  1034.  
  1035.           ##### DO:
  1036.  
  1037.           Trace PROG11, watching the highlight progress through the main
  1038.           program, to the FUNCTION, and back to the main program.
  1039.  
  1040.           The function name, GiveMe5 must be given a value in the function.
  1041.           What do you think would happen if no value were given to the name,
  1042.           GiveMe5?
  1043.  
  1044.           ##### DO:
  1045.  
  1046.           Remove the statement, GiveMe5:=5; in the FUNCTION.  The function
  1047.           BEGIN END block now has no active statements.
  1048.  
  1049.           ##### DO:
  1050.  
  1051.           Run the program.  What was printed?  Mine printed 22005.  Yours
  1052.           might print 0 or something else.  This is just whatever happens to
  1053.           be in the memory location allocated for GiveMe5.
  1054.  
  1055.  
  1056.                           4.  Providing Input to the FUNCTION
  1057.  
  1058.           You would no doubt agree that the function, GiveMe5, in the
  1059.           previous section is pretty useless.  You could use the statement
  1060.           WriteLn(5); in the main program to do the same thing as
  1061.           WriteLn(GiveMe5);
  1062.  
  1063.           Most useful functions operate on data which is provided as input to
  1064.           the function.
  1065.  
  1066.           PROG11A uses a function with input.  A number is input to the
  1067.           Function named Cube.  The function calculates the cube of the
  1068.           number and stores the result in a memory location associated with
  1069.           the name, Cube.
  1070.  
  1071.           ##### DO:
  1072.  
  1073.           Examine FUNCTION Cube in PROG11A using the editor.
  1074.  
  1075.           FUNCTION Cube(Number:Integer) : Integer;
  1076.  
  1077.           The FUNCTION declaration above means:
  1078.           
  1079.  
  1080.           FUNCTION         Type of subprogram, a FUNCTION.
  1081.           
  1082.           Cube             The name of the FUNCTION.
  1083.  
  1084. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  68
  1085.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1086.  
  1087.  
  1088.  
  1089.           
  1090.           (Number:Integer) An integer "parameter" called Number is used
  1091.                            within the FUNCTION. This is the number to be        
  1092.                            cubed.
  1093.           
  1094.           : Integer;       Type of value to be assigned to Cube.
  1095.  
  1096.           ##### DO:
  1097.  
  1098.           Study the connection between the following two statements:
  1099.  
  1100.           In FUNCTION Cube:
  1101.  
  1102.           FUNCTION Cube(Number:Integer) : Integer;
  1103.  
  1104.           In PROG11A:
  1105.  
  1106.           WriteLn('The cube is: ', Cube(No) );
  1107.  
  1108.           Cube(No) in the statement above invokes (calls) the FUNCTION Cube
  1109.           to operate on the number called No.
  1110.  
  1111.           PROG11A,  No ----->  Number, in FUNCTION Cube.
  1112.  
  1113.           The value of the variable called No in PROG11A is provided to the
  1114.           FUNCTION to use as a value of its variable called Number.
  1115.  
  1116.           ##### DO:
  1117.  
  1118.           Put the variables Number and No in the watch window.  Trace the
  1119.           Write and ReadLn in the main program.  Enter 3.  Stop when the
  1120.           WriteLn statement is highlighted.
  1121.  
  1122.           Notice that No now has the value 3.  Number (in the Function) is an
  1123.           "Unknown identifier".
  1124.  
  1125.           ##### DO:
  1126.  
  1127.           Trace the WriteLn statement--press F7 once.
  1128.  
  1129.           Observe that 2 things happened:
  1130.  
  1131.                1.  The program execution transferred from the WriteLn
  1132.                statement in the main program to the beginning of the Function
  1133.                Cube.
  1134.  
  1135.                2.  Number now has the value 3, which it received from No in
  1136.                the main program.
  1137.  
  1138.           ##### DO:
  1139.  
  1140.           Trace the rest of the program.  Watch the program execution return
  1141.           to the main program and end there.
  1142.  
  1143. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  69
  1144.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1145.  
  1146.  
  1147.  
  1148.           ##### DO:
  1149.  
  1150.           Set the lower window to Output and run PROG11A several times using
  1151.           the following values for input:
  1152.  
  1153.           3,  -3,  0
  1154.  
  1155.           31   (cube should be 29791)
  1156.  
  1157.           32   (cube should be 32768)  Is it?  Remember this problem from an
  1158.           earlier lesson?  You will explore some techniques for detecting
  1159.           this problem in the next lesson.
  1160.  
  1161.           Now, let's see what happens if you change some of the things in the
  1162.           FUNCTION and the reference to it.
  1163.  
  1164.           What would happen if you used a constant instead of No in the
  1165.           reference, Cube(No)?
  1166.  
  1167.           ##### DO:
  1168.  
  1169.           Change the WriteLn statement to:
  1170.                WriteLn('The cube is: ', Cube(3) );
  1171.  
  1172.           Run the program several times with different input values.
  1173.  
  1174.           Do you get the same result no matter what you input?
  1175.  
  1176.           Notice that the variable, No, which you input, is no longer used in
  1177.           the FUNCTION reference, Cube(3).
  1178.  
  1179.           Can you use expressions in the reference to the function?
  1180.  
  1181.           ##### DO:
  1182.  
  1183.           Change the FUNCTION reference to Cube(No - 2).
  1184.  
  1185.           Run the program several times with input values:
  1186.  
  1187.           5     (No - 2) is 3, so cube should be 27.
  1188.  
  1189.           1     (No - 2) is -1, so cube should be -1.
  1190.  
  1191.           -1    (No - 2) is -3, so cube should be -27.
  1192.  
  1193.           Do integer expressions work o.k?
  1194.  
  1195.           What if you tried to use a non-integer value in the Cube(No)
  1196.           reference?
  1197.  
  1198.           ##### DO:
  1199.  
  1200.           Modify the WriteLn statement to:
  1201.                WriteLn('The cube is: ', Cube('This is a string') )
  1202.  
  1203. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  70
  1204.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1205.  
  1206.  
  1207.  
  1208.           Run the program.
  1209.  
  1210.           Did the compiler complain?
  1211.  
  1212.           Error 26: Type mismatch.
  1213.  
  1214.           Note that the cursor is positioned at or near the problem.
  1215.  
  1216.           The problem: 'This is a string' was given to the function to use as
  1217.           an integer value,  but it is a string, not an integer.
  1218.  
  1219.           A good way to learn to recognize errors, is to introduce one error
  1220.           at a time and check the results.
  1221.  
  1222.           Let's look at some errors which you might make in setting up the
  1223.           FUNCTION itself.
  1224.  
  1225.           ##### DO:
  1226.  
  1227.           Change the type of Number to Char.  The FUNCTION declaration should
  1228.           appear:
  1229.  
  1230.           FUNCTION Cube(Number:Char) : Integer;
  1231.  
  1232.           What happens when you attempt to run or compile the program?
  1233.  
  1234.           Error 41: Operand types do not match operator.
  1235.  
  1236.           Notice that the cursor stops, not at the line you modified, but at
  1237.           the line:
  1238.  
  1239.           Cube := Number * Number * Number;
  1240.  
  1241.           Why?
  1242.  
  1243.           There is nothing wrong with the line you changed.  It may not do
  1244.           what you want it to, but to the computer, it is just an Integer
  1245.           FUNCTION with a character for input.
  1246.  
  1247.            The problem appears (to the compiler) when an attempt is made to
  1248.           multiply a character times a character in the line marked by the
  1249.           cursor.  At this point the compiler alerts you to the problem.
  1250.  
  1251.           DEBUGGING NOTE: DON'T FORGET, THE CURSOR POINTS TO THE PLACE WHERE
  1252.           THE ERROR WAS DETECTED BY THE COMPILER.  THE CAUSE OF THE ERROR MAY
  1253.           BE ELSEWHERE!
  1254.  
  1255.           What would happen if you inadvertently designated the function as
  1256.           something other than Integer?
  1257.  
  1258.           ##### DO:
  1259.  
  1260.           Change the FUNCTION declaration to:
  1261.  
  1262. î        TURBO-LESSONS - A Pascal Tutorial       Version 5.0        Page  71
  1263.             Copyright 1989  Lyle M. Faurot      Lessons 1-16: $10.00
  1264.  
  1265.  
  1266.  
  1267.           FUNCTION Cube(Number:Integer) : Char;
  1268.  
  1269.           Run the program.    What results did you get?
  1270.  
  1271.           Error 26 again - Type mismatch.
  1272.  
  1273.           Again, the cursor points to the calculation.
  1274.  
  1275.           This time though, the calculation itself is o.k.    The problem
  1276.           occurs when an attempt is made to assign the integer result to the
  1277.           Character FUNCTION, Cube.
  1278.  
  1279.           ********************** SHAREWARE REMINDER ************************
  1280.  
  1281.           Are you enjoying the lessons?  Have you sent your payment yet?  If
  1282.           you are like me, no matter how good your intentions, you may
  1283.           neglect to send your check if you wait until you have finished the
  1284.           lessons and set them aside!  (Just a friendly reminder--I would
  1285.           like to write more tutorials.  Your check makes that possible.)
  1286.