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

  1.                  CHAPTER 3 - The simple Pascal data types
  2.  
  3.  
  4.             Pascal  has only 5 basic data types which are predefined 
  5.  
  6.         and  can be used anywhere you desire provided you  use  them 
  7.  
  8.         properly.   The  five  types  and a very  brief  description 
  9.  
  10.         follows;
  11.  
  12.             Integer   The integers from -32768 to 32767
  13.             Byte      The integers from 0 to 255
  14.             Real      Floating point numbers from 1E-38 to 1E+38
  15.             Boolean   Can only have the value TRUE or FALSE
  16.             Char      Any character in the ASCII character set
  17.  
  18.             Please note that the Byte type of data is not a part  of 
  19.  
  20.         the  standard  Pascal  definition  but  is  included  as  an 
  21.  
  22.         extension to the TURBO Pascal compiler.
  23.  
  24.             A  complete definition of these can be found on pages 41 
  25.  
  26.         and  42 of the TURBO Pascal reference manual (version  3.0).  
  27.  
  28.         It  would  be good to read those two pages now  for  a  good 
  29.  
  30.         definition  prior to our learning how to define and use them 
  31.  
  32.         in  a  program.   The  integers are by far  the  easiest  to 
  33.  
  34.         understand so we will start with a simple program that  uses 
  35.  
  36.         some  integers in a very simple way.   Load INTVAR into your 
  37.  
  38.         TURBO system and lets take a look at it.
  39.  
  40.                             OUR FIRST VARIABLES
  41.  
  42.             Immediately  following the PROGRAM statement is  another 
  43.  
  44.         reserved word, VAR.  VAR is used to define a variable before 
  45.  
  46.         it  can  be  used anywhere in  the  program.   There  is  an 
  47.  
  48.         unbroken  rule  of Pascal that states "Nothing can  be  used 
  49.  
  50.         until  it is defined."  The compiler will complain at you if 
  51.  
  52.         you try to use a variable without properly defining it.   It 
  53.  
  54.         seems  a  bit bothersome to have to  define  every  variable 
  55.  
  56.         prior  to  its  use but this rule will catch  many  spelling 
  57.  
  58.         errors of variables before they cause trouble.   Some  other 
  59.  
  60.         languages  will  simply define a new variable with  the  new 
  61.  
  62.         name and go merrily on its way producing some well formatted 
  63.  
  64.         garbage for you.
  65.  
  66.             Notice  that  there is only one VAR,  but it is used  to 
  67.  
  68.         define three different variables,  "count",  "x",  and  "y".  
  69.  
  70.         Once  a  VAR is recognized,  the compiler will  continue  to 
  71.  
  72.         recognize  variable  definitions  line after line  until  it 
  73.  
  74.         finds another reserved word.  It would be permissible to put 
  75.  
  76.         a VAR on the second line also but it is not  necessary.   It 
  77.  
  78.         would  also be permissible to put all three variables on one 
  79.  
  80.         line  but programming style will dictate where you  put  the 
  81.  
  82.         three  variables.   Following the colon on each line is  the 
  83.  
  84.         word  INTEGER  which  is  a  standard  identifier  which  is 
  85.  
  86.         different from a reserved word.  An identifier is predefined 
  87.  
  88.         like  a reserved word but you can redefine it thereby losing 
  89.  
  90.  
  91.  
  92.                                   Page 11
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.                  CHAPTER 3 - The simple Pascal data types
  103.  
  104.  
  105.         its  original purpose and meaning.   For now and for a  long 
  106.  
  107.         time, don't do that. Page 38 contains a list of identifiers.
  108.  
  109.                            OUR FIRST ARITHMETIC
  110.  
  111.             Now that we have three variables defined as integer,  we 
  112.  
  113.         are  free to use them in a program in any way we  desire  as 
  114.  
  115.         long as we use them properly.   If we tried to assign a REAL 
  116.  
  117.         value  to  "x",  the compiler will generate an  error,  once 
  118.  
  119.         again preventing a garbage output.  Observe the start of the 
  120.  
  121.         main  body  of  the program.   There  are  three  statements 
  122.  
  123.         assigning values to "x",  "y", and "count".  A fine point of 
  124.  
  125.         mathematics  would state that "count" is only equal  to  the 
  126.  
  127.         value of x+y until either was modified,  therefore the equal 
  128.  
  129.         sign used in so many other languages is not used here.   The 
  130.  
  131.         sign  :=  is used,  and can be read as "is replaced  by  the 
  132.  
  133.         value   of"   when  reading  a  listing  to   preserve   the 
  134.  
  135.         mathematical purity of Pascal. Another quicker way is to use 
  136.  
  137.         the word "gets".   Thus x := x + 1 would be read "x gets the 
  138.  
  139.         value of x plus 1".  We will see later that the simple equal 
  140.  
  141.         sign is reserved for use in a different manner.
  142.  
  143.             The first three statements give "x" the value of 12, "y" 
  144.  
  145.         the  value of 13,  and "count" the value of 12+13 or 25.  We 
  146.  
  147.         need  to get those values out of the computer,  so  we  need 
  148.  
  149.         another extension to the WRITELN statement.   The first part 
  150.  
  151.         of the data within the parentheses should be familiar to you 
  152.  
  153.         now,  but  the second part is new.   Multiple outputs can be 
  154.  
  155.         handled within one WRITELN if the fields are separated by  a 
  156.  
  157.         comma.   To  output a variable,  simply write the variable's 
  158.  
  159.         name in the output field.  The number following the variable 
  160.  
  161.         in  each case is the number of output columns to be used  by 
  162.  
  163.         the output data.  This number is optional and can be omitted 
  164.  
  165.         allowing the system to use as many columns as it needs.  For 
  166.  
  167.         purposes  of  illustration  they  have  all  been   assigned 
  168.  
  169.         different  numbers  of  columns.   At this  point,  you  can 
  170.  
  171.         compile and run INTVAR to see its output.  
  172.  
  173.             To  illustrate  the various ways to  output  data,  load 
  174.  
  175.         INTVAR2   and  observe  that  even  though  the  output   is 
  176.  
  177.         identical,  it  is output in a completely different  manner.  
  178.  
  179.         Observe  especially  that  a WRITELN all  by  itself  simply 
  180.  
  181.         returns to the beginning of a new line on the video monitor.  
  182.  
  183.         Compile and run this program also to observe its output.
  184.  
  185.                       NOW LET'S USE LOTS OF VARIABLES
  186.  
  187.             Load  ALLVAR to observe a short program using all  5  of 
  188.  
  189.         the  basic  data types.   The variables are simply  assigned 
  190.  
  191.         values and the values are printed.   A complete and detailed 
  192.  
  193.         description of the options available in the WRITE  statement 
  194.  
  195.  
  196.  
  197.                                   Page 12
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.                  CHAPTER 3 - The simple Pascal data types
  208.  
  209.  
  210.         is  given in the TURBO reference manual on pages 111 through 
  211.  
  212.         113.   It would be to your advantage to read this section at 
  213.  
  214.         this time since very little explanation will be given  about 
  215.  
  216.         WRITE  statements from this point on.   We will discuss  the 
  217.  
  218.         method  by which we can write to disk files or other  output 
  219.  
  220.         devices when the time comes.
  221.  
  222.             Back  to  the basic types.   Pascal does lots  of  cross 
  223.  
  224.         checking  for obvious errors.   It is illegal to assign  the 
  225.  
  226.         value of any variable with a value that is of the wrong type 
  227.  
  228.         or outside the allowable range of that variable.   There are 
  229.  
  230.         routines  to convert from one system to another when that is 
  231.  
  232.         necessary.  Suppose, for example, that you wished to use the 
  233.  
  234.         value of an integer in a calculation of real numbers.   That 
  235.  
  236.         is  possible  by first converting the integer  into  a  real 
  237.  
  238.         number  of the same value and using the new real variable in 
  239.  
  240.         the  desired calculations.   The new real variable  must  of 
  241.  
  242.         course  be defined in a VAR as a real before it can be used.  
  243.  
  244.         Details of how to do the conversion will be given later.
  245.  
  246.             Since we have some variables defined,  it would be  nice 
  247.  
  248.         to  use  the  properties  of computers for  which  they  are 
  249.  
  250.         famous, namely some mathematics.  Two programs are available 
  251.  
  252.         for your observation to illustrate the various kinds of math 
  253.  
  254.         available,  REALMATH using real variables, and INTMATH using 
  255.  
  256.         integer variables.   You can edit, compile, and run these on 
  257.  
  258.         your  own  with  no  comment from  me  except  the  comments 
  259.  
  260.         embedded into the source files.  Chapter 6 on pages 51 to 54 
  261.  
  262.         of your TURBO reference manual completely defines the simple 
  263.  
  264.         mathematics available.
  265.  
  266.             Byte is used just like integers but with a much  smaller 
  267.  
  268.         value.   Only  one byte of computer memory is used for  each 
  269.  
  270.         variable defined as byte but 2 are used for integer types.
  271.  
  272.                              BOOLEAN VARIABLES
  273.  
  274.             Lets  take a look at the boolean variable which is  only 
  275.  
  276.         allowed  to  take on two different values,  TRUE  or  FALSE.  
  277.  
  278.         This  variable  is  used  for loop  controls,  end  of  file 
  279.  
  280.         indicators  or  any other TRUE or FALSE  conditions  in  the 
  281.  
  282.         program.   Variables  can be compared to determine a boolean 
  283.  
  284.         value.  An  illustration is the best way to learn about  the 
  285.  
  286.         boolean variable so load BOOLMATH, observe, compile, and run 
  287.  
  288.         it.
  289.  
  290.             Char is a very useful variable,  but not all by  itself.  
  291.  
  292.         It is very powerful when used in an array or some other user 
  293.  
  294.         defined  data  structure which is beyond the scope  of  this 
  295.  
  296.         chapter.   A  very simple program,  CHARDEMO is included  to 
  297.  
  298.         give  you  an  idea  of how a char  variable  can  be  used.  
  299.  
  300.  
  301.  
  302.                                   Page 13
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.                  CHAPTER 3 - The simple Pascal data types
  313.  
  314.  
  315.         Observe  and run CHARDEMO for a very brief idea of what  the 
  316.  
  317.         char variable is all about.
  318.  
  319.             Examine  the sample program CONVERT for several examples 
  320.  
  321.         of converting data from one simple variable to another.  The 
  322.  
  323.         program is self explanatory.
  324.  
  325.  
  326.                            PROGRAMMING EXERCISE
  327.  
  328.         1.  Write a program containing several variable  definitions 
  329.  
  330.             and do some math on them, printing out the results.
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.                                   Page 14
  373.  
  374.