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
WHAT YOU KNOW: A MODEL FOR
CREATING COMPUTER SOFTWARE
• Specify the problem
• Develop a design
• Implement the design
• Maintain the design
COMPILING PROGRAMS: BASIC
VIEW
Pascal Machine
program language
program
Pascal
compiler
anything.p input gpc output a.out
BASIC STRUCTURE OF PASCAL
PROGRAMS
Header
Program documentation
Program name (input and output operations);
Declarations
const
var
:
Statements
begin
:
end.
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);
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 ";"
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").
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
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
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;
ACCESSING AND ASSIGNING
VALUES TO VARIABLES
• Accessing
• Can be done by referring to the name of the variable
• Syntax:
name
• Example:
num
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;
Not allowed!
num2 = 12.5;
num2 := num1;
num1 := num2;
1 The source can be any expression (constant, variable or formula)
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.
NAMED CONSTANTS (2)
• Examples:
• const
• TAXRATE = 0.25;
• SAMPLESIZE = 1000;
• YES = True;
• NO = False;
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;
Magic Numbers
(avoid!)
• DEATHRATE = 0.1257;
• begin
• population_change := (BIRTHRATE - DEATHRATE) * current_population;
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
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'…);
OUTPUT (2)
Examples:
var
num : integer;
begin
num := 10;
writeln('line1');
write('line2A');
writeln('line2B');
writeln(num);
writeln('num=',num);
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);
FORMATTING OUTPUT (2)
•Examples
var
num : real;
begin
num := 12.34;
writeln(num);
writeln(num:5:2);
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);
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);
FORMATTING OUTPUT: A LARGER
EXAMPLE
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.
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…);
INPUT (2)
• Examples:
• var
• num1, num2 : integer
• begin
• read (num1);
• read (num2);
• read (num1, num2);
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
INPUT: READ VS. READLN (AN
EXAMPLE)
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);
INPUT: READ VS. READLN (AN
EXAMPLE (2))
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);
EXTRA USES OF READLN
• To filter out extraneous input
• As an input prompt
• e.g.,
• writeln('To continue press return');
• readln;
SUMMARY
• What is involved in creating and running computer programs
• Typing in programs with a text editor
• 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