home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / modula2 / mod2txt.zip / CHAP4.TXT < prev    next >
Text File  |  1987-03-25  |  18KB  |  388 lines

  1.              Chapter 4 - Modula-2 Loops and Control Structures
  2.  
  3.  
  4.              Loops  are  some  of the most important and  most  used
  5.         constructs  in computer programming and in all parts of your
  6.         life.   You use loops all the time for many things.  Walking
  7.         is  a repetition of putting one foot in front of  the  other
  8.         until  you  get  where you are  going.   Eating  a  sandwich
  9.         involves a loop of eating,  chewing,  swallowing,  etc.   In
  10.         this  chapter we will first cover all of the possible  loops
  11.         you  can  define  in Modula-2,  then go on  to  the  control
  12.         structures, the decision makers.
  13.  
  14.              Load  and display the program LOOPDEMO.MOD.   This is a
  15.         rather  large  program compared to the ones we have seen  so
  16.         far, but I felt it would be better to cover all of the loops
  17.         in one file than have you compile and run 4 different files.
  18.  
  19.                            REPEAT ... UNTIL LOOP
  20.  
  21.              Ignoring  the declaration part of the listing and going
  22.         straight to the program itself,  we first come to the REPEAT
  23.         loop  which  does  just what it says it will  do.   It  will
  24.         repeat until it is told to stop.   The REPEAT in line 16 and
  25.         the UNTIL go together,  and everything between them will  be
  26.         executed  until  the condition following the  UNTIL  becomes
  27.         TRUE.   The  condition  can  be  any  expression  that  will
  28.         evaluate to a BOOLEAN answer, TRUE or FALSE.  It can even be
  29.         a composite expression with AND's,  OR's,  and NOT's like we
  30.         studied  in the last chapter.   It can be composed of any of
  31.         the  simple types discussed so far as long as the terms  are
  32.         all compatible and it evaluates to a BOOLEAN value.  In this
  33.         case we have a very simple expression,  "Index = 5".   Since
  34.         "Index" is initialized to 0 and is incremented each time  we
  35.         go  through the loop,  it will eventually reach a value of 5
  36.         and   the  loop  will  terminate,   after  which  time   the
  37.         expressions following it will be executed.
  38.  
  39.             We are not quite finished with the REPEAT loop  yet,  we
  40.         will  have  more to say about it when we complete the  WHILE
  41.         loop.
  42.  
  43.                                 WHILE  LOOP
  44.  
  45.              The WHILE loop is very much like the REPEAT loop except
  46.         that  the condition is tested at the beginning of  the  loop
  47.         and   when  the  condition  becomes  FALSE,   the  loop   is
  48.         terminated.   Once again, the condition can be as complex as
  49.         desired  but in this case it is the very simple "Index < 5".
  50.         When  Index  reaches  5,  the loop  is  terminated  and  the
  51.         statements following the loop are executed.
  52.  
  53.              The biggest difference between the REPEAT and the WHILE
  54.         loops is concerned with when the test is made.  In the WHILE
  55.  
  56.  
  57.                                  Page 23
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.              Chapter 4 - Modula-2 Loops and Control Structures
  68.  
  69.  
  70.         loop,  the test is made at the beginning,  so it is possible
  71.         that  the  statements inside the loop will not  be  executed
  72.         even once.   In the REPEAT loop, the test is made at the end
  73.         of  the loop,  so the statements in the loop will always  be
  74.         executed  at least once.   It is also good to keep  in  mind
  75.         that the REPEAT stops when its condition goes TRUE,  and the
  76.         WHILE stops when its condition goes FALSE.
  77.  
  78.              There is another loop that we can use in which we  exit
  79.         from  the center using any test we can devise.   It will  be
  80.         covered after we complete the FOR loop.
  81.  
  82.                                 THE FOR LOOP
  83.  
  84.              The  FOR  loop exists in one form or another in  nearly
  85.         every  programming language and you will use  it  repeatedly
  86.         because  it is so useful.   It uses the reserved words  FOR,
  87.         TO,  BY,  DO,  and  END.   It uses any simple variable  type
  88.         except  REAL,  and counts loops depending on what counts you
  89.         put in for beginning and ending points.   The first  example
  90.         on  line 31 says for the computer to start "Index" at 1  and
  91.         count  to 5,  going through the loop once for each value  of
  92.         "Index".   The count advances by 1 each time because nothing
  93.         else is specified and 1 is the default.  The end of the loop
  94.         is  specified  by  the  reserved  word  END,   and  as  many
  95.         statements as desired can be within the body of the loop.
  96.  
  97.              The  next  loop starts in line 37 and this time  counts
  98.         from 5 to 25 but incrementing by 4 each time because of  the
  99.         "BY  4" part of the line.   The loop will continue until the
  100.         second limit is going to be exceeded, at which time the loop
  101.         will  stop.   The beginning and ending limits can themselves
  102.         be some kind of a calculated value or a constant,  the  only
  103.         provision  being  that they must be of the same type as  the
  104.         loop  indexing variable.   In fact they can be negative  and
  105.         the increment value can be negative.  This is illustrated in
  106.         the  next loop that starts in line 48 where we count  by  -7
  107.         until we go from 5 to -35.  No further explanation should be
  108.         required for this loop.
  109.  
  110.              The  next loop,  starting in line 54,  uses  calculated
  111.         limits  to  determine its starting and ending points and  it
  112.         uses the name "Where" for its incrementing value.  The value
  113.         of  "Where"  is established in the definition part  of  this
  114.         program  as a constant.   It is simply used here and will be
  115.         explained in a future lesson when we get to it.   "Where" is
  116.         a  constant with a value of 11,  and the incrementing  value
  117.         must always be a constant.
  118.  
  119.              The  next  two FOR loops use a CHAR type  variable  and
  120.         simply "count" from "A" to "Z",  or backwards in the case of
  121.  
  122.  
  123.                                  Page 24
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.              Chapter 4 - Modula-2 Loops and Control Structures
  134.  
  135.  
  136.         the second one.
  137.  
  138.              Several things should be pointed out about the FOR loop
  139.         for you.  The three values must agree in type,  that is  the
  140.         index,  the starting point, and the ending point.  The index
  141.         must  not  be  changed by any logic within the loop  or  the
  142.         results will be unpredictable.   The value of the index must
  143.         be assumed to be undefined after the loop  terminates.   You
  144.         may discover that it is predictable on your compiler, but it
  145.         may  not  be  on some other compiler,  and you may  want  to
  146.         transfer your program to another system someday.
  147.  
  148.                              THE INFINITE LOOP
  149.  
  150.              The fourth and final loop is an infinite loop, it never
  151.         terminates by itself.  It is up to you the programmer to see
  152.         to  it that some means of terminating it is  available,  the
  153.         most  usual is through use of the EXIT statement.   Anyplace
  154.         in the loop you can set up some conditions for exiting based
  155.         on whatever you desire.   Executing the EXIT procedure  will
  156.         cause  the  program  control  to leave the  loop  and  begin
  157.         executing the statements following the loop.
  158.  
  159.              Now  you have been exposed to the four loops  available
  160.         in Modula-2,  the REPEAT,  WHILE, FOR, and LOOP.  Spend some
  161.         time  studying this program,  then compile and run it to see
  162.         if  it  does  what you expect it  to  do.   Loops  are  very
  163.         important.  You  will  do the vast majority of your  logical
  164.         control in loops and IF statements.
  165.  
  166.                           WHAT IS AN IF STATEMENT?
  167.  
  168.              Load  the  program  IFDEMO.MOD and display it  on  your
  169.         monitor for an example of some IF statements.   Ignoring the
  170.         header  we  notice that the program is composed of  one  big
  171.         loop in order to have some changing variables.   Within  the
  172.         loop  are  3  IF  statements,   the  most  used  conditional
  173.         statement in Modula-2.
  174.  
  175.              The first IF statement is given in line 11.   It simply
  176.         says  "if  the  value of Index1 is less  than  4,  then"  do
  177.         everything  from the reserved word THEN to the reserved word
  178.         END which is associated with it.   If the value of Index1 is
  179.         not  less than 4,  then all of these statements are  ignored
  180.         and  the  next  statement  to be executed will  be  the  one
  181.         following the reserved word END.  In a nutshell, that is all
  182.         there  is  to the simple  IF  statement.   Once  again,  the
  183.         condition  can  be  any expression that will evaluate  to  a
  184.         BOOLEAN result,  and it can be composed of any of the simple
  185.         types of data elements.
  186.  
  187.  
  188.  
  189.                                  Page 25
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.              Chapter 4 - Modula-2 Loops and Control Structures
  200.  
  201.  
  202.                              THE "ELSE" CLAUSE
  203.  
  204.              The  second IF statement,  beginning in line 17 has  an
  205.         added feature,  the ELSE clause.   If the BOOLEAN expression
  206.         does not evaluate to TRUE,  then instead of the  expressions
  207.         following  the THEN being executed,  the group following the
  208.         ELSE will be.  Thus, if it is TRUE, everything from the THEN
  209.         to the ELSE is executed, but if it is FALSE, everything from
  210.         the  ELSE  to the END is executed.   The  END  statement  is
  211.         therefore the terminator for the effect of the IF statement.
  212.  
  213.                      WHAT CAN GO IN THE IF STATEMENTS?
  214.  
  215.              You  may  be wondering what is allowed to go  into  the
  216.         group of executable statements between the THEN and the ELSE
  217.         or  some other place.   The answer is,  anything you want to
  218.         put there.  You can put other IF statements, loops, input or
  219.         output statements,  calculations,  just about anything.   If
  220.         you indent the statements properly, you will even be able to
  221.         read and understand what you put in there and why you put it
  222.         there.   Of course, if you put a loop in there, for example,
  223.         you can put other constructs within the loop including other
  224.         IF statements, etc.  Thus you can go as far as you desire in
  225.         building up a program.
  226.  
  227.                               THE ELSIF CLAUSE
  228.  
  229.              The third and last kind of IF statement is given in the
  230.         third  example starting on line 24.   In this case,  if  the
  231.         expression within the IF statement is found to be FALSE, the
  232.         statements  following  the  THEN are skipped  and  the  next
  233.         construct  is found,  the ELSIF.   If program control  comes
  234.         here,  it  has a further expression to  evaluate,  which  if
  235.         TRUE,  will  cause the statements immediately following  its
  236.         THEN  to  be executed.   If this expression is found  to  be
  237.         FALSE,  the  statements following the ELSE will be executed.
  238.         The net result is that,  one and only one of the 3 groups of
  239.         instructions  will be executed each time through  the  loop.
  240.         It  is permissible to add as many ELSIF cases as desired  to
  241.         this  construct,   leading  to  a  "many  way"  branch.   In
  242.         addition,  the  ELSE  is  entirely  optional  regardless  of
  243.         whether or not the ELSIF's are used.
  244.  
  245.              After  studying this program,  compile and run  it  and
  246.         compare the results with what you expected.
  247.  
  248.                           LOOP's IN IF's IN LOOP's
  249.  
  250.              Load  and  display the next example program  LOOPIF.MOD
  251.         for an example of some of the latest topics being  combined.
  252.         This  program makes nonsense data but is valuable because it
  253.  
  254.  
  255.                                  Page 26
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.              Chapter 4 - Modula-2 Loops and Control Structures
  266.  
  267.  
  268.         is small enough to understand quickly to see how LOOP's  and
  269.         IF's  can be nested together.   The entire program is a  FOR
  270.         loop  containing  an  IF statement.   Each part  of  the  IF
  271.         statement  has a loop nested within it.   There is no reason
  272.         why this process could not be continued if there were a need
  273.         to.  Study this program then compile and run it.
  274.  
  275.                        FINALLY, A MEANINGFUL PROGRAM
  276.  
  277.              Load  and  display the program named  TEMPCONV.MOD  for
  278.         your  first look at a program that really does do  something
  279.         useful.   This  is  a  program  that  generates  a  list  of
  280.         temperatures in centigrade,  converts the list to farenheit,
  281.         and displays the list along with a note in the table at  the
  282.         freezing point and boiling point of water.   You should have
  283.         no difficulty understanding this program, so the fine points
  284.         will be left up to you.
  285.  
  286.              A  few comments on good formatting is in order at  this
  287.         point.   Notice  the temperature conversion program and  how
  288.         well  it is formatted.   It is simple to follow the flow  of
  289.         control, and the program itself needs no comments because of
  290.         the judicious choice of variable names.  The block header at
  291.         the  top of the page is a good example of how you should get
  292.         used  to defining your programs.   A simple block header  of
  293.         that  variety  goes  a  long way  toward  making  a  program
  294.         maintainable and useful later.   Take notice also of the way
  295.         the variables are each defined in a comment.   A program  as
  296.         simple  as  this probably doesn't need this much  attention,
  297.         but it would be good for you to get into practice early.  It
  298.         would be good for you to think of each of your programs as a
  299.         work of art and strive to make them look good.
  300.  
  301.              After spending some time studying this program, compile
  302.         and  run  it to see what it does.  Load and study  the  next
  303.         program named DUMBCONV.MOD to see if you can figure out what
  304.         it does.   If you are really sharp,  you will see that it is
  305.         the  same  program  as the last one but without all  of  the
  306.         extra effort to put it into a neat,  easy to follow  format.
  307.         Compile and run this program and you will see that they both
  308.         do  the  same  thing.   They  are identical as  far  as  the
  309.         computer is concerned.   But there is a world of  difference
  310.         in the way they can be understood by a human being.
  311.  
  312.                              THE CASE STATEMENT
  313.  
  314.              Load  and display the program named CASEDEMO.MOD for an
  315.         example  of the last decision making construct in  Modula-2,
  316.         the CASE statement.  A CASE statement is a "many-way" branch
  317.         based  on some simple variable.   In this program we have  a
  318.         loop which sets the variable "Dummy" to the values from 1 to
  319.  
  320.  
  321.                                  Page 27
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.              Chapter 4 - Modula-2 Loops and Control Structures
  332.  
  333.  
  334.         25 successively.  Each time it comes to the CASE  statement,
  335.         one of the branches is taken.   The first branch is taken if
  336.         the value is from 1 to 5,  the second branch is taken if the
  337.         value is from 6 to 9,  the third is taken if it is either  a
  338.         10 or 11, etc.  Finally, if the value is not found in any of
  339.         the branches, the ELSE path is taken as would be the case of
  340.         a 12,  a 13,  or a few others.   The important point is that
  341.         one  and only one of the many paths are taken each time  the
  342.         CASE construct is entered.   The CASE variable can be any of
  343.         the  simple types except for the REAL type.   For each path,
  344.         as many statements can be executed as desired before the "|"
  345.         is  put  in  to end that path.   The  CASE  statement  is  a
  346.         powerful  statement when you need it but you will not use it
  347.         nearly  as  often as you will use the IF statement  and  the
  348.         various loops.
  349.  
  350.         PROGRAMMING EXERCISES
  351.  
  352.         1.   Write a program that will put your name on the monitor
  353.              10 times using a loop.
  354.  
  355.         2.   Write a program that lists the numbers from 1 to 12 on
  356.              the monitor and prints a special message beside the
  357.              number that represents the month of your birthday.
  358.  
  359.         3.   Write a program that calculates and lists the numbers
  360.              from 1 to 8 along with the factorial of each. This
  361.              will require use of a loop within a loop.  A factorial
  362.              is the number obtained by multiplying each number less
  363.              than and up to the number in question.  For example,
  364.              factorial 4 = 1 * 2 * 3 * 4.  Use a CARDINAL type var-
  365.              iable for the result, then change it to an INTEGER to
  366.              see the difference in output due to the range of the
  367.              two different variable types.  This is a good illustra-
  368.              tion of the fact that careful choice of variable type
  369.              is sometimes very important.
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.                                  Page 28
  388.