0% found this document useful (0 votes)
14 views40 pages

Pascal F

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views40 pages

Pascal F

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

The Pascal Programming Language

(with material from tutorialspoint.com)

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

 notable systems in Pascal


 Skype
 TeX
 embedded systems
3
History
 developed by Niklaus Wirth in the early 1970s
 developed for teaching programming with a general-purpose, high-level
language
 named for Blaise Pascal, French mathematician and pioneer in computer
development
 Algol-based
 Algol-60 is a subset of Pascal
 block structure
 used in early Mac development
 historically cited as
 easy to learn
 structured
 producing transparent, efficient, reliable programs
 able to compile across multiple computer platforms
4
Features of Pascal
 strongly typed
 extensive error checking
 arrays, records, files, and sets
 highly structured
 supports object-oriented programming

Source: xkcd.com/571 5
Hello, world!
program HelloWorld (output);

{ main program }
begin
writeln ('Hello, World!');
end.

 heading, declaration, execution parts


 { } comments
 writeln – with newline
 program ends with .

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

 name1, name2 : type;

 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.

Please enter your first name: Christopher


Please enter your last name: Wren

***Welcome, Christopher Wren


13
Example Program
program Circumference (input, output);

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;

writeln ('The circumference is ', circ:7:2);


end.

Enter the radius of the circle: 2.70


The circumference is 16.96

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);

c := a mod b; Line 1 – Value of c is 31


writeln ('Line 5 – Value of c is ', c); Line 2 – Value of c is 11
Line 3 – Value of c is 210
c := a div b; Line 4 – Value of d is 2.10
writeln ('Line 6 – Value of c is ', c); Line 5 – Value of c is 1
end. Line 6 – Value of c is 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

 different from above

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

 aggregate of like types


 contiguous memory
 examples

 different types of subscripts allowed

 packed arrays store data, such as chars, side by side instead of


along the default 4-byte boundary 35
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

You might also like