Transcript Slides

Pascal
Jinbo Lin
Pascal
• Structured and Strong type language
• program HelloWorld;
• uses crt;
• var
x:string='Hello, World!';
• begin
•
writeln(x);
•
readkey;
• end.
History
• Pascal is a programming language that grew out from
ALGOL.
• Published by Dr. Niklaus Wirth in 1971.
• Designed as a teaching programming language.
• Later, Dr. Ken Bowles in UCSD adapt the Pascal
compiler to Apple II.
• By early 1980's, U.S. Educational Testing Service decide
to add a computer science AP Exam, Pascal was chosen.
• In 1999, C++ replaced Pascal, and Java replaced C++
soon after that.
Names, Binding, and Scopes
• Pascal is case insensitive.
• letters, digits, and _ are only characters allowed to make
identifiers.
• Different types of compiler has its own rules on the
length of identifier name.
• Static Binding
• All ALGOL based language are static scoping, which
includes Pascal.
Data Types
• Simple :
–
–
–
–
–
integer
real
character
boolean
user-defined type
• Structured:
–
–
–
–
array
record
file
set
• Pointer.
Data Types (Integer)
• Pascal has many ways to define those data types, and
each type may be vary on the range and the memory
size.
• Ten types of Integer:
–
–
–
–
byte : 0 to 255, 1 byte
smallint: -(2^15) to (2^15) -1, 2 bytes
longint: -(2^31) to (2^31)-1 , 4 bytes
ingeter, shortint, word, cardinal, qword, longword, int64.
• The most common way, integer, will be stored as either
smallint or longint.
Data Types (String)
• String:
– character array: a sequence of zero or more characters enclosed
by single quotes. (e.g. myString: pack array [1..20] of char)
– string: a sequence of characters with an optional size
specification. (e.g. myString: string)
– short string: a string type that with the specification of length (e.g.
myString: string[20])
– null terminated string: use a null value at the end of actual string
value to indicate the length (e.g. myString: pchar)
Expressions and Assignment Statements
• operator precedency rule is the same as other language
• Strict type checking
• Only two exception:
– implicit data type conversion within same group
– implicit data type conversion with int and real.
• Pascal Support Operator Overloading
Control Structure
• Selection
– if then
– if then else
– case
• Iteration
– for do
– while do
– repeat until
• key word: break, continue.
Control Structure Cont.
• Goto: statement that
provides an unconditional
jump from the goto to a
labeled statement in the
same function.
•
•
•
•
•
•
•
•
value of i: 1
value of i: 2
value of i: 3
value of i: 4
value of i: 5
value of i: 6
value of i: 8
value of i: 9
program example;
uses crt;
label 1;
var
i : integer;
begin
i := 1;
1:
repeat
if( i = 7) then
begin
i := i + 1;
goto 1;
end;
writeln('value of i: ', i);
i:= i +1;
until i = 10;
readkey;
end.
SubProgram
• Procedure vs Function
• header, local declaration,
body
• pass by reference vs. pass
by value
function name(argument(s): type1; argument(s): type2; ...): function_type;
local declarations;
begin
...
< statements >
...
name:= expression;
end;
Support of Object-Oriented Programming
• Object Pascal
• Object vs. Class
• Stack vs. Heap
type object-identifier = object
private
field1 : field-type;
field2 : field-type;
...
public
procedure proc1;
function f1(): function-type;
end;
Concurrency
• Concurrent Pascal, or Pascal FC
• Constructs that are removed:
– records
– goto statement and labels
– procedures as parameter
– packed arrays
– pointer types
– file types.
Exception Handling
• Try
• Except
• Finally
Readability
• Pros:
– Code is structured
– Simplicity: not flexible
– More English words than symbols
• Cons:
– Too many ways to define a data type like string and integer.
– use begin .. end instead of { .. }
• Conclusion: high readability.
Writability
• Pros:
– Case Insensitive
– Operator Overloading
– Abstract data types
• Cons:
– More English words than symbols
– Lack of flexibility
– Relatively less API available
• Conclusion: decent Writability
Reliability
• Pros:
– Strongly typed
– Support for exception handling
– Rich data types set
• Cons:
– Aliasing: pointer and pass by reference mode
• Conclusion: super reliable
Cost
• Software: two major IDE: Lazarus and Delphi, are free to
download online.
• Hardware: Lazarus is a cross-platform IDE that can run
on Windows, Linux and Mac.
• Overall: Pascal has high readability, decent writability,
and it is super reliable. The price is, Pascal is not
powerful.