Introduction To Pascal
Introduction To Pascal
PASCAL PROGRAMMING
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
• Usage:
• Declaration
• Accessing or assigning values to the variables
DECLARING VARIABLES
• 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
• Examples:
• const
• TAXRATE = 0.25;
• SAMPLESIZE = 1000;
• YES = True;
• NO = False;
PURPOSE OF NAMED CONSTANTS
• Vs.
• const
• BIRTHRATE = 0.1758;
Magic Numbers
(avoid!)
• DEATHRATE = 0.1257;
• begin
• population_change := (BIRTHRATE - DEATHRATE) * current_population;
PURPOSE OF NAMED CONSTANTS
Examples:
var
num : integer;
begin
num := 10;
writeln('line1');
write('line2A');
writeln('line2B');
writeln(num);
writeln('num=',num);
FORMATTING OUTPUT
•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)
• 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
• (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