Professional Documents
Culture Documents
Programmimg Operation
Programmimg Operation
Introduction to Pascal
Programming Language
Arithmetic Expressions
Logical/Boolean Expression
Assignment Statement
Learning Outcomes
Upon completion of this lecture learners will
be able to:
convert mathematics expressions into Pascal
arithmetic expressions,
evaluate Pascal arithmetic expressions based on
operator precedence.
Page 2 of 22
Pascal Arithmetic
Unary
Syntax
<operator> <operand>
Example
-5
Binary
Syntax
Example
5 + 2
Page 3 of 22
example
result
Addition
5 + 2
Subtraction / unary
minus
5 - 2
-5
3
-5
Multiplication
5 * 2
10
Real Division
5.0 / 2
2.5
Div
Integer division
5 div 2
Mod
Modulo (Remainder)
5 mod 2
Page 4 of 22
Type of
First
Operand
Integer /
real
Integer / real
Integer /
real
Integer / real
Integer /
real
Integer / real
Integer /
real
Integer / real
Real
Div
Integer
Integer
Integer
Mod
Integer
Integer
integer
Page 5 of 22
Examples
Operation
6 + 2
6.2 + 2
6 - 2
6.0 - 2
6 * 2
result
8
8.2
4
4.0
12
6.0 * 2
12.0
6 / 2
3.0
7 div 2
7 mod 2
1
Page 6 of 22
Relational Operator
Operator Description
example
result
Equals to
5 = 2
false
>
Greater than
5 > 2
true
<
Less than
5 < 2
false
>=
Greater than or
equals to
<=
5 <= 2
false
<>
Not equals to
5 <> 2
true
true
Page 7 of 22
Boolean Operator
In some cases, we need to combine more than one
relational operation, for example:
In Math we write 0 marks 100
In Pascal we write
(marks >= 0) AND (marks <= 100)
Operator
example
result
NOT
NOT true
false
AND
false
OR
True OR False
True
Page 8 of 22
Truth Table
Operand2
AND
OR
True
True
True
True
True
False
False
True
False
True
False
True
False
False
False
False
Operand1
Page 9 of 22
Operator Precedence
Priority
1
Operator
NOT
Unary
+, -, OR
Left to right
Page 10 of 22
Arithmetic Evaluation
7 + 3 * 2
----6
------13
Page 11 of 22
.. Arithmetic Evaluation
7.0 + 3 DIV 2
------1
---------8.0
Page 12 of 22
.. Arithmetic Evaluation
9 - 4 / 2 MOD 2
----2.0
---------ERROR*
*MOD accepts integer operands only
Page 13 of 22
.. Arithmetic Evaluation
9 - 4 DIV 2 MOD 2
------2
---------0
-----------9
Page 14 of 22
Arithmetic Evaluation
5 > 6 - 2
----4
------TRUE
Page 15 of 22
Arithmetic Evaluation
5 > 6 AND 2 <= 3
-------TRUE
----------ERROR
Page 16 of 22
Arithmetic Evaluation
(5 > 6) AND (2 <= 3)
-------FALSE
-------TRUE
----------------FALSE
Page 17 of 22
Arithmetic Functions
Pascal provides predefined functions that perform
common operations
Have to include the unit/library before the var
section
uses math;
Syntax
Example
sqrt(49)
power (5, 2)
Page 18 of 22
.. Arithmetic Functions
Function
Description
Type of
Argument
Abs(x)
Type of
result
Example
Same
Abs(-9) = 9
Abs(-3.5) = 3.5
(non ve)
Integer/
real
Real
Sqrt(9) = 3.0
Sqrt(-3.5) = Error!!
Real
Integer
Round(3.4) = 3
Round (-3.5) = -4
Real
Integer
Trunc(-3.4) = -3
Integer
Random(-7) = Error!!
Page 19 of 22
.. Arithmetic Functions
Function
Type of
Argument
Type of
result
Example
Results
ceil(x)
Integer/
real
Integer
Ceil(9.2)
Ceil (-3.2)
10
-3
floor(x)
Integer/
Real
Integer
floor(9.9)
floor(-3.5)
9
-4
power(x,y)
Integer/
Real
same
Power(9,3)
Power(9.1,2)
729
82.81
Sqr(x)
Integer/
Real
same
Sqr(9)
Sqr(9.1)
81
82.81
Refer to:
http://www.freepascal.org/docs-html/rtl/math/index-5.html
Page 20 of 22
Assignment Statement
Exercise
Declare 3 variables of type string (StudentName1,
StudentName2, StudentName3 ) and 2 variables to
store 2 test results.
Your program should be able to calculate the
average value of the StudentName1,
StudentName2, StudentName3
Ask users to key in their names. Assign that 3
names into the first 3 variables. Ask from each
users for 2 test results.
Calculate and display the average of test results for
each student..
Page 22 of 22