Saturday, March 19, 2011

Variables and Constants, Input and Output, Data Types, Assignment Statements


The understanding and the feelings are moulded by intercourse; the understanding and feelings are corrupted by intercourse. Thus good or bad society improves or corrupts them. It is, then, all-important to know how to choose in order to improve and not to corrupt them; and we cannot make this choice, if they be not already improved and not corrupted. Thus a circle is formed, and those are fortunate who escape it. Blaise Pascal, Pensees, 1669


The reading, writing and modification of variables provides a core function of programming in a manner that is similar to the use variables in algebra. In Pascal a variable is any string of letters and numbers that begins with a letter. It is better programming practise to write a variable that has a meaningful name in context; the variable "InterestRate" makes reading a program easier than a variable named "Z". As the name indicates, variables are values whose value can be changed. In contrast, a constant is a value whose value does not change, for example the location of a configuration file. The same convention should be used when naming constants as variables, however the use of capitals is a good convention.

Variables may be of different types. The basic data types are integers (whole numbers), real numbers (fractional values), boleean values (true or false) and char (a single character), string (an array of char). Pascal uses strong types, meaning that mixing between different types is prohibited. Other programming languages use weak-typing meaning that a conversion to or from different types are required when mixed. Pascal also has static types, when means that type-checking is performed as part of compilation, rather than during execution. The alternative is dynamic typing - which surrenders safety for flexibility. When it comes to type, Pascal is strong, static and safe.

The following program illustrates the use of variables, constants and types.


program Circum;
const PI = 3.14149;
var diameter, circumference : real;
begin
Writeln('Enter the diameter of the circle');
Readln(diameter);
circumference := PI * diameter;
Writeln('The circle has a circumference of ', circumference)
end.


There are a number of things to notice about this program.

Firstly is the use of = and := for the constant 'PI' and the use of the variable 'circumference', respectively. In the first case the value of PI is equal to the value given. In the latter case, called an assignment operator, the variable on the left-hand side "becomes equal to" the value given on the right hand side.

Secondly, the constants and variables in the program are declared and the type of variables is stated.

Finally, there is basic input and output through the readln and writeln statements. Content from standard input (from keyboard to screen) is read by the program, stored as a variable, which is then used for a calculation, the result of which is sent as part of a statement to standard output (the screen).

Taken as a whole, this program covers a great deal of some basic features of a Pascal program! The program in compilation and operation can be carried out as follows.


lev@squirrel:~/programming/pascal$ fpc circum.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for i386
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for i386
Compiling circum.pas
Linking circum
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
10 lines compiled, 0.1 sec
lev@squirrel:~/programming/pascal$ ./circum
Enter the diameter of the circle
22
The circle has a circumference of 6.91127800000000E+001