Welcome to Pascal Programming
Welcome to Pascal Programming
Whole
integer 3, 104, 0, -9
numbers
(Floating point
real Real numbers 4.0, -0.08, 48.6, 2.0E4
numbers)
declaration of constants;
declaration of variables;
begin
program statements;
readln;
end.
Declaration and Initialization of
Constants and Variables
Recall: Variables and Constants MUST have
meaningful names.
We declare and initialize constants and variables in
Pascal as using these rules:
1. Constants are always declared before variables.
2. Constants are initialize upon declaration using
equal sign (=)
3. Variables are initialized after begin using colon
equal (:=)
Declaration and Initialization of
Constants and Variables
Structure Example
program programname; program areaofcircle;
const const
constant= value; pi= 3.14;
var var
variable1, variable2: data type; radius, area: real;
variable3, variable4: data type; count, sum: integer;
begin begin
variable1:= value; radius:= 0.0;
Program Statements
Pascal Programs have four (4) main types of
statement. They are:
1. Input statements
2. Assignment statements
3. Output statements.
4. Comments
Input Statements
• In Pascal we use readln(variable); for single
input and readln(variable, variable ...); for
multiple. When the computer see readln, it
pause and waits for a user inputted value.
• After the value is typed and the ENTER key is
pressed, the value will be stored in the
variable following readln.
• If multiple input is required a space is used to
separate the values followed by ENTER.
Assignment Statements
• An assignment statement is used to store a
value in a variable.
• An assignment statement is written as
follows:
left value := right value
The right value is a literal or an expression that
evaluates to a value that will be stored in the
left hand value which is a variable.
Output Statements
• In Pascal we use write and writeln to achieve
output. They are used to output strings,
variable values or a combination of both
separated by a comma (,).
Pascal Code User sees
writeln(‘I am ok’); I am ok
write(‘My name is ’, name); My name is Jamhi
write(‘My name is ’, name,‘ . I am ’, age); My name is Jamhi. I am 30
writeln;
Difference between write and writeln
• A write statement outputs information to the screen
and positions the cursor at the end of the output.
• A writeln statement outputs information to the
screen and positions the cursor on a new line below
the output.
Pascal code User sees
write(‘I am great’); I am great_
writeln(‘I am great’); I am great
_
Comments
• Comments are used for internal documentation.
That is instruction written to explain what is being
done in a program.
• In Pascal comments, statements are enclosed in
curly brackets or curve brackets and stars eg.
{comment statement}
(* This is also a comment*)
//double solidus – used for single line comments
• Anything appearing between {} or (**) or follow //
is ignored by the compiler.
Example program segment
Code
write ('Enter the radius: '); //Prompts user for radius
readln (radius);
area:=pi*radius*radius; {finds the area}
writeln('The area of the circle with radius ', radius:3:2, ‘ is ’ , area:3:2);
(*Statement outputting original value of radius and area with decimal
controls*)