Title: Programming with pascal
1Programming with pascal
- 1. Numeric Expressions
- 2. Basic Input / Output Operations
- 3. Datatypes and Expressions
2Numeric Operators and Expressions
- Numeric oprators of standard Pascal
- (Muplication)
- / (Real division)
- DIV (Integer division)
- MOD (Integer remainder)
- (Addition)
- - (Substraction)
3Numeric Operators
- Hierarchy of applying and evaluating arithmrtic
operators -
- ( )
FIRST - / DIV MOD
- - LAST
4Numeric Expressions
- Numeric expressions A numeric expression is any
formulae that composed from these operators,
numeric constants and variables. - Examples of expressions
- 5 3 2 (result 6)
- 5 3 2 (result 13)
- 5 (3 2) (result 5)
-
5Numeric Operators and Expressions
- Examples of (numeric) expressions
- I ( I should be either numeric
constant or variable) - J
- I 2J
- 6 4 4 3 16 / 8
- 12 / (4 2)
- 12 DIV (4 2)
- 6 (4- 43 16) / 8
- 12 N1 8 DIV 3 ( N1 is supposed to be
integer variable or constant )
6- Hierarchy of applying and evaluating arithmrtic
operators -
- ( )
FIRST - / DIV MOD
- - LAST
7Basic Input/Output Operations
- There are four predefined procedures in Pascal
for reading and writing to and from a file - Output write, writeln
- Input read, readln
- The predefined procedure "write" has the general
form - write (expression1,expression2 ...)
-
- Expressioni is any (numeric) expressions or any
explicative message (as sequence of characters,
numbers or symbols) between apostrophes. - gt It writes the values of one or more
expressions and any message as it is to the
output file (which may also be the terminal).
8Basic Input/Output Operations
Program surface This program compute the
surface of a rectangle and display the
result Var width, length integer Var
surf integer begin width 8 Length 13
surf width , length Write(The surface is
, surf) end.
Program Power This program compute the power of
a number and display the result Var I, Rs
integer begin I 8 Rs I I
Write(Result , Rs) end.
9Basic Input/Output Operations
- The predefined procedure "read" has the general
form - read (variable1,variable2, ...)
- It reads values from the input file (which may
also be the keyboard) and assigns them to the
supplied variables. -
10Program surface This program compute the
surface of any rectangle and display the
result Var width, length integer Var
surf integer Begin Write(Please enter the
value of Width,width) readln(width) Write(Pl
ease enter the value of length,length)
readln(length) surf width length
Write(The surface is , surf) end.
Program Power This program compute the power of
any number and display the result Var I, Rs
integer begin Write(Please enter the value of
I, I) Readln(I) Rs I I Write(Result
, Rs) end.
11Basic Input/Output Operations
- The predefined procedures
- writeln (expression1,expression2,...)
- and
- readln (variable1,variable2,...)
- operate like write and read. In addition they
- generate a line feed after reading or writing.
12Data Type Properties
- As a data type the integer numbers consist of a
set of - - values ..., -2, -1, 0, 1, 2, ... - and a set
of operations for these values, (Like addition,
subtraction, multiplication, division,
remainder in division , predecessor, successor,
etc). - Whole numbers and their operations make up the
integer data type in Pascal. - Generally speaking, any data type consists of a
Set of Objects and a Class of Operations for
these objects.
13Data Type Properties
- Data types are divided into two classes
- - simple data types
- - structured data types
- Data Types in Pascal
Simple Structured Integer
arrays Boolean records Reals
sets Char files Pointer enumerate
14Standard Data Types
- In Pascal there are four simple data types, the
objects and operations of which are already
predefined. They are also called standard data
types. They are - - boolean
- - integer
- - real
- - char
15The Boolean Data Type
- It consists of the truth values
- - true (logically true)
- - false (logically false)
- There are two kinds of operations defined for
such boolean values - - logical operators
- - comparison operators
16The Boolean Data Type
- Each of the following logical operators yields a
boolean value. - - and (logical conjunction)- or (logical
disjunction)- not (logical negation) - Comparison operators are
- (equal) ltgt (not
equal) - lt (less or equal) lt (less)
- gt (greater) gt
(greater or equal) - Each of them yields a boolean value. It has been
defined that false lt true yields true.
(falselttrue yields true truegtfalse yields false
trueltgtfalse yields true)
17The Integer Data Type
- It includes a subset of all the positive and
negative numbers the size of this subset is
implementation dependent (e.g. from -32768 to
32767). - Arithmetic operators for integer objects are
- (addition) - (subtraction)
(multiplication) - div (integer division) mod (remainder of
integer division) - Examples 7 div 3 yields 2 7 mod 3 yields 1
-7 div 3 yields -2 - -7 mod 3 yields -1
- Furthermore there are comparison operators with
the usual meanings. - Examples 7 lt13 yields true
- 7 gt 13 yields true
- -7 ltgt 7 yields true
18The Integer Data Type
- The following standard functions are predefined
for integer objects - sqr ( I ) square of I
- abs ( I ) absolute value of I
- succ ( I ) successor of I, i.e. succ ( I ) I
1 - pred ( I ) predecessor of I, i.e. pred ( I )
I - 1 - odd ( I ) if I odd, then yields a boolean
value true, otherwise yelds a boolean value
false. - Examples
- sqr(3) yields 9 abs(3)
yields 3 - abs(-3) yields 3 succ(3) yields
4 - pred(3) yields 2 odd(5) yields
true - odd(10) yields false
19The Real Data Type
- Real stands for a subset of the real numbers.
- Real values can be represented in two different
ways in Pascal - the so-called fixed point notation
- (e.g. 13.275, 0.01 - "." is the decimal point)
- - the so-called floating point notation (e.g.
1.3275e1, 10.0e-3 - "e" stands for the exponent
of base 10.) - Samples of valid real constants
- 5.0, 1.123, 47e1, 4433e-2 99.43e-21
-
20The Real Data Type
- The arithmetic operators for the real data type
are - (addition) - (subtraction)
(multiplication) - / (division)
- Examples
- 2.7-5.4 yields -2.7
- 2.70.54e1 yields 8.1
- 30.0/8.0 yields 3.75
- Attention! The division operator for real numbers
is / , that for integers is div.
21The Real Data Type
- The comparison operators have their usual
meanings. - Examples
- 2.5e7 gt 2.5e6 yields true
- 2500.0 2.5e3 yields true
- The following predefined standard functions have
their usual mathematical meanings - sqr (square) abs (absolute value)
- sqrt (square root) exp (exponential
function) - In (natural logarithm) sin (sine)
- cos (cosine) arctan (arc tangent)
22The Real Data Type
- Examples
- sqrt(0.16) 0.4
- exp(1.0) 2.71828
- With the next two standard functions real values
can be converted to integer values - - round(R) R is rounded to the nearest
integer value - - trunc(R) truncates the decimal fraction of
R. - Examples
- round(21.32) 21
- round(3.5) 4
- round(-3.5) -4
- trunc(21.32) 21
- trunc(3.5) 3
23The Char Data Type
- This includes the set of all available
characters. A value of type char is represented
as a character enclosed in apostrophes. - Example 'A', '', '3
- Please note, the char value ' 3 ' and the
integer value 3 are not the same ! - 3 is an integer '3' is a printable character
- The only operators defined for char are the
comparison operators. - They yield results according to the alphabetical
order. - Examples
- 'B'lt'C yields true 'y'gt'z
yields false - '6'lt'7 yields true
24The Real Data Type
- Standard functions for the char data type
- Printable characters are coded as integers in the
computer. The most usual code for this is the
ASCII character set, which will be used in the
examples as well. - - ord(C) yields the code (i.e. integer value)
of C. - - The inverse function of ord is chr(I) which
maps an integer to the corresponding character.
- - succ(C) yields the successor of C
- - pred(C) yields the predecessor of C
- Examples
- ord('C) yields 81 ord('R') yields
82 succ('Q) yields 'R - chr(81) yields 'Q chr(82) yields 'R
pred('R') yields 'Q'