Introduction
PASCAL is a programming language named after the 17th century
mathematician Blaise Pascal. The Pascal language
The Pascal language was originally designed in 1971 by Niklaus Wirth, professor at the Polytechnic of Zurich, Switzerland. Pascal was designed as a simplified version for educational purposes of the language Algol, which dates from 1960.
When Pascal was designed, many programming languages existed, but few were in widespread use: FORTRAN, C, Assembler, COBOL. The key idea of the new language was order, managed through a strong concept of data type, and requiring declarations and structured program controls. The language was also designed to be a teaching tool for students of programming classes.
Strict rules make it difficult for the programmer to write bad code! A program is a sequence of instructions which inform a computer of a required task.
Turbo Pascal was created from the Borland compiler, that first gave a IDE to develop programs.
Basic format of every Pascal program
Every Pascal program has the same essential format, which is
illustrated below,
program TITLE ( input, output ); begin program statements; program statement end.
Some valid keywords which implement Pascal are,
Integer Char Record Case Real If While With Else
In addition, Pascal is NOT case sensitive. That means Else and else are treated the same.
Comments
are inserted into Pascal programs by enclosing the comment
within { and } braces.
Pascal supports FOUR standard variable types, which are
:=
Addition Example
program Add (output); var number1, number2, result : integer; begin number1 := 10; number2 := 20; result := number1 + number2; writeln(number1, ' plus ', number2, ' is ', result ) end. Sample Program Output 10 plus 20 is 30 _
program DISPLAYVARIABLES (output); var number1 : integer; letter : char; money : real; begin number1 := 23; letter := 'W'; money := 23.73; writeln('number1 = ', number1 ); writeln('letter = ', letter ); writeln('money = ', money ) end.
The program that follows reads two numbers from the keyboard, assigns them to the specified variables, then prints them to the console screen.
program READDEMO (input, output); var numb1, numb2 : integer; begin writeln('Please enter two numbers separated by a space'); read( numb1 ); read( numb2 ); writeln; writeln('Numb1 is ', numb1 , ' Numb2 is ', numb2 ) end.
program IF_DEMO (input, output); {Program demonstrating IF THEN statement} var number, guess : integer; begin number := 2; writeln('Guess a number between 1 and 10'); readln( guess ); if number = guess then writeln('You guessed correctly. Good on you!'); if number <> guess then writeln('Sorry, you guessed wrong.') end.
And/OR/NOT
program AND_OR_NOT_DEMO (output); var a, b, c : integer; begin a := 5; b := 3; c := 99; if (a = 5) or (b > 2) then writeln('A'); if (a < 5) and (b > 2) then writeln('B'); if (a = 5) and (b = 2) then writeln('C'); if (c <> 6) or (b > 10) then writeln('D') else writeln('E'); if (b = 3) and (c = 99) then writeln('F'); if (a = 1) or (b = 2) then writeln('G'); if not( (a < 5) and (b > 2)) then writeln('H') end.
program CELSIUS_TABLE ( output ); var celsius : integer; fahrenheit : real; begin writeln('Degree''s Celsius Degree''s Fahrenheit'); for celsius := 1 to 20 do begin fahrenheit := ( 9 / 5 ) * celsius + 32; writeln( celsius:8, ' ',fahrenheit:16:2 ) end end.
case operator of '*' : result:= number1 * number2; '/' : result:= number1 / number2; '+' : result:= number1 + number2; '-' : result:= number1 - number2; otherwise invalid_operator := 1 end;
An array can be defined as a type, then a working variable created as follows,
type array_name = ARRAY [lower..upper] of data_type; var myarray : array_name; {this creates myarray which is of type array_name }
or by using a var statement as follows.
var myarray : ARRAY [1..100] of integer;
Lower and Upper define the boundaries for the array. Data_type is the type of variable which the array will store, eg, type int, char etc. A typical declaration follows,
Pascal uses three types of modules. The first two are called PROCEDURES, the other a FUNCTION.
Procedures help support structured program design, by allowing the independant development of modules. Procedures are essentially sub-programs.
Here is a sample function:
function Multiply2( number1, number2 : integer ) : integer; var Result : integer; begin Result := number1 * number2; Multiply2 := Result end;
Procedure which accepts parameters:
program ADD_NUMBERS (input, output); procedure CALC_ANSWER ( first, second : integer ); var result : integer; begin result := first + second; writeln('Answer is ', result ) end; var number1, number2 : integer; begin writeln('Please enter two numbers to add together'); readln( number1, number2 ); CALC_ANSWER( number1, number2) end.Pass by Reference
To force the procedure to use variable parameters, preceed the declaration of the variables (inside the parenthesis after the function name) with the keyword var.
This has the effect of using the original variables, rather than a copy of them.
program Variable_Parameters (output); procedure SWAP ( var value1, value2 : integer ); var temp : integer; begin temp := value1; value1 := value2; {value1 is actually number1} value2 := temp {value2 is actually number2} end; var number1, number2 : integer; begin number1 := 10; number2 := 33; writeln( 'Number1 = ', number1,' Number2 = ', number2 ); SWAP( number1, number2 ); writeln( 'Number1 = ', number1,' Number2 = ', number2 ) end.
The following portion of code shows how to define a record, then create a working variable to be of the same type.
TYPE studentname = packed array[1..20] of char; studentinfo = RECORD name : studentname; mark : integer END; VAR student1 : studentinfo;
The first portion defines the composition of the record identified as studentinfo. It consists of two parts (called fields).
Records with records
type date = RECORD day, month, year : integer END; time = RECORD hours, minutes, seconds : integer END; date_time = RECORD sdate : date; stime : time END;
This defines a record whose elements consist of two other previously declared records. The statement
var today : date_time;
declares a working variable called today, which has the same composition as the record date_time. The statements
today.sdate.day := 11; today.sdate.month := 2; today.sdate.year := 1985; today.stime.hours := 3; today.stime.minutes := 3; today.stime.seconds := 33;
A pointer provides an indirect means of accessing the value of a particular data item. I know cs355 class, let's look at an example..weeee....how exciting (I'm getting delirious.)
program pointers1( output ); type int_pointer = ^integer; var iptr : int_pointer; begin new( iptr ); iptr^ := 10; writeln('the value is ', iptr^); dispose( iptr ) end.
The line
type int_pointer = ^integer;
declares a new type of variable called int_pointer, which is a pointer (denoted by ^) to an integer.
The line
var iptr : int_pointer;
declares a working variable called iptr of type int_pointer.
Currently, there is no storage space allocated with iptr, which means you cannot use it till you associate some storage space to it.
The line
new( iptr );
creates a new dynamic variable (ie, its created when the program actually runs on the computer). The pointer variable iptr points to the location/address in memory of the storage space used to hold an integer value.
The line
dispose( iptr )
means deallocate (free up) the storage space allocated/associated with iptr, and return it to the computer system.
Message is then declared as the same type as STRING, ie, a packed array of characters, elements numbered one to eight.
PROGRAM DGSTRING (INPUT, OUTPUT); TYPE STRING = PACKED ARRAY [1..8] OF CHAR; VAR MESSAGE : STRING; BEGIN WRITELN('HELLO BRIAN.'); MESSAGE := '12345678'; WRITELN('THE MESSAGE IS ', MESSAGE) END.
Turbo Pascal, how-ever, allows an easier use of character
strings by providing a new keyword called STRING.
We'll just use standard Pascal for this week's assignment.