home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PASCTUT1.ZIP / CHAP4.TXT < prev    next >
Encoding:
Text File  |  1986-07-14  |  12.4 KB  |  323 lines

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