home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / CTUTOR1.ZIP / CHAP3.TXT < prev    next >
Encoding:
Text File  |  1986-06-30  |  17.9 KB  |  389 lines

  1.                         Chapter 3 - Program Control                        
  2.  
  3.  
  4.                                THE WHILE LOOP
  5.  
  6.              The  C programming language has several structures  for 
  7.  
  8.         looping  and conditional branching.   We will cover them all 
  9.  
  10.         in this chapter and we will begin with the while loop.   The 
  11.  
  12.         while  loop continues to loop while some condition is  true.
  13.  
  14.         When   the   condition  becomes  false,   the   looping   is 
  15.  
  16.         discontinued.   It therefore does just what it says it does, 
  17.  
  18.         the name of the loop being very descriptive. 
  19.  
  20.              Load the program WHILE.C and display it for an  example 
  21.  
  22.         of  a while loop.   We begin with a comment and the  program 
  23.  
  24.         name,  then  go  on  to define an integer  variable  "count" 
  25.  
  26.         within the body of the program.  The variable is set to zero 
  27.  
  28.         and we come to the while loop itself.  The syntax of a while 
  29.  
  30.         loop is just as shown here.  The keyword "while" is followed 
  31.  
  32.         by an expression of something in parentheses,  followed by a 
  33.  
  34.         compound  statement  bracketed by braces.   As long  as  the 
  35.  
  36.         expression in parenthesis is true, all statements within the 
  37.  
  38.         braces will be executed.   In this case,  since the variable 
  39.  
  40.         count  is incremented by one every time the  statements  are 
  41.  
  42.         executed, it will eventually reach 6, the statement will not 
  43.  
  44.         be executed,  and the loop will be terminated.   The program 
  45.  
  46.         control   will   resume  at  the  statement  following   the 
  47.  
  48.         statements in braces. 
  49.  
  50.              We  will  cover  the compare  expression,  the  one  in 
  51.  
  52.         parentheses, in the next chapter.  Until then, simply accept 
  53.  
  54.         the  expressions for what you think they should do  and  you 
  55.  
  56.         will probably be correct.
  57.  
  58.              Several  things must be pointed out regarding the while 
  59.  
  60.         loop.   First,  if the variable count were initially set  to 
  61.  
  62.         any  number greater than 5,  the statements within the  loop 
  63.  
  64.         would  not be executed at all,  so it is possible to have  a 
  65.  
  66.         while  loop  that  never  is  executed.   Secondly,  if  the 
  67.  
  68.         variable  were  not incremented in the loop,  then  in  this 
  69.  
  70.         case,  the loop would never terminate, and the program would 
  71.  
  72.         never complete.   Finally, if there is only one statement to 
  73.  
  74.         be executed within the loop, it does not need braces but can 
  75.  
  76.         stand alone.
  77.  
  78.              Compile and run this program.
  79.  
  80.                              THE DO-WHILE LOOP
  81.  
  82.              A  variation  of the while loop is illustrated  in  the 
  83.  
  84.         program DOWHILE.C,  which you should load and display.  This 
  85.  
  86.         program  is nearly identical to the last one except that the 
  87.  
  88.         loop  begins  with the reserved word  "do",  followed  by  a 
  89.  
  90.         compound  statement  in  braces,   then  the  reserved  word 
  91.  
  92.  
  93.  
  94.                                  Page 12 
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.                         Chapter 3 - Program Control                        
  105.  
  106.  
  107.         "while",  and  finally  an expression in  parentheses.   The 
  108.  
  109.         statements in the braces are executed repeatedly as long  as 
  110.  
  111.         the expression in parentheses is true.   When the expression 
  112.  
  113.         in parentheses becomes false,  execution is terminated,  and 
  114.  
  115.         control passes to the statements following this statement.
  116.  
  117.              Several  things  must  be pointed  out  regarding  this 
  118.  
  119.         statement.  Since  the test is done at the end of the  loop, 
  120.  
  121.         the  statements  in  the braces will always be  executed  at 
  122.  
  123.         least once.   Secondly,  if "i" were not changed within  the 
  124.  
  125.         loop,  the loop would never terminate, and hence the program 
  126.  
  127.         would  never terminate.   Finally,  just like for the  while 
  128.  
  129.         loop,  if  only  one statement will be executed  within  the 
  130.  
  131.         loop,  no braces are required.  Compile and run this program 
  132.  
  133.         to see if it does what you think it should do.
  134.  
  135.              It  should come as no surprise to you that these  loops 
  136.  
  137.         can be nested.  That is, one loop can be included within the 
  138.  
  139.         compound  statement of another loop,  and the nesting  level 
  140.  
  141.         has no limit.
  142.  
  143.                                 THE FOR LOOP
  144.  
  145.              The  "for" loop is really nothing new,  it is simply  a 
  146.  
  147.         new  way  to describe the "while" loop.   Load and edit  the 
  148.  
  149.         file  named  FORLOOP.C for an example of a  program  with  a 
  150.  
  151.         "for"  loop.  The  "for" loop consists of the reserved  word 
  152.  
  153.         "for" followed by a rather large expression in  parentheses.  
  154.  
  155.         This expression is really composed of three fields separated 
  156.  
  157.         by  semi-colons.   The  first field contains the  expression 
  158.  
  159.         "index  = 0" and is an initializing field.   Any expressions 
  160.  
  161.         in  this field are executed prior to the first pass  through 
  162.  
  163.         the loop.   There is essentially no limit as to what can  go 
  164.  
  165.         here,  but  good programming practice would require it to be 
  166.  
  167.         kept simple.   Several initializing statements can be placed 
  168.  
  169.         in this field, separated by commas.
  170.  
  171.              The second field,  in this case containing "index < 6", 
  172.  
  173.         is  the  test which is done at the beginning  of  each  loop 
  174.  
  175.         through  the program.   It can be any expression which  will 
  176.  
  177.         evaluate  to a true or false.   (More will be said about the 
  178.  
  179.         actual value of true and false in the next chapter.)
  180.  
  181.              The expression contained in the third field is executed 
  182.  
  183.         each time the loop is executed but it is not executed  until 
  184.  
  185.         after  those  statements  in the main body of the  loop  are 
  186.  
  187.         executed.   This field, like the first, can also be composed 
  188.  
  189.         of several operations separated by commas.
  190.  
  191.              Following  the  for()  expression  is  any  single   or 
  192.  
  193.         compound statement which will be executed as the body of the 
  194.  
  195.  
  196.  
  197.                                  Page 13 
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.                         Chapter 3 - Program Control                        
  208.  
  209.  
  210.         loop.   A  compound  statement  is  any  group  of  valid  C 
  211.  
  212.         statements enclosed in braces.   In nearly any context in C, 
  213.  
  214.         a  simple statement can be replaced by a compound  statement 
  215.  
  216.         that will be treated as if it were a single statement as far 
  217.  
  218.         as program control goes.  Compile and run this program.
  219.          
  220.                               THE IF STATEMENT
  221.  
  222.              Load  and  display the file IFELSE.C for an example  of 
  223.  
  224.         our first conditional branching statement, the "if".  Notice 
  225.  
  226.         first,  that there is a "for" loop with a compound statement 
  227.  
  228.         as its executable part containing two "if" statements.  This 
  229.  
  230.         is an example of how statements can be nested.  It should be 
  231.  
  232.         clear  to  you  that each of the  "if"  statements  will  be 
  233.  
  234.         executed 10 times.
  235.  
  236.              Consider the first "if" statement.   It starts with the 
  237.  
  238.         keyword  "if" followed by an expression in parentheses.   If 
  239.  
  240.         the expression is evaluated and found to be true, the single 
  241.  
  242.         statement following the "if" is executed,  and if false, the 
  243.  
  244.         following  statement  is  skipped.   Here  too,  the  single 
  245.  
  246.         statement  can be replaced by a compound statement  composed 
  247.  
  248.         of  several statements bounded by  braces.   The  expression 
  249.  
  250.         "data  == 2" is simply asking if the value of data is  equal 
  251.  
  252.         to 2,  this will be explained in detail in the next chapter.  
  253.  
  254.         (Simply suffice for now that if "data = 2" were used in this 
  255.  
  256.         context, it would mean a completely different thing.)
  257.  
  258.                             NOW FOR THE IF-ELSE
  259.  
  260.              The  second  "if"  is  similar to the  first  with  the 
  261.  
  262.         addition  of a new reserved word,  the "else" following  the 
  263.  
  264.         first  printf  statement.   This  simply says  that  if  the 
  265.  
  266.         expression in the parentheses evaluates as true,  the  first 
  267.  
  268.         expression  is executed,  otherwise the expression following 
  269.  
  270.         the "else" is executed.   Thus,  one of the two  expressions 
  271.  
  272.         will  always be executed,  whereas in the first example  the 
  273.  
  274.         single expression was either executed or skipped.  Both will 
  275.  
  276.         find  many uses in your C programming efforts.   Compile and 
  277.  
  278.         run this program to see if it does what you expect.
  279.  
  280.                            THE BREAK AND CONTINUE
  281.  
  282.              Load  the file named BREAKCON.C for an example  of  two 
  283.  
  284.         new statements.  Notice that in the first "for", there is an 
  285.  
  286.         if  statement that calls a break if xx equals 8.   The break 
  287.  
  288.         will  jump  out of the loop you are in and  begin  executing 
  289.  
  290.         statements following the loop,  effectively terminating  the 
  291.  
  292.         loop.   This  is a valuable statement when you need to  jump 
  293.  
  294.         out  of  a  loop  depending on the  value  of  some  results 
  295.  
  296.         calculated  in the loop.   In this case,  when xx reaches 8, 
  297.  
  298.  
  299.  
  300.                                  Page 14 
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.                         Chapter 3 - Program Control                        
  311.  
  312.  
  313.         the  loop is terminated and the last value printed  will  be 
  314.  
  315.         the previous value, namely 7.
  316.  
  317.              The  next  "for" loop,  contains a  continue  statement 
  318.  
  319.         which  does not cause termination of the loop but jumps  out 
  320.  
  321.         of the present iteration.  When the value of xx reaches 8 in 
  322.  
  323.         this case,  the program will jump to the end of the loop and 
  324.  
  325.         continue  executing  the loop,  effectively eliminating  the 
  326.  
  327.         printf statement during the pass through the loop when xx is 
  328.  
  329.         eight.   Compile and run the program to see if it does  what 
  330.  
  331.         you expect.
  332.  
  333.                             THE SWITCH STATEMENT
  334.  
  335.              Load  and  display the file SWITCH.C for an example  of 
  336.  
  337.         the  biggest construct yet in the C  language,  the  switch.  
  338.  
  339.         The switch is not difficult, so don't let it intimidate you.  
  340.  
  341.         It  begins with the keyword "switch" followed by a  variable 
  342.  
  343.         in parentheses which is the switching variable, in this case 
  344.  
  345.         "truck".   As many cases as desired are then enclosed within 
  346.  
  347.         a pair of braces.  The reserved word "case" is used to begin 
  348.  
  349.         each  case  entered followed by the value of  the  variable, 
  350.  
  351.         then a colon, and the statements to be executed.
  352.  
  353.              In  this example,  if the variable "truck" contains the 
  354.  
  355.         value 3 during this pass of the switch statement, the printf 
  356.  
  357.         will  cause "The value is three" to be  displayed,  and  the 
  358.  
  359.         "break" statement will cause us to jump out of the switch. 
  360.  
  361.              Once  an  entry  point is  found,  statements  will  be 
  362.  
  363.         executed until a "break" is found or until the program drops 
  364.  
  365.         through  the bottom of the switch braces.   If the  variable 
  366.  
  367.         has  the value 5,  the statements will begin executing where 
  368.  
  369.         "case  5  :" is found,  but the first statements  found  are 
  370.  
  371.         where the case 8 statements are.  These are executed and the 
  372.  
  373.         break  statement  in the "case 8" portion  will  direct  the 
  374.  
  375.         execution  out the bottom of the switch.   The various  case 
  376.  
  377.         values can be in any order and if a value is not found,  the 
  378.  
  379.         default portion of the switch will be executed.
  380.  
  381.              It should be clear that any of the above constructs can 
  382.  
  383.         be  nested  within  each  other  or  placed  in  succession, 
  384.  
  385.         depending on the needs of the particular programming project 
  386.  
  387.         at hand.
  388.  
  389.              Compile  and  run SWITCH.C to see if it does  what  you 
  390.  
  391.         expect it to after this discussion.
  392.  
  393.              Load  and display the file GOTOEX.C for an example of a 
  394.  
  395.         file  with some "goto" statements in it.   To use  a  "goto" 
  396.  
  397.         statement,  you simply use the reserved word "goto" followed 
  398.  
  399.  
  400.  
  401.                                  Page 15 
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.                         Chapter 3 - Program Control                        
  412.  
  413.  
  414.         by the symbolic name to which you wish to jump.  The name is 
  415.  
  416.         then  placed  anywhere in the program followed by  a  colon.  
  417.  
  418.         You  are  not  allowed to jump into any loop,  but  you  are 
  419.  
  420.         allowed to jump out of a loop.  Also, you are not allowed to 
  421.  
  422.         jump out of any function into another.   These attempts will 
  423.  
  424.         be  flagged by your compiler as an error if you attempt  any 
  425.  
  426.         of them.
  427.  
  428.              This  particular program is really a mess but it  is  a 
  429.  
  430.         good example of why software writers are trying to eliminate 
  431.  
  432.         the  use of the "goto" statement as much as  possible.   The 
  433.  
  434.         only place in this program where it is reasonable to use the 
  435.  
  436.         "goto"  is the one in line 17 where the program jumps out of 
  437.  
  438.         the three nested loops in one jump.   In this case it  would 
  439.  
  440.         be  rather messy to set up a variable and jump  successively 
  441.  
  442.         out of all three loops but one "goto" statement gets you out 
  443.  
  444.         of all three.
  445.  
  446.              Some  persons say the "goto" statement should never  be 
  447.  
  448.         used  under  any  circumstances but this  is  rather  narrow 
  449.  
  450.         minded  thinking.   If there is a place where a "goto"  will 
  451.  
  452.         clearly  do a neater control flow than some other construct, 
  453.  
  454.         feel free to use it.  It should not be abused however, as it 
  455.  
  456.         is in the rest of the program on your monitor.
  457.  
  458.              Entire  books  are written on  "gotoless"  programming, 
  459.  
  460.         better known as Structured Programming.   These will be left 
  461.  
  462.         to  your  study.   One  point  of reference  is  the  Visual 
  463.  
  464.         Calculater described in Chapter 14 of this  tutorial.   This 
  465.  
  466.         program  is  contained in four separately compiled  programs 
  467.  
  468.         and  is a rather large complex program.   If you spend  some 
  469.  
  470.         time studying the source code,  you will find that there  is 
  471.  
  472.         not  a single "goto" statement anywhere in it.   Compile and 
  473.  
  474.         run  GOTOEX.C  and study its output.   It would  be  a  good 
  475.  
  476.         exercise  to rewrite it and see how much more readable it is 
  477.  
  478.         when the statements are listed in order.
  479.          
  480.                        FINALLY, A MEANINGFUL PROGRAM
  481.  
  482.              Load  the  file  named TEMPCONV.C for an example  of  a 
  483.  
  484.         useful,  even  though somewhat limited program.   This is  a 
  485.  
  486.         program  that generates a list of centigrade  and  farenheit 
  487.  
  488.         temperatures  and prints a message out at the freezing point 
  489.  
  490.         of water and another at the boiling point of water. 
  491.  
  492.              Of particular importance is the formatting.  The header 
  493.  
  494.         is  simply  several lines of comments  describing  what  the 
  495.  
  496.         program  does in a manner that catches the readers attention 
  497.  
  498.         and  is  still  pleasing to the  eye.  You  will  eventually 
  499.  
  500.         develop your own formatting style, but this is a good way to 
  501.  
  502.         start.   Also if you observe the for loop,  you will  notice 
  503.  
  504.  
  505.  
  506.                                  Page 16 
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.                         Chapter 3 - Program Control                        
  517.  
  518.  
  519.         that  all  of  the contents of the  compound  statement  are 
  520.  
  521.         indented  3 spaces to the right of the "for" reserved  word, 
  522.  
  523.         and  the  closing brace is lined up under the "f" in  "for".  
  524.  
  525.         This  makes debugging a bit easier because the  construction 
  526.  
  527.         becomes  very  obvious.   You  will  also  notice  that  the 
  528.  
  529.         "printf"  statements that are in the "if" statements  within 
  530.  
  531.         the  big  "for" loop are indented  three  additional  spaces 
  532.  
  533.         because they are part of another construct. 
  534.  
  535.              This  is  the first program in which we used more  than 
  536.  
  537.         one  variable.   The three variables are simply  defined  on 
  538.  
  539.         three  different lines and are used in the same manner as  a 
  540.  
  541.         single variable was used in previous programs.   By defining 
  542.  
  543.         them  on different lines,  we have an opportunity to  define 
  544.  
  545.         each with a comment.
  546.  
  547.                       ANOTHER POOR PROGRAMMING EXAMPLE
  548.  
  549.              Recalling  UGLYFORM.C from the last chapter,  you saw a 
  550.  
  551.         very  poorly  formatted program.   If you load  and  display 
  552.  
  553.         DUMBCONV.C you will have an example of poor formatting which 
  554.  
  555.         is  much closer to what you will actually find in  practice.  
  556.  
  557.         This  is  the same program as TEMPCONV.C with  the  comments 
  558.  
  559.         removed  and  the  variable  names  changed  to  remove  the 
  560.  
  561.         descriptive aspect of the names.  Although this program does 
  562.  
  563.         exactly the same as the last one,  it is much more difficult 
  564.  
  565.         to  read and understand.   You should begin to develop  good 
  566.  
  567.         programming practices now.
  568.  
  569.              Compile  and  run  this program to  see  that  it  does 
  570.  
  571.         exactly what the last one did.
  572.  
  573.  
  574.         PROGRAMMING EXERCISES
  575.  
  576.         1.  Write a program that writes your name on the monitor ten 
  577.  
  578.             times.   Write this program three times, once with  each 
  579.  
  580.             looping method.
  581.  
  582.         2.  Write a program that counts from one to ten, prints the 
  583.  
  584.             values  on  a separate line for each,  and  includes  a     
  585.  
  586.             message  of  your  choice when the count  is  3  and  a 
  587.  
  588.             different message when the count is 7.
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.                                  Page 17 
  600.  
  601.