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

  1.                    CHAPTER 2 - Getting started in Pascal
  2.  
  3.  
  4.                          YOUR FIRST PASCAL PROGRAM
  5.  
  6.             Lets  get right into a program that really does  nothing 
  7.  
  8.         but  is an example of the most trivial Pascal program.  Load 
  9.  
  10.         Turbo  Pascal,  select TRIVIAL as a Work  file,  and  select 
  11.  
  12.         Edit. This assumes that you have been successful in learning 
  13.  
  14.         how to use the TURBO Pascal system.
  15.  
  16.             You  should  now  have the most trivial  Pascal  program 
  17.  
  18.         possible  on your display,  and we can take a look  at  each 
  19.  
  20.         part to define what it does.
  21.  
  22.             The  first  line  is  required in  the  standard  Pascal 
  23.  
  24.         definition and is the program name which can be any name you 
  25.  
  26.         like,  as  long  as it follows the rules for  an  identifier 
  27.  
  28.         given  in  the  next  paragraph.  It  can  have  no  blanks, 
  29.  
  30.         otherwise  it would be considered as two words and it  would 
  31.  
  32.         confuse the compiler. The first word PROGRAM is the first of 
  33.  
  34.         the reserved words mentioned earlier and it is the indicator 
  35.  
  36.         to the Pascal compiler that this is the name of the program. 
  37.  
  38.         Notice that the line ends with a semicolon.  Pascal uses the 
  39.  
  40.         semicolon  as  the  statement  separator  and  although  all 
  41.  
  42.         statements do not actually end in a semicolon,  most do, and 
  43.  
  44.         use of the semicolon will clear up later in your mind. TURBO 
  45.  
  46.         Pascal does not require the PROGRAM statement, but to remain 
  47.  
  48.         compatible with standard Pascal,  it will simply ignore  the 
  49.  
  50.         entire  statement.  I like to include a program name both to 
  51.  
  52.         keep  me thinking in standard Pascal,  and to add  a  little 
  53.  
  54.         more indication of the purpose of each program.
  55.  
  56.                           WHAT IS AN IDENTIFIER?
  57.  
  58.             All identifiers,  including program name,  procedure and 
  59.  
  60.         function names,  type definitions, and constant and variable 
  61.  
  62.         names,  will  start  with an alphabetical character  and  be 
  63.  
  64.         composed  of  any  combination  of  alphabetic  and  numeric 
  65.  
  66.         characters  with  no embedded blanks.  Upper or  lower  case 
  67.  
  68.         alphabetic  characters are not significant and may be  mixed 
  69.  
  70.         at  will.  (If  you find this definition confusing  at  this 
  71.  
  72.         point,  don't worry about it,  it will be clear later but it 
  73.  
  74.         must  be defined early).  The standard definition of  Pascal 
  75.  
  76.         requires that any implementation (i.e.  any compiler written 
  77.  
  78.         by  some  company)  must use at least 8  characters  of  the 
  79.  
  80.         identifier  as  significant  and may  ignore  the  remaining 
  81.  
  82.         characters  if more are used.  Most implementations use  far 
  83.  
  84.         more  than 8.  TURBO Pascal uses up to 127 characters in  an 
  85.  
  86.         identifier  as  being significant.  Since nearly all  Pascal 
  87.  
  88.         compilers use the underline as an allowable character in  an 
  89.  
  90.         identifier, it will be freely used throughout this tutorial. 
  91.  
  92.         The  underline is used in the program name "puppy_dog" which 
  93.  
  94.         should be on your display at this time.
  95.  
  96.  
  97.  
  98.                                  Page 6
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.                    CHAPTER 2 - Getting started in Pascal
  109.  
  110.  
  111.  
  112.             Returning  to the example program,  the next line  is  a 
  113.  
  114.         blank  line which is ignored by all Pascal  compilers.  More 
  115.  
  116.         will be said about that at the end of this chapter.
  117.  
  118.                             NOW FOR THE PROGRAM
  119.  
  120.             The  next two lines comprise the actual Pascal  program, 
  121.  
  122.         which  in  this  case  does absolutely  nothing.  It  is  an 
  123.  
  124.         illustration  of the minimum Pascal program.  The two  words 
  125.  
  126.         BEGIN  and  END  are the next two  reserved  words  we  will 
  127.  
  128.         consider.  Any  logical  grouping  of  Pascal  code  can  be 
  129.  
  130.         isolated  by bracketing it with the two reserved words BEGIN 
  131.  
  132.         and END. You will use this construct repeatedly as you write 
  133.  
  134.         Pascal code so it is well to learn it thoroughly. Code to be 
  135.  
  136.         executed by conditional jumps will be bracketed by BEGIN and 
  137.  
  138.         END, as will code within a loop, and code contained within a 
  139.  
  140.         subroutine (although they are called PROCEDURES in  Pascal), 
  141.  
  142.         and  in many other ways.  In the present program,  the BEGIN 
  143.  
  144.         and  END  are  used to bracket the main  program  and  every 
  145.  
  146.         Pascal  program will have the main program bracketed by  the 
  147.  
  148.         BEGIN  and END statement.  Since there is nothing to  do  in 
  149.  
  150.         this program, there are no statements.
  151.  
  152.             Finally,  although  it could be very easily  overlooked, 
  153.  
  154.         there  is one more very important part of the  program,  the 
  155.  
  156.         period  following  END.  The  period is the  signal  to  the 
  157.  
  158.         compiler  that  it  has reached the end  of  the  executable 
  159.  
  160.         statements and is therefore finished compiling. Every Pascal 
  161.  
  162.         program  will have one,  and only one period in it and  that 
  163.  
  164.         one period will be at the end of the program. I must qualify 
  165.  
  166.         that  statement  in  this regard,  a period can be  used  in 
  167.  
  168.         comments,  and in text to be output.  In fact there are some 
  169.  
  170.         data  formats that require using a period as part  of  their 
  171.  
  172.         structure.  The statement is true however that there is only 
  173.  
  174.         one period in the executable part of a Pascal program. Think 
  175.  
  176.         of  a Pascal program as one long sentence with one period at 
  177.  
  178.         the end.
  179.  
  180.             That should pretty well describe our first program.  Now 
  181.  
  182.         it is time to compile and run it. To do so you must exit the 
  183.  
  184.         editor,   using  Ctrl-K-D,  unless  you  modified  the  exit 
  185.  
  186.         command.  Then compile the program,  and finally run it  and 
  187.  
  188.         observe the result.
  189.  
  190.             Since  that  program  didn't do much,  it was  not  very 
  191.  
  192.         interesting, so let's get one that does something.
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                                  Page 7
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.                    CHAPTER 2 - Getting started in Pascal
  211.  
  212.  
  213.                        A PROGRAM THAT DOES SOMETHING
  214.  
  215.             Load the Pascal program WRITESM and edit it.   The  name 
  216.  
  217.         is  sort  of  cryptic for "Write Some" and it  will  give  a 
  218.  
  219.         little   output  to  the  monitor.   The  program  name   is 
  220.  
  221.         "kitty_cat"  which says nothing about the program itself but 
  222.  
  223.         is  any identifier we choose to make it.  We still have  the 
  224.  
  225.         BEGIN  and END to define the main program area  followed  by 
  226.  
  227.         the period.  However,  now we have two additional statements 
  228.  
  229.         between the BEGIN and END.  WRITELN is another reserved word 
  230.  
  231.         and  it is probably not surprising that it means to write  a 
  232.  
  233.         line of data somewhere. Without a modifier, (to be explained 
  234.  
  235.         in due time),  it will write to the default device which, in 
  236.  
  237.         the  case of our IBM compatible,  is the video display.  The 
  238.  
  239.         data within the parentheses is the data to be output to  the 
  240.  
  241.         display and although there are many possibilities of display 
  242.  
  243.         information,  we will restrict ourselves to the simplest for 
  244.  
  245.         the time being.  Any data between apostrophes will simply be 
  246.  
  247.         output as text information.
  248.  
  249.             Notice  the semicolon at the end of each line.  This  is 
  250.  
  251.         the statement separator referred to earlier and tells Pascal 
  252.  
  253.         that  this  line is complete as it stands,  nothing more  is 
  254.  
  255.         coming that could be considered part of this statement. This 
  256.  
  257.         program will output the two lines of text and stop.  Now  it 
  258.  
  259.         is time to go try it.  Exit the editor, then compile and run 
  260.  
  261.         the program.
  262.  
  263.             You  should  now get the two lines output to  the  video 
  264.  
  265.         display  every  time  you run it.  When you  grow  bored  of 
  266.  
  267.         running WRITESM lets go on to another example.
  268.  
  269.                      ANOTHER PROGRAM WITH MORE OUTPUT
  270.  
  271.             Load and edit WRITEMR.  This new program has three lines 
  272.  
  273.         of  output  but the first two are different because  another 
  274.  
  275.         reserved  word  is introduced to  us,  namely  WRITE.  WRITE 
  276.  
  277.         causes  the text to be output in exactly the same manner  as 
  278.  
  279.         WRITELN, but WRITE does not cause a carriage return. WRITELN 
  280.  
  281.         causes its output to take place then returns the  "carriage" 
  282.  
  283.         to  the first character of the next line.  The end result is 
  284.  
  285.         that all three of the lines will be output on the same  line 
  286.  
  287.         when the program is run. Notice that there is a blank at the 
  288.  
  289.         end  of  each of the first two lines so that the  formatting 
  290.  
  291.         will look nice. Exit the editor now and try the new program.
  292.  
  293.             It is time to confess to a little lie. WRITELN and WRITE 
  294.  
  295.         are   not  actually  reserved  words,   they  are   actually 
  296.  
  297.         predefined  functions which we have not discussed  yet.  For 
  298.  
  299.         the time being,  it is easiest to think of them as  reserved 
  300.  
  301.  
  302.  
  303.  
  304.  
  305.                                  Page 8
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.                    CHAPTER 2 - Getting started in Pascal
  316.  
  317.  
  318.         words.  When  we get to the proper point,  we will  redefine 
  319.  
  320.         them properly.
  321.  
  322.             Now  might be a good time for you to go back to  editing 
  323.  
  324.         WRITEMR and add a few more output commands to see if they do 
  325.  
  326.         what  you think they should do.  When you tire of  that,  we 
  327.  
  328.         will  go on to the next file and learn about comments within 
  329.  
  330.         your Pascal program.
  331.  
  332.                       ADDING COMMENTS IN THE PROGRAM
  333.  
  334.             The  file named PASCOMS is similar to the others  except 
  335.  
  336.         that  comments  have  been added to  illustrate  their  use. 
  337.  
  338.         Pascal  defines  comments as anything between (* and  *)  or 
  339.  
  340.         anything  between  {  and  }.  Originally  only  the  wiggly 
  341.  
  342.         brackets  were defined but since many keyboards didn't  have 
  343.  
  344.         them available, the parenthesis star combination was defined 
  345.  
  346.         as an extension and is probably universal by now, so you can 
  347.  
  348.         use either. Most of the comments are self explanatory except 
  349.  
  350.         for the one within the code. Since comments can go from line 
  351.  
  352.         to line, the two lines that would print "send money" are not 
  353.  
  354.         Pascal code but are commented out. Try compiling and running 
  355.  
  356.         this  program,  then  edit the comments out  so  that  "send 
  357.  
  358.         money" is printed also.
  359.  
  360.             When  you have successfully modified and run the program 
  361.  
  362.         with  comments,  we  will go on to explain  good  formatting 
  363.  
  364.         practice  and  how  Pascal actually  searches  through  your 
  365.  
  366.         source file (Pascal program) for its executable statements.
  367.  
  368.                          GOOD FORMATTING PRACTICE
  369.  
  370.             Edit  GOODFORM now to see an example of good  formatting 
  371.  
  372.         style.  It  is important to note that Pascal doesn't give  a 
  373.  
  374.         hoot  where you put carriage returns or how many blanks  you 
  375.  
  376.         put  in when a blank is called for as  a  delimiter.  Pascal 
  377.  
  378.         only  uses  the  combination of reserved words  and  end-of-
  379.  
  380.         statement  semicolons to determine the logical structure  of 
  381.  
  382.         the  program.   Since  we  have  really  only  covered   two 
  383.  
  384.         executable  statements,  I  have used them to build  a  nice 
  385.  
  386.         looking  program that can be easily understood at a  glance. 
  387.  
  388.         Compile and run this program to see that it really does what 
  389.  
  390.         you think it should do. 
  391.  
  392.                        VERY POOR FORMATTING PRACTICE
  393.  
  394.             Edit   UGLYFORM  now  to  see  an  example  of  terrible 
  395.  
  396.         formatting style.  It is not really apparent at a glance but 
  397.  
  398.         the  program you are looking at is exactly the same  program 
  399.  
  400.         as the last one. Pascal doesn't care which one you ask it to 
  401.  
  402.         run because to Pascal,  they are identical.  To you they are 
  403.  
  404.  
  405.  
  406.                                  Page 9
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.                    CHAPTER 2 - Getting started in Pascal
  417.  
  418.  
  419.         considerably different,  and the second one would be a  mess 
  420.  
  421.         to try to modify or maintain sometime in the future.
  422.  
  423.             UGLYFORM  should be a good indication to you that Pascal 
  424.  
  425.         doesn't  care about programming style or form.  Pascal  only 
  426.  
  427.         cares  about  the structure,  including reserved  words  and 
  428.  
  429.         delimiters such as blanks and semicolons.  Carriage  returns 
  430.  
  431.         are  completely  ignored as are extra blanks.  You  can  put 
  432.  
  433.         extra blanks nearly anywhere except within reserved words or 
  434.  
  435.         variable  names.  You  should pay attention  to  programming 
  436.  
  437.         style  but don't get too worried about it yet.  As time goes 
  438.  
  439.         by you will develop a style of statement indentation, adding 
  440.  
  441.         blank  lines  for clarity,  and clear commenting  of  Pascal 
  442.  
  443.         source  code.  Programs  are available to read  your  source 
  444.  
  445.         code,  and  put  it in a "pretty" format,  but that  is  not 
  446.  
  447.         important now.
  448.  
  449.             Not only is the form of the program important, the names 
  450.  
  451.         used  for variables can be very helpful or hindering  as  we 
  452.  
  453.         can see in the next chapter. Feel free to move things around 
  454.  
  455.         and modify the format of any of the programs we have covered 
  456.  
  457.         so far and when you are ready, we will start on variables in 
  458.  
  459.         the next chapter.
  460.  
  461.              Be sure you compile and run UGLYFORM.
  462.  
  463.                            PROGRAMMING EXERCISES
  464.  
  465.         1.  Write  a  program that displays your name on  the  video 
  466.  
  467.             monitor. 
  468.  
  469.         2.  Modify  your program to display your name and address on 
  470.  
  471.             one  line,  then  modify it by changing the  WRITE's  to 
  472.  
  473.             WRITELN's so that the name and address are on  different 
  474.  
  475.             lines.
  476.  
  477.  
  478.  
  479.  
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.                                  Page 10
  495.  
  496.