IPT - A Virtual Approach IPT A Virtual Approach by Peter Whitehouse
Quick Links:
 
 
Information and Intelligent Systems Social and Ethical Implications Human Computer Interaction Software and Systems Engineering eXercise Files Course Outline and Assessment A-Z of Geeky Acronyms Terrace Work Program 2004 Sillybus FAQ = Frequently Asked Questions Help
 
 

The Store

Adding To The Environment

General Project Overview


         environment        - store (where variables are held)

         + algorithm        - body (where instructions that operate on
                            store data are held)

         + data = A PROGRAM

A good programmer tries to do many things:

  • be consistent in naming of objects used in programs
  • include in-program documentation that describes what is done, whey and to what
  • be sensible/prudent decisions regarding the types of objects the program uses
  • chooses a logical arrangement of STORE/ENVIRONMENT objects
  • aims for a small/straight-forward BODY


Types and Constants

Pascal is an incredibly rich and flexible language. Programmers are presented with a rich array of pre-declared data types and built-ins.

We have encountered many simple data types so far:

    byte, char, integer, real, boolean ...

It is also possible (and simple) to invent your own types of data to answer a particular need or simplify a particular design decision. Similarly, it is simple to use one of the many pre-declared constants or create your own

When designing a project, THINK FIRST. As a teacher, this is very easy to say. As a learning programmer however, this is very tricky as you are just learning design as well. Good (=careful) programmers generally avoid inventing objects as they think of them - usually all objects are identified in the planning stages.

Creating variables on-the-go often leads to poorly structured programs that are difficult to de-bug and worst to maintain.

Add comments AS YOU GO - don't say "I'll put it in later." - you will forget!


CONSTANTS

As the name implies - a CONSTANT is an object to be used in your program that will not change its value throughout the life of the program. If the object needs to change value (or indeed receive a value during the running of the program) then a VARIABLE is what you want to use.


   syntax:     const     name [:type]  = value;
                        {name [:type]  = value;}

      eg1:     const     gravity  = 9.81;

in the above example we have created a label (gravity) that can be used throughout your program in place of the value 9.81.Once you have defined such a constant in a program, you CANNOT alter its value in the program.

      eg2: const c : real = 248673.0;

Example 2 is all-together different - although c is labelled a constant, pascal treats it like a variable - meaning you can alter it's value during execution of the program. This (IWHO) is a flaw in the grammar of Pascal - as such I would advise you avoid using a typed constant in preference to a variable.

Commonly used values in programs should be stored as constants - this increases readability, and allows quick changes to the program. As constants are declared at the top of the program, it is easy to edit them in the development stages of your program when you want to scale-up or down your program for the purposes of testing.

How the definitions work: Unlike the Var, Const statements are instructions to the compiler to replace every occurrence of the constant's name with the supplied value at compiling compile. There is no space allocated for Consts in RAM.

A Const statement can therefore be thought of as a definition NOT an assignment statement.

Typed constants act like a Var + assignment (and are non- standard).

Pre-defined constants can be re-defined (eg. Const pi = 22/7). Take care as this kills the pre-stored value of this constant if it is already known by the system.


SPECIFYING SUB-RANGES

There is often the need to narrow the range (by definition) of possible values a variable is allowed to take on.

   eg2:  yearLevel = 1..12;    <-- 15 is clearly out of the range
         letter = 'A'..'Z';        <-- '*' is not a letter

In the Var it is possible to specify ranges

   syntax:     name : val1..val2;     (so long as val1 <= val2)

eg3: Var yearLevel : 1..12; letter : 'A'..'Z';

Subranges can be specified for any ordinal data (ie. NOT REAL)

When you use sub-ranges, you 'tighten' up your program (ie. it will encounter an error condition at the slightest violation of range)

   eg4:  const     last = 5;
         var       loop : 1..last;
         begin
            loop := 1;
            while loop <= last do
               begin
                   Showmessage('again and ...');
                   inc(loop)          <---- last time through the
               end;                         loop will crash prog.
         end;

USER ENUMERATED TYPES

It is possible, and often desirable to invent your own data types.

   eg5:  var rainbow : (red, orange, yellow, green, blue, indigo,
                         violet);

In the above example, a variable called rainbow can have any of the above listed values with 'red' becoming a value and not a valid variable name.

Enumerated types are mapped directly to a byte as so:

         red       =  00000000       ord(red) = 0
         orange    =  00000001       ord(orange) = 1
         yellow    =  00000010       ord(yellow) = 3
         violet    =  00000110       ord(violet) = 6

Assignments and many ordinal operations are supported with user enumerated types:

      eg:   rainbow  := green;
            rainbow  := succ(rainbow);    (rainbow <> violet)
            rainbow  := pred(yellow);

There are some problems - input/output is not directly supported for enumerated types.

         eg:   rainbow := inputbox('colour','enter a colour','red') is illegal!
               showmessage(rainbow)   is too!

We can work around such problems using CASE and other selection structures to cope with outputting the values:

      eg:   case rainbow of
               red       :  showmessage('red');
               orange    :  showmessage('orange');

Input of values for user-enumerated types is tricky if the values are not a subset of an existing conventional datatype. In the case where values of your user-enumerated type are not part of any recognisable type, character strings can be used to input the values again with a case or other selection structure to do the actual assignment.

As user-enumerated types are mapped onto a byte, NO MORE THAN 256 different values are allowed to be defined for a new type.

You need to be careful with the values you list as being part of your type - values enumerated can only be used in ONE type declaration:

      eg:   var    months :    (jan, feb, mar, oct, may);
                   bases  :    (bin, dec, oct, hex);   <-----error


      eg:   var    letterGrades : ('A', 'B', 'C', 'D', 'E')
                     {A Subrange of char, alternatively 'A'..'E'}
                   cardinal       :  0..maxint;
                   switch         :  (on, off);

TYPE STATEMENTS

Type statements can be used to combat problems encountered with overlapping value lists, and greatly increase readability of your code:

   syntax:  type  name  =  specification;
                 {name  =  specification;}

where specification could be a subrange, or an enumerated type, or a pre-defined type or another type.

   eg:   type  wholeNumbers       =  0..maxint;
               oneToSix           =  1..6;
               reply              =  (y,n);
               diceThrow          =  one_to_six;
               yesOrNo            =  reply;
               interger           =  integer;

Type definitions differ from Variable declarations (var) in that variable declarations use space in the store (the data segment) while Type occupies space in the code segment

Type does not create a variable, merely the instructions on how memory shall be organised if a variable of that type is declared

const & type are definitions, var is a declaration

   consider:

         type      grades   = (VHA,HA,SA,LA,VLA);
         var       grade    : grades;

         begin
            showmessage (inttostr(ord(SA)));
            grade     :=    grades(2);
            grade     :=    grades(30 DIV (6+3));

Adding to the Environment
 

wonko@wonko.info
©Copyright t 1992..2017+. Edition 25.150117
wonkosite
Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.1 Australia License
.