Title: Pascal
1Pascal By Parth and Isaac.
2History of Pascal
- Pascal was invented by Dr.Niklaus Wirth (1971)
- Pascal was initially made as an extension for
ALGOL - Pascal was made to be an highly-structured
language, and it resembled ALGOL in many ways - Pascal was named after the 17th century French
philosopher and mathematician who was working
mechanical digital computers - Pascal was able to produce an intermediate code
that would be able to compile to an executable
code, ALGOL could not do this - Pascal became standard through universities in
the early 1980s - Borland released Turbo Pascal which was able to
compile thousands lines of code in a minute - When the Macintosh came out, Pascal was used as
the preeminent structure programming language - Pascal ultimately came to fall when Windows
popularized the object-orientation language C
3- Variables and constants
- Pascal has a section called var reserved for
variables. Pascal is a highly structured language
and has a strict type-checking. - The basic data types in Pascal are integer, real,
char, boolean, string. The other data types are
byte, shortint, longint, word - Integer Whole numbers from -32768 to 32767
- Byte The integers from 0 to 255
- Real Floating point numbers from 1E-38 to
1E38 - Boolean Can only have the value TRUE or FALSE
- Char Any character in the ASCII character set
- String up to 255 characters
- Shortint The integers from -128 to 127
- Word The integers from 0 to 65535
- Longint The integers from -2147483648 to
2147483647
4Loops There are 3 types of loops which are the
for loop, while loop and repeat until loop. For
loop The for loop uses a loop counter variable,
which it adds 1 to each time, to loop from a
first number to a last number. Its syntax is
for counter_variable initial_value to
ending_value do begin statements end. program
Loopsvari Integerbeginfor i 1 to 10
doWriteln ('Hello')end. Another variant of
for loop is when we want to have successive
values of control variables decreasing from a
higher value to a lower value. Its syntax is for
counter_variable final_value downto
initial_value do begin statements end. While
loop The while loop repeats while a condition is
true. The condition is tested at the top of the
loop and not at any time while the loop is
running as the name suggests. A while loop does
not need a loop variable but if you want to use
one then you must initialize its value before
entering the loop. Its syntax is while
(condition_is_true) begin statements end
5program Loopsvari integerbegini
0while (i lt 10)begini i 1 writeln
('Hello')end end. Repeat-until loop The
repeat until loop is like the while loop except
that it tests the condition at the bottom of the
loop. The condition in the repeat-until loop is
evaluated after the execution of statements
instead of before, granting at least one
execution of statement even if condition is
becomes false. Also the loop does not have a
begin and an end. Its syntax is repeat
statements until (condition_is_true) program
Loopsvari integerbegini 0repeati
i 1 writeln ('Hello')until (i 10)end.
6Selection if statement
- The if statement allows you to branch an Boolean
operation. There are several branching formats
the one-way, two-way and multi-way. - Note If the if statement only accepts one
statement then you must use a begin and end to
enclose the statement.
7Selection if statement examples
One-way
if BooleanExpression then StatementIfTrue
Two-way
if BooleanExpression then StatementIfTrueelse
StatementIfFalse
Multi-way
if Condition1 then Statement1else if
Condition2 then Statement2 else
Statement3
8Selection case statement
- Similar to the if statement is the case
statement. The case statement is an variation of
the if statement, its conditions or Boolean
expression is more specific rather than general.
General Form
case selector of List1
Statement1 List2 Statement2
... Listn Statementn
otherwise Statement end
9Procedures and functions Unlike other programming
languages like c, pascal has both procedures
and functions. Procedure Procedures are also
called sub-programs and are used if a certain
function needs to be done again and again. The
procedure can have its own local variables,
starts off with begin and ends with end and can
have parameters.. The procedures reduce the size
of the program, makes the program neat, avoids
repeating lines of code in a program and
increased the debugging efficiency. The Syntax
for procedure is procedure name_of_procedure
(variable_name type) If the arguments needs to
be passed by reference, var is used before the
variable name. The syntax is procedure name (var
variable_name type) The following is a sample
program procedure Name (a, b integer var c, d
integer)beginc 3 a 5endbeginalpha
1 gamma 50 delta 30Name (alpha, 2,
gamma, delta)end.
10Functions Functions work the same way as
procedures, but they always return a single value
to the main program through its own name. Its
syntax is function name_of_function
(parameter_list) return_type Functions are
called in the main program by using them in
expressions e.g. a name (5) 3 In a
function, the return value is set by assigning a
value to the function identifier. e.g. name 5
Here is a sample program program
findbigger function bigger (a, b integer)
integer begin if (agtb) then bigger a
else bigger b end begin writeln ('The
bigger number is ',bigger(3,2)) end. Pascal
also has recursion for procedures and fucntions.
Recursion means allowing a function or procedure
to call itself. It keeps calling itself until
some limit is reached.
11The following is a sample program program
calc_factorial function factorial (num
integer) integer begin if num 1 then
factorial 1 else factorial factorial
(num-1) num end begin writeln ('6! is
',factorial(6)) end.
12Arrays
- In Pascal arrays can be classified as
one-dimensional or multi-dimensional. - An array contains several storage spaces, all of
the same type. Each storage space is referred to
as an subscript. - Arrays are useful because you can store a lot of
data of the same branch without declaring an
individual variable for each value. They work
very well with loops, because the index can be
used as the subscript.
13Arrays types
One-dimensional
type typename array
enumerated_type of another_data_type
Multi-dimensional
type datatype array enum_type1,
enum_type2 of datatype
14Program Structure
- Program structure in Pascal have to be in a
sequential order, but parts of that may be
omitted. The program requires a title, and it
needs to have a begin and an end. The variables
and constant are declared before the program
begins. - The text to be commented must be enclosed with (
and )
15Program Structure - Outline
PROGRAM ProgramName (FileList)CONST(
Constant declarations )TYPE( Type
declarations )VAR( Variable declarations
)( Subprogram definitions )BEGIN(
Executable statements )END.