..:: A guide for You who wanna be a programming master!! ::..

Menu
Basics
Tips 'n Tricks

---------------------
Home
---------------------
History
---------------------
Program Structure
---------------------
Compilers
---------------------


Materials :
  1. Basics
  2. Input-output
  3. Expressions
  4. Procedures and Functions


Identifiers

Identifiers are names that allow you to reference stored values, such as variables and constants. Also, every program and unit must be named by an identifier.

Rules for identifiers:

  • Must begin with a letter from the English alphabet.
  • Can be followed by alphanumeric characters (alphabetic characters and numerals) and possibly the underscore (_).
  • May not contain certain special characters, many of which have special meanings in Pascal.
    ~ ! @ # $ % ^ & * ( ) + ` - = { } [ ] : " ; ' < > ? , . / |
Different implementations of Pascal differ in their rules on special characters. Note that the underscore character (_) is usually allowed.

Several identifiers are reserved in Pascal as syntactical elements. You are not allowed to use these for your identifiers. These include but are not limited to:

and array begin case const div do downto else end file for forward function goto if in label mod nil not of or packed procedure program record repeat set then to type until var while with

Modern Pascal compilers ship with much functionality in the API (Application Programming Interfaces). For example, there may be one unit for handling graphics (e.g. drawing lines) and another for mathematics. Unlike newer languages such as C# and Java, Pascal does not provide a classification system for identifiers in the form of namespaces. So each unit that you use may define some identifiers (say DrawLine) which you can no longer use. Pascal includes a system unit which is automatically used by all programs. This provides baseline functionality such as rounding to integer and calculating logarithms. The system unit varies among compilers, so check your documentation.

Pascal is not case sensitive! (It was created in the days when all-uppercase computers were common.) MyProgram, MYPROGRAM, and mYpRoGrAm are equivalent. But for readability purposes, it is a good idea to use meaningful capitalization. Most programmers will be on the safe side by never using two capitalizations of the same identifiers for different purposes, regardless of whether or not the language they're using is case-sensitive. This reduces confusion and increases productivity.

Identifiers can be any length, but some Pascal compilers will only look at the first several characters. One usually does not push the rules with extremely long identifiers or loads of special characters, since it makes the program harder to type for the programmer. Also, since most programmers work with many different languages, each with different rules about special characters and case-sensitivity, it is usually best to stick with alphanumeric characters and the underscore character.

back to top

Constants

Constants are referenced by identifiers, and can be assigned one value at the beginning of the program. The value stored in a constant cannot be changed.

Constants are defined in the constant section of the program:

const
  Identifier1 = value;
  Identifier2 = value;
  Identifier3 = value;

For example, let's define some constants of various data types: strings, characters, integers, reals, and Booleans. These data types will be further explained in the next section.

const
  Name = 'Tao Yue';
  FirstLetter = 'a';
  Year = 1997;
  pi = 3.1415926535897932;
  UsingNCSAMosaic = TRUE;

