Pascal F
Pascal F
1
Overview
Background & History
Features
Hello, world!
General Syntax
Variables/Data Types
Operators
Conditional Statements
Loops
Functions and Procedures
Arrays and Records
2
Why Pascal?
well-structured, strongly typed
explicit pass by value, pass by reference
imperative, object-oriented
easy to learn
originally developed as a learning language
surged in popularity in the 1980s
Source: xkcd.com/571 5
Hello, world!
program HelloWorld (output);
{ main program }
begin
writeln ('Hello, World!');
end.
6
General Syntax
comments
{}
{* *} for multiline comments
{* this is a
multiline comment *}
case insensitivity
x and X are the same variable
reserved words: begin, Begin, and BEGIN all the same
7
General Syntax
reserved words
8
Variables
var keyword
beginning of variable declarations
before begin/end block
names
letters or digits beginning with a letter
examples
x : integer;
r : real = 3.77;
9
Data Types
10
Data Types
constants
before var section
const
DAYS_IN_WEEK = 7;
NAME = 'Maria';
enumerated types
order significant
type
COLORS = (red, orange, yellow, green, blue, indigo, violet);
MONTHS = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
11
Data Types
subranges
subset of type within a certain range
grades on a test: 0..100
can appear in any section
type
summer = (Jun..Sep);
var
gr : 1..100;
user-defined types
type
days = integer;
var
d : days;
12
Example Program
program Welcome (input, output);
const
intro = '***';
type
name = string;
var
firstname, lastname : name;
begin
write ('Please enter your first name: ');
readln (firstname); writeln (firstname);
write ('Please enter your last name: ');
readln (lastname); writeln (lastname);
writeln;
writeln (intro, 'Welcome, ', firstname, ' ', lastname);
end.
const
PI = 3.14159;
var
radius, diameter, circ: real;
begin
write ('Enter the radius of the circle: ');
readln (radius); writeln (radius:4:2);
diameter := 2 * radius;
circ := PI * diameter;
14
Operators
program calculator (input, output);
var
a, b, c: integer;
d : real;
begin
a := 21;
b := 10;
c := a + b;
writeln ('Line 1 – Value of c is ', c);
c := a - b;
writeln ('Line 2 – Value of c is ', c);
c := a * b;
writeln ('Line 3 – Value of c is ', c);
d := a / b;
writeln ('Line 4 – Value of d is ', d:3:2);
15
Relational Operators
16
Logical Operators
17
Operator Precedence
18
Conditional Statements
if-then
if-then-else
19
Conditional Statements
20
Conditional Statements
use begin/end blocks, if necessary
21
Case Statements
22
Loops
23
Loops
while-do
break
continue
24
Loops
while-do
25
Loops
for-do
26
Loops
repeat-until
27
Loops
nested loops
28
Functions and Procedures
Pascal has explicit differentiation between functions and
procedures
different reserved words
functions must return a value
procedures do not return a value
recursion allowed
29
Functions
please don’t write code formatted like this
30
Procedures
please don’t write code formatted like this, either
31
Parameter Passing
call by value and call by reference
explicitly differentiated through var keyword
32
Parameter Passing: Call by Value
33
Parameter Passing: Call by Reference
34
Arrays
example
36
Records
aggregate with differing types
must use type declaration
example
37
Records
38
Records
39
Other Topics
pointers
sets
variants
like unions in C/C++
strings
file I/O
memory management
classes
40