1. James Tam
Getting Started With Pascal
Programming
How are computer programs created
What is the basic structure of a Pascal Program
Variables in Pascal
Performing input and output with Pascal
Common programming errors
Useful mathematical functions in Pascal
2. James Tam
What You Know: A Model For Creating Computer
Software
Specify the problem
Develop a design
Implement the design
Maintain the design
3. James Tam
Implement The Design
Involves the writing of the program in a programming language
e.g., Pascal, Java, C, C++ etc. etc.
This program must then be translated into a machine
understandable form.
4. James Tam
Translators
Convert computer programs to machine language
Types
1) Interpreters
• Translate the program as it's executed.
2) Compilers
• Translate the program before it's executed.
5. James Tam
Compiling Programs: Basic View
anything.p
Pascal
program
gpc
Pascal
compiler
input a.out
Machine
language
program
output
6. James Tam
Compiling Programs On Different Operating
Systems
Pascal
program
Solaris
compiler
a.out (Solaris)
AmigaDOS
compiler
a.out (AmigaDOS)
Windows
compiler
a.out (Windows)
7. James Tam
Basic Structure Of Pascal Programs
Program documentation
Program name (input and output operations);
Header
const
var
:
Declaration
s
begin
:
end.
Statements
8. James Tam
Details Of The Parts Of A Pascal Program
Headers
• Program documentation
- Version number, date of last modification, what does the program do etc.
- Comments for the reader of the program (and not the computer)
(* Marks the beginning of the documentation
*) Marks the end of the documentation
• Program heading
- Name of program, input and/or output operations performed by the program
• Example
(*
* Tax-It v1.0: This program will electronically calculate your tax return.
*)
program taxIt (input, output);
9. James Tam
Details Of The Parts Of A Pascal Program (2)
Declarations
• List of constants and variables
• More to come later in this section of notes
Statements
• The instructions in the program that actually gets stuff done
• They tell the computer what to do as the program is running
• Each statement is separated by a semicolon ";"
• Much more to come later in the course
10. James Tam
The Smallest Pascal Program
(*
* Smallest.p:
* The smallest Pascal program that will still compile written by James Tam
* v1.0.
*)
program smallest;
begin
end.
Note: The name "smallest" should match the filename "smallest.p". You can find an online version
of this program in the Unix file system under /home/231/examples/intro/smallest.p (the compiled
version is called "smallest").
11. James Tam
Creating And Compiling Programs: On The
Computer Science Network
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Hand-written or hand-drawn
To begin creating the Pascal program, in
Unix type "XEmacs filename.p"
To compile the program, in Unix
type "gpc filename.p"
To run the program, in Unix
type "./a.out"
12. James Tam
Variables
Set aside a location in memory
Used to store information (temporary)
Types:
• integer – whole numbers
• real – whole numbers and fractions
- Can't start or end with a decimal
• char – alphabetic, numeric and miscellaneous symbols
• boolean – true or false values
Usage:
• Declaration
• Accessing or assigning values to the variables
13. James Tam
Declaring Variables
Sets aside memory
Memory locations addressed through the name
Naming conventions
• Should be meaningful
• Any combination of letters, numbers or underscore (can't begin with a number and
shouldn't begin with an underscore)
• Can't be a reserved word e.g., program, begin, end (see Appendix B)
• Avoid using words with an existing meaning e.g., integer, real, boolean, write, writeln,
read, readln
• Avoid distinguishing variable names only by case
• For variable names composed of multiple words separate each word by capitalizing the
first letter of each word (save for the first word) or by using an underscore.
• Okay:
- tax_rate
- firstName
• Not Okay
- 1abc
- x
- test.msg
- good-day
14. James Tam
Declaring Variables (2)
Occurs in the variable declaration ("var") section
i.e.,
var
name of first variable, name of second variable…: type of variables;
e.g.,
var
height, weight: real;
age: integer;
15. James Tam
Accessing And Assigning Values To Variables
Accessing
•Can be done by referring to the name of the variable
•Syntax:
name
•Example:
num
16. James Tam
Accessing And Assigning Values To Variables (2)
Assignment
• Performed via the assignment operator :=
• Usage:
- Destination := Source;1
• Example:
- x := 5;
- x:= y;
- interest := principle * rate;
- character := 'a';
• Avoid assigning mixed types
e.g.,
var
num1: integer;
num2: real;
begin
num1 = 12;
num2 = 12.5;
num2 := num1;
1 The source can be any expression (constant, variable or formula)
num1 := num2;
Not allowed!
17. James Tam
Named Constants
A memory location that is assigned a value that cannot be changed
Occurs in the constant declaration ("const") section
The naming conventions for choosing variable names also applies
to constants but constants should be all UPPER CASE.
Syntax:
const
NAME OF FIRST CONSTANT = value of first constant;
NAME OF SECOND CONSTANT = value of second constant;
etc.
18. James Tam
Named Constants (2)
Examples:
const
TAXRATE = 0.25;
SAMPLESIZE = 1000;
YES = True;
NO = False;
19. James Tam
Purpose of Named Constants
1) Makes the program easier to understand
e.g.,
begin
population_change := (0.1758 – 0.1257) * current_population;
Vs.
const
BIRTHRATE = 0.1758;
DEATHRATE = 0.1257;
begin
population_change := (BIRTHRATE - DEATHRATE) * current_population;
Magic Numbers
(avoid!)
20. James Tam
Purpose of Named Constants
2) Makes the program easier to maintain
If the constant is referred to several times throughout the
program.
const
BIRTHRATE = 0.1758;
DEATHRATE = 0.1257;
begin
BIRTHRATE
BIRTHRATE
DEATHRATE DEATHRATE BIRTHRATE
BIRTHRATE BIRTHRATE
BIRTHRATE
21. James Tam
Output
Displaying information onscreen
Done via the write and writeln statements
Syntax (either write or writeln):
write ('text message');
or
writeln('text message');
write(name of variable or constant);
or
writeln (name of variable or constant);
write('message', name of variable, 'message'…);
or
writeln('message', name of variable, 'message'…);
22. James Tam
Output (2)
Examples:
var
num : integer;
begin
num := 10;
writeln('line1');
write('line2A');
writeln('line2B');
writeln(num);
writeln('num=',num);
23. James Tam
Formatting Output
Automatic formatting of output
•Field width: The computer will insert enough spaces to ensure
that the information can be displayed.
•Decimal places: For real numbers the data will be displayed in
exponential form.
Manually formatting of output:
Syntax:
• write or writeln (data: Field width for data: Number decimal places for
data);
24. James Tam
Formatting Output (2)
Examples
var
num : real;
begin
num := 12.34;
writeln(num);
writeln(num:5:2);
25. James Tam
Formatting Output (3)
If the field width doesn’t match the actual size of the field
• Field width too small – extra spaces will be added for numerical variables
but not for other types of data.
• Examples:
num := 123456;
writeln(num:3);
writeln('123456':3);
• Field width too large – the data will be right justified (extra spaces will
be put in front of the data).
• Examples:
num := 123;
writeln(num:6);
Writeln('123':6);
26. James Tam
Formatting Output (4)
If the number of decimal places doesn’t match the actual number
of decimal places.
• Set number of decimal places less than the actual number of decimal places
– number will be rounded up.
• Example:
num1 := 123.4567
writeln (num1:6:2);
• Set number of decimal places greater than the actual number of decimal
places – number will be padded with zeros.
• Example:
num1 := 123.4567;
writeln(num1:6:6);
27. James Tam
Formatting Output: A Larger Example
For the complete program and executable look under
/home/231/examples/intro/out1.p (out1 for the compiled version)
program out1;
var
num1 : integer;
num2 : real;
begin
num1 := 123;
num2 := 123.456;
writeln('Auto formatted by Pascal', num1, num2);
writeln('Manual format':13, num1:3, num2:7:3);
writeln('Manual not enough':13, num1:2, num2:6:3);
writeln('Manual too much':16, num1:4, num2:8:4);
end.
28. James Tam
Input
The computer program getting information from the user
Done via the read and readln statements
Syntax:
(single input)
read (name of variable);
or
readln (name of variable);
(multiple inputs)
read (nv1, nv2…);
or
readln (nv2, nv3…);
30. James Tam
Input: Read Vs. Readln
Both:
• Reads each value inputted and matches it to the corresponding variable.
Read
• If the user inputs additional values they will remain
Readln
• Any additional values inputted will be discarded
31. James Tam
Input: Read Vs. Readln (An example)
For the complete version of this program look in Unix under:
/home/231/examples/intro/read1.p (or read1 and read2 for the compiled
version)
e.g., read1.p
write('Input some integers making sure to separate each one with a space ');
write('or a new line: ');
read (num1, num2);
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
read(num3, num4);
32. James Tam
Input: Read Vs. Readln (An example (2))
For the complete version of this program look in Unix under:
/home/231/examples/intro/read2.p (or read2 for the compiled version)
e.g., read2.p
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
readln (num1, num2);
write('Input some integers making sure to separate each one with a space ');
write('or a newline: ');
readln(num3, num4);
33. James Tam
Extra Uses Of Readln
To filter out extraneous input
As an input prompt
e.g.,
writeln('To continue press return');
readln;
35. James Tam
Syntax/Compile Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Syntax error:
No executable
(a.out) produced.
36. James Tam
Runtime Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Executing a.out
Runtime error
(execution stops)
37. James Tam
Logic Errors
anything.p
(Unix file)
Pascal
program
Flowchart or
pseudo-code
(on paper)
High level
algorithm
XEmacs
Text editor
gpc
Pascal
compiler
Machine language
program
a.out (Unix
file)
Executing a.out
Program finishes
execution but may
result in incorrect
result
38. James Tam
Some Useful Functions
See also Appendix D in Pascal Programming and Problem
Solving by Leestma S. and Nyhoff L.
Name Description Input type Type of result Example
abs absolute value integer integer abs(-2) = 2
real real abs(-2.2) = 2.2
round rounding real integer round(2.6) = 3
trunc truncation real integer trunc(2.6) = 2
sqr squaring integer integer sqr(2) = 4
real real sqr(1.1) = 1.21
sqrt square root integer real sqrt(4) = 2.00
or
real
39. James Tam
Summary
What is involved in creating and running computer programs
• Typing in programs with a text editor
• Translating the computer program into machine language
What are the fundamental parts of a Pascal program
What are the basic types of variables employed in Pascal and how are they
used
How to output information with the write and writeln statements
Getting information from the user through the read and readln statements
What are the different types of programming errors
How are some common mathematical functions performed in Pascal
Editor's Notes
#12:Real – complete – must include a fractional and a whole number portion e.g., 82.1 not 82.