Note that in Pascal, characters are enclosed in single quotes, or apostrophes (')! This contrasts with newer languages which often use or allow double quotes or Heredoc notation. Standard Pascal does not use or allow double quotes to mark characters or strings.

Constants are useful for defining a value which is used throughout your program but may change in the future. Instead of changing every instance of the value, you can change just the constant definition.

Typed constants force a constant to be of a particular data type. For example,

const
  a : real = 12;

would yield an identifier a which contains a real value 12.0 instead of the integer value 12.

back to top

Variables and Data Types

Variables are similar to constants, but their values can be changed as the program runs. Variables must first be declared in Pascal before they can be used:

var
  IdentifierList1 : DataType1;
  IdentifierList2 : DataType2;
  IdentifierList3 : DataType3;
  ...

IdentifierList is a series of identifiers, separated by commas (,). All identifiers in the list are declared as being of the same data type.

The basic data types in Pascal include:

  • integer
  • real
  • char
  • Boolean
Standard Pascal does not make provision for the string data type, but most modern compilers do. Experienced Pascal programmers also use pointers for dynamic memory allocation, objects for object-oriented programming, and many others, but this gets you started.

More information on Pascal data types:

  • The integer data type can contain integers from -32768 to 32767. This is the signed range that can be stored in a 16-bit word, and is a legacy of the era when 16-bit CPUs were common. For backward compatibility purposes, a 32-bit signed integer is a longint and can hold a much greater range of values.
  • The real data type has a range from 3.4x10-38 to 3.4x1038, in addition to the same range on the negative side. Real values are stored inside the computer similarly to scientific notation, with a mantissa and exponent, with some complications. In Pascal, you can express real values in your code in either fixed-point notation or in scientific notation, with the character E separating the mantissa from the exponent. Thus,
          452.13 is the same as 4.5213e2
  • The char data type holds characters. Be sure to enclose them in single quotes, like so: 'a' 'B' '+' Standard Pascal uses 8-bit characters, not 16-bits, so Unicode, which is used to represent all the world's language sets in one UNIfied CODE system, is not supported.
  • The Boolean data type can have only two values:
    TRUE and FALSE

An example of declaring several variables is:

var
  age, year, grade : integer;
  circumference : real;
  LetterGrade : char;
  DidYouFail : Boolean;

back to top

Assignment and Operations

Once you have declared a variable, you can store values in it. This is called assignment.

To assign a value to a variable, follow this syntax:

variable_name := expression;

Note that unlike other languages, whose assignment operator is just an equals sign, Pascal uses a colon followed by an equals sign, similarly to how it's done in most computer algebra systems.

The expression can either be a single value:

some_real := 385.385837;

or it can be an arithmetic sequence:

some_real := 37573.5 * 37593 + 385.8 / 367.1;

The arithmetic operators in Pascal are:

Operator Operation Operands Result
+ Addition or unary positive real or integer real or integer
- Subtraction or unary negative real or integer real or integer
* Multiplication real or integer real or integer
/ Real division real or integer real
div Integer division integer integer
mod Modulus (remainder division) integer integer

div and mod only work on integers. / works on both reals and integers but will always yield a real answer. The other operations work on both reals and integers. When mixing integers and reals, the result will always be a real since data loss would result otherwise. This is why Pascal uses two different operations for division and integer division. 7 / 2 = 3.5 (real), but 7 div 2 = 3 (and 7 mod 2 = 1 since that's the remainder).

Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types will convert to a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section:

var
  some_int : integer;
  some_real : real;

When the following block of statements executes,

some_int := 375;
some_real := some_int;

some_real will have a value of 375.0.

Changing one data type to another is referred to as typecasting. Modern Pascal compilers support explicit typecasting in the manner of C, with a slightly different syntax. However, typecasting is usually used in low-level situations and in connection with object-oriented programming, and a beginning programming student will not need to use it.

In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to make a value positive, but is typically left out since values default to positive.

Do not attempt to use two operators side by side, like in:

some_real := 37.5 * -2;

This may make perfect sense to you, since you're trying to multiply by negative-2. However, Pascal will be confused — it won't know whether to multiply or subtract. You can avoid this by using parentheses to clarify:

some_real := 37.5 * (-2);

The computer follows an order of operations similar to the one that you follow when you do arithmetic. Multiplication and division (* / div mod) come before addition and subtraction (+ -), and parentheses always take precedence. So, for example, the value of: 3.5*(2+3) will be 17.5.

Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of Boolean operations. Also, you should not perform arithmetic operations on characters.

back to top

Standard Functions

Pascal has several standard mathematical functions that you can utilize. For example, to find the value of sin of pi radians:

value := sin (3.1415926535897932);

Note that the sin function operates on angular measure stated in radians, as do all the trigonometric functions. If everything goes well, value should become 0.

Functions are called by using the function name followed by the argument(s) in parentheses. Standard Pascal functions include:

Function Description Argument type Return type
abs absolute value real or integer same as argument
arctan arctan in radians real or integer real
cos cosine of a radian measure real or integer real
exp e to the given power real or integer real
ln natural logarithm real or integer real
round round to nearest integer real integer
sin sin of a radian measure real or integer real
sqr square (power 2) real or integer same as argument
sqrt square root (power 1/2) real or integer real
trunc truncate (round down) real or integer integer

For ordinal data types (integer or char), where the allowable values have a distinct predecessor and successor, you can use these functions:

Function Description Argument type Return type
chr character with given ASCII value integer char
ord ordinal value integer or char integer
pred predecessor integer or char same as argument type
succ successor integer or char same as argument type

Real is not an ordinal data type! That's because it has no distinct successor or predecessor. What is the successor of 56.0? Is it 56.1, 56.01, 56.001, 56.0001?

However, for an integer 56, there is a distinct predecessor — 55 — and a distinct successor — 57.

The same is true of characters:

'b'
Successor: 'c'
Predecessor: 'a'

The above is not an exhaustive list, as modern Pascal compilers include thousands of functions for all sorts of purposes. Check your compiler documentation for more.

back to top

Displaying The Data

Now you know how to use variables and change their value. Ready for your first programming assignment?

But there's one small problem: you haven't yet learned how to display data to the screen! How are you going to know whether or not the program works if all that information is still stored in memory and not displayed on the screen?

So, to get you started, here's a snippet from the next few lessons. To display data, use:

writeln (argument_list);

The argument list is composed of either strings or variable names separated by commas. An example is:

writeln ('Sum = ', sum);

back to top

You can compile your work by pressing "F9" when in the IDE. Or if you wanna launch it directly after compiling, just press "Ctrl+F9"!!

..:: by_Zain Fathoni ::..
.: SMA N 1 Jember / XII.Rintisan :.
copyright © 2008
Adapted from www.tayoue.com