home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / pascal1.zip / CHAP4.TXT < prev    next >
Text File  |  1988-01-15  |  16KB  |  389 lines

  1.  
  2.            CHAPTER 4 - The Pascal loops and control structures
  3.  
  4.  
  5.              Every program we have examined to this point has been a
  6.         simple  one pass through with no statements being  repeated.
  7.         As in all other languages, Pascal has extensive capabilities
  8.         to  do looping and conditional branching.  We will  look  at
  9.         these now.
  10.  
  11.                                THE FOR LOOP
  12.  
  13.              We will start with what may be the easiest structure to
  14.         understand, the "for" loop.  This is used to repeat a single
  15.         Pascal  statement  any  number of  times  we  desire.   Load
  16.         LOOPDEMO and we will discuss the loops presented there.
  17.  
  18.              The  first  example  is the simplest and  is  simply  a
  19.         repeat  of  a Writeln 7 times.  We have three  new  reserved
  20.         words,  "for", "to", and "do" which are used as shown.   Any
  21.         simple  variable of type integer, byte, or char can be  used
  22.         for  the  loop  index  and  it must  be  defined  in  a  var
  23.         statement.   Following the "do" reserved word is any  single
  24.         Pascal statement that will be repeated the specified  number
  25.         of  times.  Note that the loop is an incrementing  loop  but
  26.         substitution   of   "downto"  for  "to"  will  make   it   a
  27.         decrementing  loop as is illustrated in the last example  in
  28.         this  program.   It  should be pointed  out  that  the  loop
  29.         control variable can only be incremented or decremented by 1
  30.         each time through the loop in  Pascal.
  31.  
  32.                         A COMPOUND PASCAL STATEMENT
  33.  
  34.              The  second example contains our first compound  Pascal
  35.         statement.  It was mentioned in Chapter 1 that the begin end
  36.         pair of reserved words could be used to mark the limits of a
  37.         compound  statement.   In this case,  the  single  statement
  38.         starting  with  the  "begin"  at the  end  of  line  17  and
  39.         extending through and including the end statement in line 21
  40.         is  the  single Pascal statement that will  be  executed  10
  41.         times.   A  second  variable Total has  been  introduced  to
  42.         simply add another operation to the loop.  Any valid  Pascal
  43.         operation  can  be performed within the  "begin  end"  pair,
  44.         including  another  for loop, resulting in nested  loops  to
  45.         whatever depth you desire.
  46.  
  47.              The  third  example shows how the  char  type  variable
  48.         could be used in a for loop.  Pascal requires that the  loop
  49.         variable, the starting point, and the ending point all be of
  50.         the  same  type or it will generate an  error  message.   In
  51.         addition,  it must be a variable of type integer,  byte,  or
  52.         char.  The starting point and ending point can be  constants
  53.         or expressions of arbitrary complexity.
  54.  
  55.  
  56.  
  57.  
  58.                                 Page 20
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.            CHAPTER 4 - The Pascal loops and control structures
  69.  
  70.  
  71.              The fourth example is a decrementing loop as  mentioned
  72.         earlier.  It uses the reserved word "downto".
  73.  
  74.                              THE IF STATEMENT
  75.  
  76.              Pascal has two conditional branching capabilities,  the
  77.         "if"  and the "case".  We will look at one of them now,  the
  78.         if  statement.  Load IFDEMO for an onscreen look at the  "if
  79.         then"  pair  of reserved words.  Any condition that  can  be
  80.         reduced  to  a boolean answer is put between the  "if  then"
  81.         pair  of  words.  If the resulting  expression  resolves  to
  82.         TRUE,   then  the  following  single  Pascal  statement   is
  83.         executed,  and if it resolves to FALSE, then  the  following
  84.         single  statement  is  skipped over.   Of  course,  you  can
  85.         probably  guess  that the single statement can  be  replaced
  86.         with a compound statement bracketed with a "begin end"  pair
  87.         and you are correct.  Study example 1 and you will see  that
  88.         the line will always be printed in this particular  fragment
  89.         because  Three is equal to One + Two.  It is very  difficult
  90.         to come up with a good example without combining some of the
  91.         other control structures but we will do so in the next file.
  92.  
  93.              The  second example in lines 14 through 19, is  similar
  94.         to  the first but has the single statement replaced  with  a
  95.         compound statement and should be easy to understand.
  96.  
  97.              The  third example in lines 21 through 24,  contains  a
  98.         new reserved word, "else".  When the if condition is  FALSE,
  99.         the  single  statement  is skipped and  if  a  semicolon  is
  100.         encountered, the if clause is totally complete.  If  instead
  101.         of  a  semicolon, the reserved word "else"  is  encountered,
  102.         then the single Pascal statement following else is executed.
  103.         One  and  only one of the two statements  will  be  executed
  104.         every  time the if statement is encountered in the  program.
  105.         Examination  of  the third example should clear this  up  in
  106.         your mind.
  107.  
  108.              Notice that the Pascal compiler is looking for either a
  109.         semicolon  to  end the if, or the reserved  word  "else"  to
  110.         continue  the  logic.  It is therefore not legal  to  use  a
  111.         semicolon  immediately preceding the reserved  word  "else".
  112.         You will get a compiler error if you do so.
  113.  
  114.                          THE IF-THEN-ELSE block
  115.  
  116.              Put on your thinking cap because the next principle  is
  117.         difficult  to grasp at first but will suddenly clear up  and
  118.         be  one  of  the most useful facts  of  Pascal  programming.
  119.         Since  the entire "if then else" block of code is  itself  a
  120.         single  Pascal  statement  by definition,  it  can  be  used
  121.         anywhere that an executable statement is legal without begin
  122.  
  123.  
  124.                                 Page 21
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.            CHAPTER 4 - The Pascal loops and control structures
  135.  
  136.  
  137.         end separators.  This is shown in the fourth example of  the
  138.         IFDEMO Pascal example program.  Lines 27 through 30 comprise
  139.         a single Pascal statement, and lines 32 through 35  comprise
  140.         another.  The if statement begun in line 26 therefore has  a
  141.         single statement in each of its branches.
  142.  
  143.              The  "if then else" construct is one of the most  used,
  144.         most useful, and therefore most important aspects of Pascal.
  145.         For this reason you should become very familiar with it.
  146.  
  147.              Try  changing  some of the conditions  in  the  example
  148.         program to see if you can get it to print when you expect it
  149.         to for your own practice.  When you are ready, we will go on
  150.         to a program with loops and conditional statements  combined
  151.         and working together.
  152.  
  153.                           LOOPS AND IFS TOGETHER
  154.  
  155.              Load  LOOPIF  and  observe it for a  few  minutes.   It
  156.         contains most of what you have studied so far and should  be
  157.         understandable  to  you at this point.  It contains  a  loop
  158.         (lines 7 & 17) with two if statements within it (lines 8 & 9
  159.         and lines 10 through 16), and another loop (lines 11 through
  160.         15) within one of the if statements.
  161.  
  162.              You  should  make careful note of the  formatting  used
  163.         here.   The "begin" is at the end of the line  which  starts
  164.         the control and the "end" is lined up under the control word
  165.         such  that  it  is  very clear  which  control  word  it  is
  166.         associated with.  You will develop your own clear method  of
  167.         formatting your code in time but until then it is  suggested
  168.         that you follow this example.
  169.  
  170.              An  easily  made error should be pointed  out  at  this
  171.         time.  If an extraneous semicolon were put at the end of the
  172.         if  statement  in line 8, the code following  the  statement
  173.         would  always be executed because the "null" statement  (the
  174.         nothing  statement  between the "then"  and  the  semicolon)
  175.         would be the conditional statement.  The compiler would  not
  176.         generate  an  error  and you would get no  warning.   Add  a
  177.         semicolon at the end of line 8 to see the error.
  178.  
  179.                        FINALLY, A MEANINGFUL PROGRAM
  180.  
  181.              Load  TEMPCONV  and  study its  structure.  Notice  the
  182.         header block that defines the program and gives a very brief
  183.         explanation  of what the program does.  This program  should
  184.         pose  no problem to you in understanding what it does  since
  185.         it  is  so clearly documented.  Run it and you will  have  a
  186.         list  of  Centigrade to Fahrenheit  temperature  conversions
  187.         with a few added notes.
  188.  
  189.  
  190.                                 Page 22
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.            CHAPTER 4 - The Pascal loops and control structures
  201.  
  202.  
  203.  
  204.              Load,  examine, and run DUMBCONV for a good example  of
  205.         poor  variable  naming.   The structure of  the  program  is
  206.         identical to the last program and when you run it, you  will
  207.         see that it is identical in output, but compared to the last
  208.         program,  it  is  difficult to understand what  it  does  by
  209.         studying the listing.  This program, like the last should be
  210.         easily  understood  by  you, so we will go on  to  our  next
  211.         Pascal control structure.
  212.  
  213.                            THE REPEAT UNTIL LOOP
  214.  
  215.              The next two Pascal constructs are very similar because
  216.         they are both indefinite loops (indefinite because they  are
  217.         not executed a fixed number of times).  One of the loops  is
  218.         evaluated  at the top and the other at the bottom.  It  will
  219.         probably  be  easier  to start  with  the  "repeat"  "until"
  220.         construct which is the loop that is evaluated at the bottom.
  221.  
  222.              Retrieve  the  file REPEATLP to see  an  example  of  a
  223.         repeat  loop.   Two more reserved words  are  defined  here,
  224.         namely  "repeat" and "until".  This rather simple  construct
  225.         simply repeats all statements between the two reserved words
  226.         until the boolean expression following the "until" is  found
  227.         to  be  TRUE.  This is the only expression I  know  of  that
  228.         operates  on  a  range of statements rather  than  a  single
  229.         statement and begin end delimiters are not required.
  230.  
  231.              A word of caution is in order here.  Since the loop  is
  232.         executed  until some condition becomes TRUE, it is  possible
  233.         that  the  condition will never be TRUE and  the  loop  will
  234.         never terminate.  It is up to you, the programmer, to insure
  235.         that the loop will eventually terminate.
  236.  
  237.              Compile and run REPEATLP to observe the output.
  238.  
  239.                               THE WHILE LOOP
  240.  
  241.              The  file  WHILELP contains an example of  another  new
  242.         construct,  the  "while" loop.  This uses the  "while"  "do"
  243.         reserved words and will execute one Pascal statement (or one
  244.         compound statement bounded with begin and end)  continuously
  245.         until  the boolean expression between the two words  becomes
  246.         FALSE.
  247.  
  248.              This  loop  is also indeterminate and could,  like  the
  249.         repeat  until loop, never terminate.  You  should  therefore
  250.         exercise care in using it.
  251.  
  252.              There are two basic differences in the last two  loops.
  253.         The repeat until loop is evaluated at the bottom of the loop
  254.  
  255.  
  256.                                 Page 23
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.            CHAPTER 4 - The Pascal loops and control structures
  267.  
  268.  
  269.         and  must therefore always go through the loop at least  one
  270.         time.  The while loop is evaluated at the top and may not go
  271.         through even once.  This gives you flexibility when choosing
  272.         the loop to do the job at hand.
  273.  
  274.              Compile,  run, and examine the output from the  example
  275.         program WHILELP.
  276.  
  277.                             THE CASE STATEMENT
  278.  
  279.              The   final  control  structure  introduces  one   more
  280.         reserved  word, "case".  The case construct actually  should
  281.         be included with the if statement since it is a  conditional
  282.         execution statement, but I chose to save it for last because
  283.         it is rather unusual and will probably be used less than the
  284.         others we have discussed in this chapter.
  285.  
  286.              The  case  statement  is used to  select  one  of  many
  287.         possible  simple Pascal statements to execute based  on  the
  288.         value  of  a simple variable.  Load the  file  CASEDEMO  and
  289.         observe the program for an example of a case statement.  The
  290.         variable between the "case" and "of" reserved words in  line
  291.         9  is  the variable used to make the  selection.   Following
  292.         that, the various selections are listed as a possible  value
  293.         or  range, followed by a colon, a single  Pascal  statement,
  294.         and  a semicolon for each selector.  Following the  list  of
  295.         selections, an "else" can be added to cover the  possibility
  296.         that none of the selections were executed.  Finally, an  end
  297.         statement  is  used to terminate the case  construct.   Note
  298.         that this is one of the few places in Pascal that an end  is
  299.         used without a corresponding begin.
  300.  
  301.              The  example file uses Count for a variable and  prints
  302.         the numbers one through five in text form, and declares that
  303.         numbers  outside this range are not in the  allowable  list.
  304.         The  program should be self explanatory beyond  that  point.
  305.         Be sure to compile and run this example program.
  306.  
  307.              Load and display the sample program BIGCASE for another
  308.         example of a case statement with a few more added  features.
  309.         This  program uses the identical structure as  the  previous
  310.         program  but in line 11 a range is used as the  selector  so
  311.         that if the value of Count is 7, 8, or 9 this selection will
  312.         be  made.   In line 12, three different listed  values  will
  313.         cause  selection  of  this part of  the  code.   Of  greater
  314.         importance  are the compound statements used in some of  the
  315.         selections.  If the variable Count has the value of 2, 4, or
  316.         6, a compound statement will be executed and if the value is
  317.         3,  a  for  loop  is executed.  If the value  is  1,  an  if
  318.         statement is executed which will cause a compound  statement
  319.         to  be executed.  In this case the if statement will  always
  320.  
  321.  
  322.                                 Page 24
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.            CHAPTER 4 - The Pascal loops and control structures
  333.  
  334.  
  335.         be  executed  because  TRUE will always  be  true,  but  any
  336.         Boolean expression could be used in the expression.
  337.  
  338.              Be sure to compile and run this program, then study the
  339.         output until you understand the result thoroughly.
  340.  
  341.              This brings us to the end of chapter 4 and you now have
  342.         enough information to write essentially any program  desired
  343.         in  Pascal.   You  would  find that you  would  have  a  few
  344.         difficulties  if  you attempted to try to write a  very  big
  345.         program  without  the  topics  coming up  in  the  next  few
  346.         chapters.   The  additional topics will greatly add  to  the
  347.         flexibility  of Pascal and will greatly ease programming  in
  348.         it.
  349.  
  350.  
  351.                            PROGRAMMING EXERCISES
  352.  
  353.         1.  Write a program that lists the numbers from 1 to 12  and
  354.             writes a special message beside the number  representing
  355.             your month of birth.
  356.  
  357.         2.  Write  a program that lists all of the numbers from 1 to
  358.             12 except for the numbers 2 and 9.
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.                                 Page 25
  389.