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

  1.                  Chapter 4 - Assignment & Logical compares
  2.  
  3.  
  4.                        INTEGER ASSIGNMENT STATEMENTS
  5.  
  6.              Load the file INTASIGN.C and display it for an  example 
  7.  
  8.         of  assignment statements.   Three variables are defined for 
  9.  
  10.         use  in the program and the rest of the program is merely  a 
  11.  
  12.         series of illustrations of various assignments.   The  first 
  13.  
  14.         two  lines  of  the assignment statements  assign  numerical 
  15.  
  16.         values  to "a" and "b",  and the next four lines  illustrate 
  17.  
  18.         the  five  basic arithmetic functions and how to  use  them.  
  19.  
  20.         The  fifth is the modulo operator and gives the remainder if 
  21.  
  22.         the two variables were divided.   It can only be applied  to 
  23.  
  24.         "int"  or  "char"  type  variables,   and  of  course  "int" 
  25.  
  26.         extensions such as "long",  "short",  etc.  Following these, 
  27.  
  28.         there  are two lines illustrating how to combine some of the 
  29.  
  30.         variables  in  some complex math expressions.   All  of  the 
  31.  
  32.         above examples should require no comment except to say  that 
  33.  
  34.         none  of  the equations are meant to be particularly  useful 
  35.  
  36.         except as illustrations.
  37.  
  38.              The  next  two expressions are perfectly acceptable  as 
  39.  
  40.         given,  but we will see later in this chapter that there  is 
  41.  
  42.         another way to write these for more compact code.
  43.  
  44.              This leaves us with the last two lines which may appear 
  45.  
  46.         to  you  as being very strange.   The C compiler  scans  the 
  47.  
  48.         assignment  statement from right to left,  (which may seem a 
  49.  
  50.         bit odd since we do not read that way),  resulting in a very 
  51.  
  52.         useful construct,  namely the one given here.   The compiler 
  53.  
  54.         finds the value 20, assigns it to "c", then continues to the 
  55.  
  56.         left finding that the latest result of a calculation  should 
  57.  
  58.         be  assigned to "b".   Thinking that the latest  calculation 
  59.  
  60.         resulted in a 20,  it assigns it to "b" also,  and continues 
  61.  
  62.         the leftward scan assigning the value 20 to "a" also.   This 
  63.  
  64.         is a very useful construct when you are initializing a group 
  65.  
  66.         of  variables.   The  last statement illustrates that it  is 
  67.  
  68.         possible  to actually do some calculations to arrive at  the 
  69.  
  70.         value which will be assigned to all three variables.
  71.  
  72.              The  program has no output so compiling  and  executing 
  73.  
  74.         this  program  will be very uninteresting.  Since  you  have 
  75.  
  76.         already  learned  how to display some integer results  using 
  77.  
  78.         the "printf" function,  it would be to your advantage to add 
  79.  
  80.         some output statements to this program to see if the various 
  81.  
  82.         statements do what you think they should do.
  83.  
  84.              This would be a good time for a preliminary  definition 
  85.  
  86.         of  a  rule to be followed in C.   The data definitions  are 
  87.  
  88.         always given before any executable statements in any program 
  89.  
  90.         block.   This is why the variables are defined first in this 
  91.  
  92.         program  and in any C program.   If you try to define a  new 
  93.  
  94.  
  95.  
  96.  
  97.  
  98.                                   Page 18
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.                  Chapter 4 - Assignment & Logical compares
  109.  
  110.  
  111.         variable after executing some statements,  the compiler will 
  112.  
  113.         issue an error.
  114.  
  115.                            ADDITIONAL DATA TYPES
  116.  
  117.              Loading and editing MORTYPES.C will illustrate how some 
  118.  
  119.         additional  data  types can be used.   Once  again  we  have 
  120.  
  121.         defined  a  few integer type variables which you  should  be 
  122.  
  123.         fairly  familiar  with  by now,  but we have added  two  new 
  124.  
  125.         types, the "char", and the "float". 
  126.  
  127.              The  "char"  type  of data is nearly the  same  as  the 
  128.  
  129.         integer  except that it can only be assigned values  between 
  130.  
  131.         zero and 255, since it is stored in only one byte of memory.  
  132.  
  133.         The "char" type of data is usually used for ASCII data, more 
  134.  
  135.         commonly  known  as  text.   The text you  are  reading  was 
  136.  
  137.         originally written on a computer with a word processor  that 
  138.  
  139.         stored the words in the computer one character per byte.  In 
  140.  
  141.         contrast,  the  integer data type is stored in two bytes  of 
  142.  
  143.         computer memory on most microcomputers. 
  144.  
  145.                               DATA TYPE MIXING
  146.  
  147.              It  would be profitable at this time to discuss the way 
  148.  
  149.         C handles the two types "char" and "int".  Most functions in 
  150.  
  151.         C  that are designed to operate with integer type  variables 
  152.  
  153.         will work equally well with character type variables because 
  154.  
  155.         they  are a form of an integer variable.   Those  functions, 
  156.  
  157.         when called on to use a "char" type variable,  will actually 
  158.  
  159.         promote  the "char" data into integer data before using  it.  
  160.  
  161.         For this reason, it is possible to mix "char" and "int" type 
  162.  
  163.         variables in nearly any way you desire.   The compiler  will 
  164.  
  165.         not get confused,  but you might.  It is good not to rely on 
  166.  
  167.         this too much, but to carefully use only the proper types of 
  168.  
  169.         data where they should be used.
  170.  
  171.               The  second new data type is the "float" type of data, 
  172.  
  173.         commonly  called floating point data.   This is a data  type 
  174.  
  175.         which  usually  has a very large range,  a large  number  of 
  176.  
  177.         significant digits, and a large number of computer words are 
  178.  
  179.         required to store it.   The "float" data type has a  decimal 
  180.  
  181.         point  associated  with it and,  on most computers,  has  an 
  182.  
  183.         allowable range of from 10E-38 to 10E+38.  Not all compilers 
  184.  
  185.         have  the  same available range,  so  check  your  reference 
  186.  
  187.         manual for the limits  on your compiler.
  188.  
  189.                        HOW TO USE THE NEW DATA TYPES
  190.  
  191.              The  first three lines of the program assign values  to 
  192.  
  193.         all nine of the defined variables so we can manipulate  some 
  194.  
  195.         of the data between the different types.
  196.  
  197.  
  198.  
  199.                                   Page 19
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.                  Chapter 4 - Assignment & Logical compares
  210.  
  211.  
  212.  
  213.              Since,  as  mentioned above,  a "char" data type is  in 
  214.  
  215.         reality  an "integer" data type,  no special  considerations 
  216.  
  217.         need be taken to promote a "char" to an "int",  and a "char" 
  218.  
  219.         type data field can be assigned to an "int" variable.   When 
  220.  
  221.         going the other way, there is no standard, so you may simply 
  222.  
  223.         get garbage if the value of the integer variable is  outside 
  224.  
  225.         the  range of the "char" type variable.   It will  translate 
  226.  
  227.         correctly  if the value is within the range of zero to  255. 
  228.  
  229.         In  the second line therefore,  when attempting to set x  (a 
  230.  
  231.         char) to -27,  you may or may not get a well defined answer, 
  232.  
  233.         it depends on your particular implementation of C.
  234.  
  235.              The   third   line   illustrates  the   simplicity   of 
  236.  
  237.         translating an integer into a "float",  simply assign it the 
  238.  
  239.         new  value  and the system will do  the  proper  conversion.  
  240.  
  241.         When  going  the  other  way  however,  there  is  an  added 
  242.  
  243.         complication.   Since  there may be a fractional part of the 
  244.  
  245.         floating  point number,  the system must decide what  to  do 
  246.  
  247.         with it.  By definitions , it will truncate it.
  248.  
  249.              This program produces no output, and we haven't covered 
  250.  
  251.         a way to print out "char" and "float" type variables, so you 
  252.  
  253.         can't  really  get  in  to this program and  play  with  the 
  254.  
  255.         results, but the next program will cover this for you.
  256.  
  257.                           LOTS OF VARIABLE TYPES
  258.  
  259.              Load the file LOTTYPES.C and display it on your screen.  
  260.  
  261.         This file contains every standard simple data type available 
  262.  
  263.         in the programming language C.   There are other types,  but 
  264.  
  265.         they are the compound types that we will cover in due time.
  266.  
  267.              Observe  the  file.   First we define a  simple  "int", 
  268.  
  269.         followed  by a "long int" and a "short int".   Consult  your 
  270.  
  271.         reference  manual for an exact definition of these for  your  
  272.  
  273.         compiler,    because   they   are   not   consistent    from 
  274.  
  275.         implementation  to implementation.   The "unsigned" is  next 
  276.  
  277.         and  is  defined as the same size as the "int" but  with  no 
  278.  
  279.         sign.   The "unsigned" then will cover a range of 0 to 65535 
  280.  
  281.         on most microcomputers.   It should be pointed out that when 
  282.  
  283.         the "long",  "short", or "unsigned" is desired, the "int" is 
  284.  
  285.         optional  and  is left out by most experienced  programmers.  
  286.  
  287.         We  have already covered the "char" and the  "float",  which 
  288.  
  289.         leaves  only the "double".   The "double" usually  covers  a 
  290.  
  291.         greater  range  than  the "float" and has  more  significant 
  292.  
  293.         digits for more precise calculations.  It also requires more 
  294.  
  295.         memory  to store a value than the  simple  "float".  Consult 
  296.  
  297.         your  reference  manual  for the range and accuracy  of  the 
  298.  
  299.         "double".
  300.  
  301.  
  302.  
  303.  
  304.                                   Page 20
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.                  Chapter 4 - Assignment & Logical compares
  315.  
  316.  
  317.              Another  diversion  is in order at  this  point.   Most 
  318.  
  319.         compilers  have no provisions for floating point  math,  but 
  320.  
  321.         only  double  floating  point math.   They  will  promote  a 
  322.  
  323.         "float"   to  a  "double"  before  doing  calculations   and 
  324.  
  325.         therefore only one math library will be needed.   Of course, 
  326.  
  327.         this  is totally transparent to you,  so you don't  need  to 
  328.  
  329.         worry  about  it.   You may think that it would be  best  to 
  330.  
  331.         simply define every floating point variable as double, since 
  332.  
  333.         they  are promoted before use in any calculations,  but that 
  334.  
  335.         may not be a good idea.  A "float" variable requires 4 bytes 
  336.  
  337.         of storage and a "double" requires 8 bytes of storage, so if 
  338.  
  339.         you have a large volume of floating point data to store, the 
  340.  
  341.         "double"  will  obviously require much  more  memory.   Your 
  342.  
  343.         compiler  may require a different number of bytes than 4  or 
  344.  
  345.         8.   Consult your reference manual for the correct number of 
  346.  
  347.         bytes used by your compiler.
  348.          
  349.              After  defining the data types,  a numerical  value  is 
  350.  
  351.         assigned  to  each  of  the defined variables  in  order  to 
  352.  
  353.         demonstrate the means of outputting each to the monitor. 
  354.  
  355.                             THE CONVERSION CHARACTERS
  356.  
  357.              Following  is a list of the conversion  characters  and 
  358.  
  359.         the way they are usedin the "printf" statement.
  360.  
  361.              d    decimal notation
  362.              o    octal notation
  363.              x    hexadecimal notation
  364.              u    unsigned notation
  365.              c    character notation
  366.              s    string notation
  367.              f    floating point notation
  368.  
  369.              Each  of  these  is used following a  percent  sign  to 
  370.  
  371.         indicate  the type of output conversion,  and between  those 
  372.  
  373.         two characters, the following  fields may be added.
  374.  
  375.              -    left justification in its field
  376.              (n)  a number specifying minimum field width
  377.              .    to separate n from m
  378.              (m)  significant fractional digits for a float
  379.              l    to indicate a "long"
  380.  
  381.              These  are all used in the examples which are  included 
  382.  
  383.         in the program presently displayed on your monitor, with the 
  384.  
  385.         exception of the string notation which will be covered later 
  386.  
  387.         in this tutorial.   Compile and run this program to see what 
  388.  
  389.         effect the various fields have on the output.
  390.  
  391.  
  392.  
  393.  
  394.  
  395.                                   Page 21
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.                  Chapter 4 - Assignment & Logical compares
  406.  
  407.  
  408.              You  now  have the ability to display any of  the  data 
  409.  
  410.         fields  in  the  previous programs and it would be  to  your 
  411.  
  412.         advantage  to go back and see if you can display any of  the 
  413.  
  414.         fields anyway you desire.
  415.  
  416.                               LOGICAL COMPARES
  417.  
  418.              Load  and  view  the file  named  COMPARES.C  for  many 
  419.  
  420.         examples  of compare statements in C.   We begin by defining 
  421.  
  422.         and  initializing  nine variables to use  in  the  following 
  423.  
  424.         compare  statements.   This initialization is new to you and 
  425.  
  426.         can be used to initialize variables while they are defined.
  427.  
  428.              The  first  group of compare statements represents  the 
  429.  
  430.         simplest  kinds  of compares since they simply  compare  two 
  431.  
  432.         variables.    Either  variable  could  be  replaced  with  a 
  433.  
  434.         constant and still be a valid compare,  but two variables is 
  435.  
  436.         the general case.  The first compare checks to see if "x" is 
  437.  
  438.         equal  to  "y"  and it uses the double equal  sign  for  the 
  439.  
  440.         comparison.   A single equal sign could be used here but  it 
  441.  
  442.         would have a different meaning as we will see shortly.   The 
  443.  
  444.         second  comparison checks to see if "x" is greater than "z".  
  445.  
  446.  
  447.              The   third   introduces  the   "NOT"   operator,   the 
  448.  
  449.         exclamation,  which can be used to invert the result of  any 
  450.  
  451.         logical  compare.   The  fourth checks for "b" less than  or 
  452.  
  453.         equal to "c",  and the last checks for "r" not equal to "s".  
  454.  
  455.         As  we  learned in the last chapter,  if the result  of  the 
  456.  
  457.         compare  is true,  the statement following the  "if"  clause 
  458.  
  459.         will  be executed and the results are given in the comments.  
  460.  
  461.         Note  that  "less than" and "greater than or equal  to"  are 
  462.  
  463.         also available, but are not illustrated here.
  464.  
  465.              It  would be well to mention the different format  used 
  466.  
  467.         for the "if" statement in this example program.   A carriage 
  468.  
  469.         return  is  not  required as a statement  separator  and  by 
  470.  
  471.         putting the conditional clause on the same line as the "if", 
  472.  
  473.         it adds to the readability of the overall program.
  474.  
  475.                                MORE COMPARES
  476.  
  477.              The  compares  in  the  second group  are  a  bit  more 
  478.  
  479.         involved.  Starting with the first compare, we find a rather 
  480.  
  481.         strange  looking set of conditions in the  parentheses.   To 
  482.  
  483.         understand  this  we must understand just what a  "true"  or 
  484.  
  485.         "false"  is in the C language.   A "false" is defined  as  a 
  486.  
  487.         value  of zero,  and "true" is defined as a non-zero  value.  
  488.  
  489.         Any  integer  or char type of variable can be used  for  the 
  490.  
  491.         result of a true/false test, or the result can be an implied 
  492.  
  493.         integer or char.
  494.  
  495.  
  496.  
  497.  
  498.                                   Page 22
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.                  Chapter 4 - Assignment & Logical compares
  509.  
  510.  
  511.              Look  at  the  first  compare of the  second  group  of 
  512.  
  513.         compare statements.   The expression "r != s" will  evaluate 
  514.  
  515.         as  a "true" since "r" was set to 0.0 above,  so the  result 
  516.  
  517.         will be a non-zero value,  probably 1.   Even though the two 
  518.  
  519.         variables  that  are  compared are  "float"  variables,  the 
  520.  
  521.         result  will  be of type "integer".   There is  no  explicit 
  522.  
  523.         variable  to which it will be assigned so the result of  the 
  524.  
  525.         compare  is  an  implied  integer.   Finally  the  resulting 
  526.  
  527.         number,  1 in this case, is assigned to the integer variable 
  528.  
  529.         "x".   If double equal signs were used,  the phantom  value, 
  530.  
  531.         namely 1,  would be compared to the value of "x",  but since 
  532.  
  533.         the  single  equal  sign  is used,  the value  1  is  simply 
  534.  
  535.         assigned  to  "x",  as  though the  statement  were  not  in 
  536.  
  537.         parentheses.  Finally, since the result of the assignment in 
  538.  
  539.         the  parentheses  was non-zero,  the  entire  expression  is 
  540.  
  541.         evaluated as "true",  and "z" is assigned the value of 1000.  
  542.  
  543.         Thus  we  accomplished  two things  in  this  statement,  we 
  544.  
  545.         assigned  "x" a new value,  probably 1,  and we assigned "z" 
  546.  
  547.         the  value of 1000.   We covered a lot in this statement  so 
  548.  
  549.         you  may wish to review it before going on.   The  important 
  550.  
  551.         things  to  remember are the values that define  "true"  and 
  552.  
  553.         "false", and the fact that several things can be assigned in 
  554.  
  555.         a  conditional  statement.   The value assigned to  "x"  was 
  556.  
  557.         probably a 1 but different compilers may assign a  different 
  558.  
  559.         value as long as it is non-zero. 
  560.  
  561.              The next example should help clear up some of the above 
  562.  
  563.         in your mind.  In this example, "x" is assigned the value of 
  564.  
  565.         "y",  and since the result is 11, the condition is non-zero, 
  566.  
  567.         which is true,   and the variable "z" is therefore  assigned 
  568.  
  569.         222.
  570.  
  571.              The third example, in the second group, compares "x" to 
  572.  
  573.         zero.   If  the result is true,  meaning that if "x" is  not 
  574.  
  575.         zero,  then "z" is assigned the value of 333,  which it will 
  576.  
  577.         be.   The  last  example in this group illustrates the  same 
  578.  
  579.         concept,  since the result will be true if "x" is  non-zero.  
  580.  
  581.         The compare to zero is not actually needed and the result of 
  582.  
  583.         the compare is true.   The third and fourth examples of this 
  584.  
  585.         group are therefore identical.
  586.  
  587.                         ADDITIONAL COMPARE CONCEPTS
  588.  
  589.              The   third  group  of  compares  will  introduce  some 
  590.  
  591.         additional  concepts,  namely  the  logical  "AND"  and  the 
  592.  
  593.         logical  "OR".   We  assign  the value of 77  to  the  three 
  594.  
  595.         integer  variables  simply to get started  again  with  some 
  596.  
  597.         defined  values.   The  first  compare of  the  third  group 
  598.  
  599.         contains  the new control "&&",  which is the logical "AND".  
  600.  
  601.         The  entire statement reads,  if "x" equals "y" AND  if  "x" 
  602.  
  603.  
  604.  
  605.  
  606.  
  607.                                   Page 23
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.                  Chapter 4 - Assignment & Logical compares
  618.  
  619.  
  620.         equals  77 then the result is "true".   Since this is  true, 
  621.  
  622.         the variable z is set equal to 33. 
  623.  
  624.              The  next  compare  in this group introduces  the  "||" 
  625.  
  626.         operator which is the "OR".   The statement reads, if "x" is 
  627.  
  628.         greater  than  "y"  OR if "z" is greater than  12  then  the 
  629.  
  630.         result is true.   Since "z" is greater than 12,  it  doesn't 
  631.  
  632.         matter  if "x" is greater than "y" or not,  because only one 
  633.  
  634.         of  the  two conditions must be true for the  result  to  be 
  635.  
  636.         true.  The result is true, so therefore "z" will be assigned 
  637.  
  638.         the value of 22.
  639.  
  640.                              LOGICAL EVALUATION
  641.  
  642.              When a compound expression is evaluated, the evaluation 
  643.  
  644.         proceeds from left to right and as soon as the result of the 
  645.  
  646.         outcome is assured,  evaluation stops.   Namely, in the case 
  647.  
  648.         of  an "AND" evaluation,  when one of the terms evaluates to 
  649.  
  650.         "false",  evaluation is discontinued because additional true 
  651.  
  652.         terms  cannot make the result ever become  "true".   In  the 
  653.  
  654.         case of an "OR" evaluation,  if any of the terms is found to 
  655.  
  656.         be  "true",  evaluation stops because it will be  impossible 
  657.  
  658.         for additional terms to cause the result to be "false".   In 
  659.  
  660.         the case of additionally nested terms,  the above rules will 
  661.  
  662.         be applied to each of the nested levels.
  663.  
  664.                           PRECEDENCE OF OPERATORS
  665.  
  666.              The  question will come up concerning the precedence of 
  667.  
  668.         operators.   Which  operators are evaluated first and  which 
  669.  
  670.         last?   There  are many rules about this topic,  which  your 
  671.  
  672.         compiler  will define completely,  but I would suggest  that 
  673.  
  674.         you don't worry about it at this point.   Instead,  use lots 
  675.  
  676.         of parentheses to group variables,  constants, and operators 
  677.  
  678.         in  a way meaningful to you.   Parentheses always  have  the 
  679.  
  680.         highest  priority  and  will remove any  question  of  which 
  681.  
  682.         operations will be done first in any particular statements.
  683.  
  684.              Going  on to the next example in group three,  we  find 
  685.  
  686.         three  simple variables used in the conditional part of  the 
  687.  
  688.         compare.   Since  all  three  are non-zero,  all  three  are 
  689.  
  690.         "true",  and therefore the "AND" of the three variables  are 
  691.  
  692.         true,  leading  to  the result being "true",  and "z"  being 
  693.  
  694.         assigned  the value of 11.   Note that since the  variables, 
  695.  
  696.         "r", "s", and "t" are "float" type variables, they could not 
  697.  
  698.         be  used this way,  but they could each be compared to  zero 
  699.  
  700.         and the same type of expression could be used. 
  701.  
  702.              Continuing on to the fourth example of the third  group 
  703.  
  704.         we  find three assignment statements in the compare part  of 
  705.  
  706.         the "if" statement.  If you understood the above discussion, 
  707.  
  708.  
  709.  
  710.                                   Page 24
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.                  Chapter 4 - Assignment & Logical compares
  721.  
  722.  
  723.         you  should have no difficulty understanding that the  three 
  724.  
  725.         variables are assigned their respective new values,  and the 
  726.  
  727.         result  of  all three are non-zero,  leading to a  resulting 
  728.  
  729.         value of "TRUE". 
  730.  
  731.                         THIS IS A TRICK, BE CAREFUL
  732.  
  733.              The last example of the third group contains a bit of a 
  734.  
  735.         trick, but since we have covered it above, it is nothing new 
  736.  
  737.         to you.  Notice that the first part of the compare evaluates 
  738.  
  739.         to  "FALSE".   The  remaining parts of the compare  are  not 
  740.  
  741.         evaluated,  because it is an "AND" and it will definitely be 
  742.  
  743.         resolved as a "FALSE" because the first term is  false.   If 
  744.  
  745.         the program was dependent on the value of "y" being set to 3 
  746.  
  747.         in  the  next  part of the compare,  it  will  fail  because 
  748.  
  749.         evaluation  will  cease following the "FALSE" found  in  the 
  750.  
  751.         first  term.   Likewise,  "z" will not be set to 4,  and the 
  752.  
  753.         variable "r" will not be changed. 
  754.  
  755.                           POTENTIAL PROBLEM AREAS
  756.  
  757.              The   last   group   of   compares   illustrate   three 
  758.  
  759.         possibilities for getting into a bit of trouble.   All three 
  760.  
  761.         have  the  common  result that "z" will not get set  to  the 
  762.  
  763.         desired value,  but for different reasons.   In the case  of 
  764.  
  765.         the  first  one,  the compare evaluates as "true",  but  the 
  766.  
  767.         semicolon  following the second parentheses  terminates  the 
  768.  
  769.         "if"  clause,  and the assignment statement involving "z" is 
  770.  
  771.         always executed as the next statement.   The "if"  therefore 
  772.  
  773.         has  no  effect  because of the  misplaced  semicolon.   The 
  774.  
  775.         second  statement is much more straightforward  because  "x" 
  776.  
  777.         will  always  be equal to itself,  therefore the  inequality 
  778.  
  779.         will never be true, and the entire statement will never do a 
  780.  
  781.         thing, but is wasted effort.  The last statement will always 
  782.  
  783.         assign  0  to "x" and the compare will therefore  always  be 
  784.  
  785.         "false",  never  executing the conditional part of the  "if" 
  786.  
  787.         statement. 
  788.  
  789.              The  conditional  statement is extremely important  and 
  790.  
  791.         must be thoroughly understood to write efficient C programs.  
  792.  
  793.         If  any  part of this discussion is unclear  in  your  mind, 
  794.  
  795.         restudy  it  until you are confident that you understand  it 
  796.  
  797.         thoroughly before proceeding onward.
  798.  
  799.                            THE CRYPTIC PART OF C
  800.  
  801.              There are three constructs used in C that make no sense 
  802.  
  803.         at   all  when  first  encountered  because  they  are   not 
  804.  
  805.         intuitive,  but they greatly increase the efficiency of  the 
  806.  
  807.         compiled  code  and  are used extensively by  experienced  C 
  808.  
  809.         programmers.   You  should therefore be exposed to them  and 
  810.  
  811.  
  812.  
  813.                                   Page 25
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.                  Chapter 4 - Assignment & Logical compares
  824.  
  825.  
  826.         learn to use them because they will appear in most,  if  not 
  827.  
  828.         all,  of the programs you see in the publications.  Load and 
  829.  
  830.         examine  the file named CRYPTIC.C for examples of the  three 
  831.  
  832.         new constructs.
  833.  
  834.              In  this  program,   some  variables  are  defined  and 
  835.  
  836.         initialized in the same statements for use below.  The first 
  837.  
  838.         executable statement simply adds 1 to the value of "x",  and 
  839.  
  840.         should come as no surprise to you.   The next two statements 
  841.  
  842.         also  add one to the value of "x",  but it is not  intuitive 
  843.  
  844.         that this is what happens.   It is simply by definition that 
  845.  
  846.         this is true.  Therefore, by definition of the C language, a 
  847.  
  848.         double   plus  sign  either  before  or  after  a   variable 
  849.  
  850.         increments  that variable by 1.   Additionally,  if the plus 
  851.  
  852.         signs are before the variable,  the variable is  incremented 
  853.  
  854.         before  it  is used,  and if the plus signs  are  after  the 
  855.  
  856.         variable,  the variable is used,  then incremented.   In the 
  857.  
  858.         next statement, the value of "y" is assigned to the variable 
  859.  
  860.         "z",  then  "y"  is incremented because the plus  signs  are 
  861.  
  862.         after  the  variable  "y".   In the last  statement  of  the 
  863.  
  864.         incrementing  group of example statements,  the value of "y" 
  865.  
  866.         is  incremented then its value is assigned to  the  variable 
  867.  
  868.         "z". 
  869.  
  870.              The  next group of statements illustrate decrementing a 
  871.  
  872.         variable by one.   The definition works exactly the same way 
  873.  
  874.         for decrementing as it does for incrementing.   If the minus 
  875.  
  876.         signs are before the variable,  the variable is decremented, 
  877.  
  878.         then  used,  and if the minus signs are after the  variable, 
  879.  
  880.         the variable is used, then decremented.
  881.  
  882.                       THE CRYPTIC ARITHMETIC OPERATOR
  883.  
  884.              Another  useful but cryptic operator is the  arithmetic 
  885.  
  886.         operator.   This operator is used to modify any variable  by 
  887.  
  888.         some constant value.  The first statement of the "arithmetic 
  889.  
  890.         operator" group of statements simply adds 12 to the value of 
  891.  
  892.         the variable "a".   The second statement does the same,  but 
  893.  
  894.         once again, it is not intuitive that they are the same.  Any 
  895.  
  896.         of the four basic functions of arithmetic, "+", "-", "*", or 
  897.  
  898.         "/",  can  be handled in this way,  by putting the  function 
  899.  
  900.         desired  in  front  of the equal sign  and  eliminating  the 
  901.  
  902.         second  reference to the variable name.   It should be noted 
  903.  
  904.         that  the  expression on the right side  of  the  arithmetic 
  905.  
  906.         operator can be any valid expression,  the examples are kept 
  907.  
  908.         simple for your introduction to this new operator. 
  909.  
  910.              Just  like the incrementing and decrementing operators, 
  911.  
  912.         the arithmetic operator is used extensively by experienced C 
  913.  
  914.         programmers and it would pay you well to understand it.
  915.  
  916.  
  917.  
  918.  
  919.                                   Page 26
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.  
  929.                  Chapter 4 - Assignment & Logical compares
  930.  
  931.  
  932.                          THE CONDITIONAL EXPRESSION
  933.  
  934.              The  conditional expression is just as cryptic  as  the 
  935.  
  936.         last  two,  but once again it can be very useful so it would 
  937.  
  938.         pay you to understand it.   It consists of three expressions 
  939.  
  940.         within parentheses separated by a question mark and a colon.  
  941.  
  942.         The  expression prior to the question mark is  evaluated  to 
  943.  
  944.         determine  if it is "true" or "false".   If it is true,  the 
  945.  
  946.         expression  between  the  question mark  and  the  colon  is 
  947.  
  948.         evaluated,  and if it is not true,  the expression following 
  949.  
  950.         the  colon  is evaluated.   The result of the evaluation  is 
  951.  
  952.         used for the assignment.   The final result is identical  to 
  953.  
  954.         that  of an "if" statement with an "else" clause.   This  is 
  955.  
  956.         illustrated  by  the  second example  in  this  group.   The 
  957.  
  958.         conditional  expression  has  the added  advantage  of  more 
  959.  
  960.         compact code that will compile to fewer machine instructions 
  961.  
  962.         in the final program.
  963.  
  964.              The  final two lines of this example program are  given 
  965.  
  966.         to  illustrate  a very compact way to assign the greater  of 
  967.  
  968.         two variables "a" or "b" to "c", and to assign the lessor of 
  969.  
  970.         the  same two variables to "c".   Notice how  efficient  the 
  971.  
  972.         code is in these two examples.
  973.  
  974.                      TO BE CRYPTIC OR NOT TO BE CRYPTIC
  975.  
  976.              Several students of C have stated that they didn't like 
  977.  
  978.         these  three  cryptic constructs and that they would  simply 
  979.  
  980.         never  use them.   This would be fine if they never have  to 
  981.  
  982.         read  anybody  else's program,  or use  any  other  programs 
  983.  
  984.         within their own.  I have found many functions that I wished 
  985.  
  986.         to  use within a program but needed a small modification  to 
  987.  
  988.         use  it,  requiring me to understand another person's  code.  
  989.  
  990.         It  would therefore be to your advantage to learn these  new 
  991.  
  992.         constructs, and use them. They will be used in the remainder 
  993.  
  994.         of this tutorial, so you will be constantly exposed to them.
  995.  
  996.              This has been a long chapter but it contained important 
  997.  
  998.         material  to  get  you  started in using  C.   In  the  next 
  999.  
  1000.         chapter,  we  will go on to the building blocks  of  C,  the 
  1001.  
  1002.         functions.  At that point, you will have enough of the basic 
  1003.  
  1004.         materials to allow you to begin writing meaningful programs.
  1005.  
  1006.  
  1007.         PROGRAMMING EXERCISES
  1008.  
  1009.         1.   Write  a program that will count from 1 to 12 and print 
  1010.  
  1011.              the count, and its square, for each count.
  1012.                   1    1
  1013.                   2    4
  1014.                   3    9   etc.
  1015.  
  1016.  
  1017.  
  1018.                                   Page 27
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.                  Chapter 4 - Assignment & Logical compares
  1029.  
  1030.  
  1031.  
  1032.         2.   Write a program that counts from 1 to 12 and prints the 
  1033.  
  1034.              count  and  its  inversion  to  5  decimal  places  for 
  1035.  
  1036.              each count. This will require a floating point number.
  1037.                   1    1.00000
  1038.                   2     .50000
  1039.                   3     .33333
  1040.                   4     .25000
  1041.                   etc.
  1042.  
  1043.         3.   Write a program that will count from 1 to 100 and print 
  1044.  
  1045.              only those values between 32 and 39, one to a line.
  1046.           
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.  
  1069.  
  1070.  
  1071.  
  1072.  
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078.  
  1079.  
  1080.  
  1081.  
  1082.  
  1083.  
  1084.  
  1085.  
  1086.  
  1087.                                   Page 28
  1088.  
  1089.