home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / ctutor1.zip / CHAP04.TXT < prev    next >
Text File  |  1989-11-10  |  32KB  |  764 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                     Chapter 4
  6.                                 ASSIGNMENT & LOGICAL COMPARES
  7.  
  8. Throughout this chapter, references are given to various
  9. ranges of variables.  Your compiler may use a different range
  10. for some of the variables since the ANSI standard does not
  11. define specific limits for all data types.  Consult the
  12. documentation for your compiler for the exact range for each
  13. of the variable types.
  14.  
  15.  
  16. INTEGER ASSIGNMENT STATEMENTS
  17. ____________________________________________________________
  18.  
  19. Load the file INTASIGN.C and display it for    ==============
  20. an example of assignment statements.  Three      INTASIGN.C
  21. variables are defined for use in the program   ==============
  22. and the rest of the program is merely a
  23. series of illustrations of various
  24. assignments.  The first two lines of the assignment statements
  25. assign numerical values to the variables named a and b, and
  26. the next five lines illustrate the five basic arithmetic
  27. functions and how to use them.  The fifth is the modulo
  28. operator and gives the remainder if the two variables were
  29. divided.  It can only be applied to int or char type
  30. variables.  Following these, there are two lines illustrating
  31. how to combine some of the variables in some complex math
  32. expressions.  All of the above examples should require no
  33. comment except to say that none of the equations are meant to
  34. be particularly useful except as illustrations.  The char type
  35. variable will be defined in the description of the next
  36. example program.
  37.  
  38. The expressions in lines 17 and 18 are perfectly acceptable
  39. as given, but we will see later in this chapter that there is
  40. another way to write these for more compact code.
  41.  
  42.  
  43. VERY STRANGE LOOKING CODE
  44. ____________________________________________________________
  45.  
  46. This leaves us with the last two lines which may appear to you
  47. as being very strange.  The C compiler scans the assignment
  48. statement from right to left, (which may seem a bit odd since
  49. we do not read that way), resulting in a very useful
  50. construct, namely the one given here.  The compiler finds the
  51. value 20, assigns it to c, then continues to the left finding
  52. that the latest result of a calculation should be assigned to
  53. b.  Thinking that the latest calculation resulted in a 20, it
  54. assigns it to b also, and continues the leftward scan
  55. assigning the value 20 to a also.  This is a very useful
  56. construct when you are initializing a group of variables.  The
  57. last statement illustrates that it is possible to actually do
  58.  
  59.                                                      Page 4-1
  60.  
  61.                  Chapter 4 - Assignments and Logical Compares
  62.  
  63. some calculations to arrive at the value which will be
  64. assigned to all three variables.  In fact, the rightmost
  65. expression can contain variables, even a, b, & c.
  66.  
  67. The program has no output, so compiling and executing this
  68. program will be very uninteresting. Since you have already
  69. learned how to display some integer results using the printf()
  70. function, it would be to your advantage to add some output
  71. statements to this program to see if the various statements
  72. do what you think they should do.
  73.  
  74.  
  75. DEFINITIONS FIRST THEN EXECUTABLE STATEMENTS
  76. ____________________________________________________________
  77.  
  78. This would be a good time for a preliminary definition of a
  79. rule to be followed in C.  The variable definitions are always
  80. given before any executable statements in any program block. 
  81. This is why the variables are defined first in this program
  82. and in every C program.  If you try to define a new variable
  83. after executing some statements, your compiler will issue an
  84. error.
  85.  
  86.  
  87. ADDITIONAL DATA TYPES
  88. ____________________________________________________________
  89.  
  90. Loading and editing MORTYPES.C will           ===============
  91. illustrate how some additional data types can   MORETYPES.C
  92. be used.  Once again we have defined a few    ===============
  93. integer type variables which you should be
  94. fairly familiar with by now, but we have
  95. added two new types, the char, and the float. 
  96.  
  97. The char type of data is nearly the same as the integer except
  98. that it can only be assigned numerical values between -128 to
  99. 127 on most implementations of C, since it is stored in only
  100. one byte of memory.  The char type of data is usually used for
  101. ASCII data, more commonly known as text.  The text you are
  102. reading was originally written on a computer with a word
  103. processor that stored the words in the computer one character
  104. per byte.  In contrast, the integer data type is stored in two
  105. bytes of computer memory on nearly all microcomputers. 
  106.  
  107.  
  108. DATA TYPE MIXING
  109. ____________________________________________________________
  110.  
  111. It would be profitable at this time to discuss the way C
  112. handles the two types char and int.  Most functions in C that
  113. are designed to operate with integer type variables will work
  114. equally well with character type variables because they are
  115. a form of an integer variable.  Those functions, when called
  116. on to use a char type variable, will actually promote the char
  117.  
  118.                                                      Page 4-2
  119.  
  120.                  Chapter 4 - Assignments and Logical Compares
  121.  
  122. data into integer data before using it.  For this reason, it
  123. is possible to mix char and int type variables in nearly any
  124. way you desire.  The compiler will not get confused, but you
  125. might.  It is good not to rely on this too much, but to
  126. carefully use only the proper types of data where they should
  127. be used.
  128.  
  129. The second new data type is the float type of data, commonly
  130. called floating point data.  This is a data type which usually
  131. has a very large range, a large number of significant digits,
  132. and a large number of computer words are required to store it. 
  133. The float data type has a decimal point associated with it and
  134. has an allowable range of from 3.4E-38 to 3.4E+38 when using
  135. most C compilers on microcomputers, and is composed of about
  136. 7 significant digits.
  137.  
  138.  
  139.  
  140. HOW TO USE THE NEW DATA TYPES
  141. ____________________________________________________________
  142.  
  143. The first three lines of the program assign values to all nine
  144. of the defined variables so we can manipulate some of the data
  145. between the different types.
  146.  
  147. Since, as mentioned above, a char data type is in reality an
  148. integer data type, no special considerations need be taken to
  149. promote a char to an int, and a char type data field can be
  150. assigned to an int variable.  When going the other way, there
  151. is no standard, so you may simply get garbage if the value of
  152. the integer variable is outside the range of the char type
  153. variable.  Most C compilers simply truncate the most
  154. significant bits and use the 8 least significant bits.  It
  155. will translate correctly if the value is within the range of
  156. -128 to 127.
  157.  
  158. The third line illustrates the simplicity of translating an
  159. integer into a float, simply assign it the new value and the
  160. system will do the proper conversion.  When going the other
  161. way however, there is an added complication.  Since there may
  162. be a fractional part of the floating point number, the system
  163. must decide what to do with it.  By definition, it will
  164. truncate it.
  165.  
  166. This program produces no output, and we haven't covered a way
  167. to print out char and float type variables, so you can't
  168. really get in to this program and play with the results, but
  169. the next program will cover this for you.
  170.  
  171. Be sure to compile and run this program after you are sure you
  172. understand it completely.
  173.  
  174.  
  175.  
  176.  
  177.                                                      Page 4-3
  178.  
  179.                  Chapter 4 - Assignments and Logical Compares
  180.  
  181.  
  182. LOTS OF VARIABLE TYPES
  183. ____________________________________________________________
  184.  
  185. Load the file LOTTYPES.C and display it on     ==============
  186. your screen.  This file contains nearly every    LOTTYPES.C
  187. standard simple data type available in the     ==============
  188. programming language C.  There are other
  189. types, but they are the compound types (ie -
  190. arrays and structures) that we will cover in due time.
  191.  
  192. Observe the file.  First we define a simple int, followed by
  193. a long int which has a range of -2147483648 to 2147483647 with
  194. most C compilers, and a short int which has a range that is
  195. identical to that for the int variable, namely -32768 to
  196. 32767.  The unsigned is next and is defined as the same size
  197. as the int but with no sign.  The unsigned then will cover a
  198. range of 0 to 65535.  It should be pointed out that when the
  199. long, short, or unsigned is desired, the int is optional and
  200. is left out by most experienced programmers.  We have already
  201. covered the char and the float, which leaves only the double.
  202.  
  203. The double is a floating point number but covers a greater
  204. range than the float and has more significant digits for more
  205. precise calculations.  It also requires more memory to store
  206. a value than the simple float.  The double in most C compilers
  207. covers a range of 1.7E-308 to 1.7E+308.
  208.  
  209. Note that other compounding of types can be done such as long
  210. unsigned int, unsigned char, etc.  Check your documentation
  211. for a complete list of variable types.
  212.  
  213. Another diversion is in order at this point.  Your compiler
  214. may have no provision for floating point math, only double
  215. floating point math.  It will promote a float to a double
  216. before doing calculations and therefore only one math library
  217. will be needed.  Of course, this is transparent to you, so you
  218. don't need to worry about it.  Because of this, you may think
  219. that it would be best to simply define every floating point
  220. variable as double, since they are promoted before use in any
  221. calculations, but that may not be a good idea.  A float
  222. variable requires 4 bytes of storage and a double requires 8
  223. bytes of storage, so if you have a large volume of floating
  224. point data to store, the double will obviously require much
  225. more memory.
  226.  
  227. After defining the data types in the program under
  228. consideration, a numerical value is assigned to each of the
  229. defined variables in order to demonstrate the means of
  230. outputting each to the monitor. 
  231.  
  232.  
  233.  
  234.  
  235.  
  236.                                                      Page 4-4
  237.  
  238.                  Chapter 4 - Assignments and Logical Compares
  239.  
  240. SOME LATE ADDITIONS
  241. ____________________________________________________________
  242.  
  243. As any programming language evolves, additional constructs are
  244. added to fill some previously overlooked need.  Two new
  245. keywords have been added to C recently, and since they may not
  246. be a part of your compiler, they are not illustrated in
  247. example programs, but they will be discussed here.  The two
  248. new keywords are const and volatile and are used to tell the
  249. compiler that variables of these types will need special
  250. consideration.  A constant is declared with the const keyword
  251. and declares a value that will never be changed by either the
  252. program or the system itself.  If volatile is used, it
  253. declares a value that may be changed by the program but it may
  254. also be changed by some outside influence such as a clock
  255. update pulse incrementing the stored value.  Since constants
  256. can never have a value assigned to them in the executable part
  257. of the program, they should always be initialized.
  258.  
  259. Examples of use in declaring constants of these two types are
  260. given as;
  261.  
  262.       const int index1 = 2;
  263.       const index2 = 6;
  264.       volatile const int index3 = 12;
  265.       volatile index4;
  266.  
  267. As mentioned earlier in this chapter, the keyword int is
  268. optional when used with these constant definitions
  269.  
  270.  
  271.  
  272. THE CONVERSION CHARACTERS
  273. ____________________________________________________________
  274.  
  275. Following is a list of some of the conversion characters and
  276. the way they are used in the printf() statement.  A complete
  277. list of all of the conversion characters should be included
  278. with the documentation for your compiler.
  279.  
  280.       d    decimal notation
  281.       i    decimal notation (new ANSI standard extension)
  282.       o    octal notation
  283.       x    hexadecimal notation
  284.       u    unsigned notation
  285.       c    character notation
  286.       s    string notation
  287.       f    floating point notation
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.                                                      Page 4-5
  296.  
  297.                  Chapter 4 - Assignments and Logical Compares
  298.  
  299. Each of these is used following a percent sign to indicate the
  300. type of output conversion, and between those two characters,
  301. the following fields may be added.
  302.  
  303.       -     left justification in its field
  304.       (n)   a number specifying minimum field width
  305.       .     to separate n from m
  306.       (m)   significant fractional digits for a float
  307.       l     to indicate a long
  308.  
  309.  
  310. These are all used in the examples which are included in the
  311. program LOTTYPES.C, with the exception of the string notation
  312. which will be covered later in this tutorial.  Note especially
  313. the variable field width specification demonstrated in lines
  314. 33 to 36.  This is not part of the original definition of C,
  315. but it is included in the ANSI standard and has become part
  316. of the C language.  Compile and run this program to see what
  317. effect the various fields have on the output.
  318.  
  319. You now have the ability to display any of the data fields in
  320. the previous programs and it would be to your advantage to go
  321. back and see if you can display some of the fields anyway you
  322. desire.
  323.  
  324.  
  325. COMBINING THE VARIOUS TYPES
  326. ____________________________________________________________
  327.  
  328. Examine the file named COMBINE.C for            =============
  329. examples of combining variables of the            COMBINE.C
  330. various types in a program.  Many times it      =============
  331. is necessary to multiply an int type
  332. variable times a float type variable and C
  333. allows this by giving a strict set of rules it will follow in
  334. order to do such combinations.
  335.  
  336. Five variables of three different types are declared in lines
  337. 4 through 6, and three of them are initialized so we have some
  338. data to work with.  Line 8 gives an example of adding an int
  339. variable to a float variable and assigning the result to a
  340. char type variable.  The cast is used to control the type
  341. addition and is indicated by putting the desired type in front
  342. of the variable as shown.  This forces each of the two
  343. variables to the character type prior to doing the addition. 
  344. In some cases, when the cast is used, the actual bit patterns
  345. must be modified internally in order to do the type coercion. 
  346. Lines 9 through 11 perform the same operation by using
  347. different kinds of type casting to achieve the final result.
  348.  
  349. Lines 13 through 15 illustrate the use of the cast to multiply
  350. two float variables.  In two of the cases the intermediate
  351. results are cast to the int type, with the result being cast
  352. back to the float type.  The observant student will notice
  353.  
  354.                                                      Page 4-6
  355.  
  356.                  Chapter 4 - Assignments and Logical Compares
  357.  
  358. that all three lines will not necessarily produce the same
  359. result.
  360.  
  361. Be sure to compile and execute this program.
  362.  
  363.  
  364. LOGICAL COMPARES
  365. ____________________________________________________________
  366.  
  367. Load and view the file named COMPARES.C for    ==============
  368. many examples of compare statements in C.  We    COMPARES.C
  369. begin by defining and initializing nine        ==============
  370. variables to use in the following compare
  371. statements.  This initialization is new to
  372. you and can be used to initialize variables when they are
  373. defined.
  374.  
  375. The first group of compare statements represents the simplest
  376. kinds of compares since they simply compare two variables. 
  377. Either variable could be replaced with a constant and still
  378. be a valid compare, but two variables is the general case. 
  379. The first compare checks to see if the value of x is equal to
  380. the value of y and it uses the double equal sign for the
  381. comparison.  A single equal sign could be used here but it
  382. would have a different meaning as we will see shortly.  The
  383. second comparison checks to see if the value of x is greater
  384. than the value of z.  
  385.  
  386. The third compare introduces the not operator, the
  387. exclamation, which can be used to invert the result of any
  388. logical compare.  The fourth checks for the value of b less
  389. than or equal to the value of c, and the last checks for the
  390. value of r not equal to the value of s.  As we learned in the
  391. last chapter, if the result of the compare is true, the
  392. statement following the if clause will be executed and the
  393. results are given in the comments.  
  394.  
  395. Note that "less than" and "greater than or equal to" are also
  396. available, but are not illustrated here.
  397.  
  398. It would be well to mention the different format used for the
  399. if statement in this example program.  A carriage return is
  400. not required as a statement separator and by putting the
  401. conditional clause on the same line as the if, it adds to the
  402. readability of the overall program in this case.
  403.  
  404.  
  405. MORE COMPARES
  406. ____________________________________________________________
  407.  
  408. The compares in the second group are a bit more involved. 
  409. Starting with the first compare, we find a rather strange
  410. looking set of conditions in the parentheses.  To understand
  411. this we must understand just what a true or false is in the
  412.  
  413.                                                      Page 4-7
  414.  
  415.                  Chapter 4 - Assignments and Logical Compares
  416.  
  417. C language.  A false is defined as a value of zero, and true
  418. is defined as a non-zero value.  Any integer or character type
  419. of variable can be used for the result of a true/false test,
  420. or the result can be an implied integer or character.
  421.  
  422. Look at the first compare of the second group of compare
  423. statements.  The expression "r != s" will evaluate as a true
  424. since the value of r was set to 0.0 in line 13, so the result
  425. will be a non-zero value.  With most C compilers, it would
  426. always be set to a 1, but you could get in trouble if you
  427. wrote a program that depended on it being 1 in all cases. 
  428. Good programming practice would be to not use the resulting
  429. 1 in any calculations.  Even though the two variables that are
  430. compared are float variables, the result will be of type
  431. integer.  There is no explicit variable to which it will be
  432. assigned so the result of the compare is an implied integer. 
  433. Finally the resulting number, probably 1 in this case, is
  434. assigned to the integer variable x.  If double equal signs
  435. were used, the phantom value, namely 1, would be compared to
  436. the value of x, but since the single equal sign is used, the
  437. value 1 is simply assigned to the variable named x, as though
  438. the statement were not in parentheses.  Finally, since the
  439. result of the assignment in the parentheses was non-zero, the
  440. entire expression is evaluated as true, and z is assigned the
  441. value of 1000.  Thus we accomplished two things in this
  442. statement, we assigned x a new value, probably 1, and we
  443. assigned z the value of 1000.  We covered a lot in this
  444. statement so you may wish to review it before going on.  The
  445. important things to remember are the values that define true
  446. and false, and the fact that several things can be assigned
  447. in a conditional statement.  The value assigned to the
  448. variable x was probably a 1, but remember that the only
  449. requirement is that it is nonzero. 
  450.  
  451. The example in line 20 should help clear up some of the above
  452. in your mind.  In this example, x is assigned the value of y,
  453. and since the result is 11, the condition is non-zero, which
  454. is true, and the variable z is assigned 222.
  455.  
  456. The third example of the second group in line 21, compares the
  457. value of x to zero.  If the result is true, meaning that if
  458. x is not zero, then z is assigned the value of 333, which it
  459. will be.  The last example in this group illustrates the same
  460. concept, since the result will be true if x is non-zero.  The
  461. compare to zero in line 21 is not actually needed and the
  462. result of the compare is true.  The third and fourth examples
  463. of this group are therefore identical.
  464.  
  465.  
  466. ADDITIONAL COMPARE CONCEPTS
  467. ____________________________________________________________
  468.  
  469. The third group of compares will introduce some additional
  470. concepts, namely the logical and and the logical or operators. 
  471.  
  472.                                                      Page 4-8
  473.  
  474.                  Chapter 4 - Assignments and Logical Compares
  475.  
  476. We assign the value of 77 to the three integer variables
  477. simply to get started again with some defined values.  The
  478. first compare of the third group contains the new control &&,
  479. which is the logical and which results in a true if both sides
  480. of the and are true.  The entire statement reads, if x equals
  481. y and if x equals 77 then the result is true.  Since this is
  482. true, the variable z is set equal to 333.
  483.  
  484. The next compare in this group introduces the || operator
  485. which is the logical or operator which results in a true if
  486. either side of the or is true.  The statement reads, if x is
  487. greater than y or if z is greater than 12 then the result is
  488. true.  Since z is greater than 12, it doesn't matter if x is
  489. greater than y or not, because only one of the two conditions
  490. must be true for the result to be true.  The result is true,
  491. so therefore z will be assigned the value of 22.
  492.  
  493.  
  494. LOGICAL EVALUATION (SHORT CIRCUIT)
  495. ____________________________________________________________
  496.  
  497. When a compound expression is evaluated, the evaluation
  498. proceeds from left to right and as soon as the result of the
  499. outcome is assured, evaluation stops.  Namely, in the case of
  500. an "and" evaluation, when one of the terms evaluates to false,
  501. evaluation is discontinued because additional true terms
  502. cannot make the result ever become true.  In the case of an
  503. "or" evaluation, if any of the terms is found to be true,
  504. evaluation stops because it will be impossible for additional
  505. terms to cause the result to be false.  In the case of
  506. additionally nested terms, the above rules will be applied to
  507. each of the nested levels.  This is called short-circuit
  508. evaluation since the remaining terms are not evaluated.
  509.  
  510.  
  511. PRECEDENCE OF OPERATORS
  512. ____________________________________________________________
  513.  
  514. The question will come up concerning the precedence of
  515. operators.  Which operators are evaluated first and which
  516. last?  There are many rules about this topic, but I would
  517. suggest that you don't worry about it at this point.  Instead,
  518. use lots of parentheses to group variables, constants, and
  519. operators in a way meaningful to you.  Parentheses always have
  520. the highest priority and will remove any question of which
  521. operations will be done first in any particular statements.
  522.  
  523. Going on to the next example in group three in line 29, we
  524. find three simple variables used in the conditional part of
  525. the compare.  Since all three are non-zero, all three are
  526. true, and therefore the and of the three variables are true,
  527. leading to the result being true, and z being assigned the
  528. value of 11.  Note that since the variables, r, s, and t are
  529. float type variables, they could not be used this way, but
  530.  
  531.                                                      Page 4-9
  532.  
  533.                  Chapter 4 - Assignments and Logical Compares
  534.  
  535. they could each be compared to zero and the same type of
  536. expression could be used. 
  537.  
  538. Continuing on to line 30 we find three assignment statements
  539. in the compare part of the if statement.  If you understood
  540. the above discussion, you should have no difficulty
  541. understanding that the three variables are assigned their
  542. respective new values, and the result of all three are
  543. non-zero, leading to a resulting value of true. 
  544.  
  545.  
  546. THIS IS A TRICK, BE CAREFUL
  547. ____________________________________________________________
  548.  
  549. The last example of the third group contains a bit of a trick,
  550. but since we have covered it above, it is nothing new to you. 
  551. Notice that the first part of the compare evaluates to false. 
  552. The remaining parts of the compare are not evaluated, because
  553. it is a logical and so it will definitely be resolved as a
  554. false because the first term is false.  If the program was
  555. dependent on the value of y being set to 3 in the next part
  556. of the compare, it will fail because evaluation will cease
  557. following the false found in the first term.  Likewise, the
  558. variable named z will not be set to 4, and the variable r will
  559. not be changed.
  560.  
  561.  
  562.  
  563. POTENTIAL PROBLEM AREAS
  564. ____________________________________________________________
  565.  
  566. The last group of compares illustrate three possibilities for
  567. getting into a bit of trouble.  All three have the common
  568. result that the variable z will not get set to the desired
  569. value, but for different reasons.  In line 37, the compare
  570. evaluates as true, but the semicolon following the second
  571. parentheses terminates the if clause, and the assignment
  572. statement involving z is always executed as the next
  573. statement.  The if therefore has no effect because of the
  574. misplaced semicolon.  This is actually a null statement and
  575. is legal in C.
  576.  
  577. The statement in  line 38 is much more straightforward because
  578. the variable x will always be equal to itself, therefore the
  579. inequality will never be true, and the entire statement will
  580. never do a thing, but is wasted effort.  The statement in line
  581. 39 will always assign 0 to x and the compare will therefore
  582. always be false, never executing the conditional part of the
  583. if statement. 
  584.  
  585. The conditional statement is extremely important and must be
  586. thoroughly understood to write efficient C programs.  If any
  587. part of this discussion is unclear in your mind, restudy it
  588. until you are confident that you understand it thoroughly
  589.  
  590.                                                     Page 4-10
  591.  
  592.                  Chapter 4 - Assignments and Logical Compares
  593.  
  594. before proceeding onward.  Compile and run this program.  Add
  595. some printout to see the results of some of the operations.
  596.  
  597.  
  598. THE CRYPTIC PART OF C
  599. ____________________________________________________________
  600.  
  601. There are three constructs used in C that       =============
  602. make no sense at all when first encountered       CRYPTIC.C
  603. because they are not intuitive, but they        =============
  604. greatly increase the efficiency of the
  605. compiled code and are used extensively by
  606. experienced C programmers.  You should therefore be exposed
  607. to them and learn to use them because they will appear in
  608. most, if not all, of the programs you see in the publications. 
  609. Load and examine the file named CRYPTIC.C for examples of the
  610. three new constructs.
  611.  
  612. In this program, some variables are defined and initialized
  613. in the same statements for use later.  The statement in line
  614. 8 simply adds 1 to the value of x, and should come as no
  615. surprise to you.  The next two statements also add one to the
  616. value of x, but it is not intuitive that this is what happens. 
  617. It is simply by definition that this is true. Therefore, by
  618. definition of the C language, a double plus sign either before
  619. or after a variable increments that variable by 1. 
  620. Additionally, if the plus signs are before the variable, the
  621. variable is  incremented before it is used, and if the plus
  622. signs are after the variable, the variable is used, then
  623. incremented.  In line 11, the value of y is assigned to the
  624. variable z, then y is incremented because the plus signs are
  625. after the variable y.  In the last statement of the
  626. incrementing group of example statements, line 12, the value
  627. of y is incremented then its value is assigned to the variable
  628. z.
  629.  
  630. The next group of statements illustrate decrementing a
  631. variable by one.  The definition works exactly the same way
  632. for decrementing as it does for incrementing.  If the minus
  633. signs are before the variable, the variable is decremented,
  634. then used, and if the minus signs are after the variable, the
  635. variable is used, then decremented.
  636.  
  637.  
  638. THE CRYPTIC ARITHMETIC OPERATOR
  639. ____________________________________________________________
  640.  
  641. Another useful but cryptic operator is the arithmetic
  642. operator.  This operator is used to modify any variable by
  643. some constant value.  The statement in line 23 simply adds 12
  644. to the value of the variable a.  The statement in line 24 does
  645. the same, but once again, it is not intuitive that they are
  646. the same.  Any of the four basic functions of arithmetic, +,
  647. -, *, or /, can be handled in this way, by putting the
  648.  
  649.                                                     Page 4-11
  650.  
  651.                  Chapter 4 - Assignments and Logical Compares
  652.  
  653. function desired in front of the equal sign and eliminating
  654. the second reference to the variable name.  It should be noted
  655. that the expression on the right side of the arithmetic
  656. operator can be any valid expression, the examples are kept
  657. simple for your introduction to this new operator. 
  658.  
  659. Just like the incrementing and decrementing operators, the
  660. arithmetic operator is used extensively by experienced C
  661. programmers and it would pay you well to understand it.
  662.  
  663.  
  664. THE CONDITIONAL EXPRESSION
  665. ____________________________________________________________
  666.  
  667. The conditional expression is just as cryptic as the last two,
  668. but once again it can be very useful so it would pay you to
  669. understand it.  It consists of three expressions within
  670. parentheses separated by a question mark and a colon.  The
  671. expression prior to the question mark is evaluated to
  672. determine if it is true or false.  If it is true, the
  673. expression between the question mark and the colon is
  674. evaluated, and if it is not true, the expression following the
  675. colon is evaluated.  The result of the evaluation is used for
  676. the assignment as illustrated in line 30.  The final result
  677. is identical to that of an if statement with an else clause. 
  678. This is illustrated by the example in lines 32 through 35 of
  679. this group of statements.  The conditional expression has the
  680. added advantage of more compact code that will compile to
  681. fewer machine instructions in the final program.
  682.  
  683. Lines 37 and 38 of this example program are given to
  684. illustrate a very compact way to assign the greater of the two
  685. variables a or b to c, and to assign the lessor of the same
  686. two variables to c.  Notice how efficient the code is in these
  687. two examples.
  688.  
  689.  
  690. TO BE CRYPTIC OR NOT TO BE CRYPTIC
  691. ____________________________________________________________
  692.  
  693. Several students of C have stated that they didn't like these
  694. three cryptic constructs and that they would simply never use
  695. them.  This will be fine if they never have to read anybody
  696. else's program, or use any other programs within their own. 
  697. I have found many functions that I wished to use within a
  698. program but needed a small modification to use it, requiring
  699. me to understand another person's code.  It would therefore
  700. be to your advantage to learn these new constructs, and use
  701. them. They will be used in the remainder of this tutorial, so
  702. you will be constantly exposed to them.
  703.  
  704. This has been a long chapter but it contained important
  705. material to get you started in using C.  In the next chapter,
  706. we will go on to the building blocks of C, the functions.  At
  707.  
  708.                                                     Page 4-12
  709.  
  710.                  Chapter 4 - Assignments and Logical Compares
  711.  
  712. that point, you will have enough of the basic materials to
  713. allow you to begin writing meaningful programs.
  714.  
  715.  
  716. PROGRAMMING EXERCISES
  717. ____________________________________________________________
  718.  
  719. 1.   Write a program that will count from 1 to 12 and print
  720.      the count, and its square, for each count.
  721.            1     1
  722.            2     4
  723.            3     9   etc.
  724.  
  725. 2.   Write a program that counts from 1 to 12 and prints the
  726.      count and its inversion to 5 decimal places for each
  727.      count. This will require a floating point number.
  728.            1    1.00000
  729.            2     .50000
  730.            3     .33333
  731.            4     .25000
  732.           etc.
  733.  
  734. 3.   Write a program that will count from 1 to 100 and print
  735.      only those values between 32 and 39, one to a line.
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751.  
  752.  
  753.  
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.                                                     Page 4-13
  764.