You are on page 1of 30

Operators

Basic types of operators


• Arithmetic operators
• Relational operators
• Logical operators
• String operators
Arithmetic operators
Operators Description Example Result
+ Add 5+5 10
- Substract 10-5 5
* Multiply 5*4 20
/ Divide 25/2 12.50
DIV Integer Division 20 DIV 3 6
Remainder of
Mod 20 Mod 6 2
division
Relational operators (Comparison
Operators )
Comparison operators compare two expressions and a return
Boolean value.
Operators Description Example Result
>  Greater than 10>8 True
<  Less than 10<8 False
Greater than or
>= 20>=10 True
equal to
Less than or equal
<= 10<=20 True
to
<>  Not Equal to 5<>4 True
= Equal to 5=7 False
Logical Operators
Logical operators combine two or more sets of conditional comparisons.

Operators Description Example Results


Negates the value of
NOT NOT(3=3) False
an expression
Evaluates to true if all (2<3) AND
AND True
expressions are true (6<7)
Evaluates to true if at
(2<3) OR
OR least one expression is True
(6<7)
true
EXERCISE

1. (2>3) AND (3>2) 4. (2>3) OR (3>2)


2. NOT (3>2) 5. NOT (5>4) AND (3<2)
3. (2<3) AND (5<4) OR (7>6) 6. (9-4<8) OR (8+3>5)
Operator precedence

Priority order Operator

1 NOT

2 * / DIV MOD AND

3 + - OR

4 = <> < <= > >=

The operators are always evaluated left to right


EXERCISE
Given that
A := 1; B := 2; C := 4;
What does X equal after each of the following
statements,
X := A / B / C; ________________
X := A + B / C; ________________
X := A * B * C; ________________
X := A * B - C; ________________
X := A + B + C; ________________
X := A / B * C; ________________
X := A * B / C; ________________
X := A + B - C; ________________
EXERCISE

1. 4+5 MOD 2 6. 7*2+3 Mod 2


2. 10+4*3 7. 8 + 13 DIV 5
3. 6*4/3*2 8. 3+5*2 Mod 3*2/3
4. 40*3/5*12 9. 7 - 4 * 6 Mod 5 + 4
5. 40*3/(5*12) 10. 5>2 AND NOT(5=6)
Selection control structures
Most programs need to make decisions. There
are several statements available in the Pascal
language for this.
• If
• case
IF statement
Types of IF statements.
If-then-
If-then-else-
Nested if
If…….then…….
The format for the If-then Pascal statement is,

if condition_is_true then execute_this_program_statement;

When the condition is true, the program statement will be


executed. If the condition is not true, then the program
statement following the keyword then will be ignored.
program ProgIF(input,output);
var number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and 10');
readln( guess );
if number=guess then writeln('You guessed correctly.’);
end.

5
If-then-else-
Syntax for the if-then-else statement is:
if condition then S1 else S2;

The IF statement can also include an ELSE statement,


which specifies the statement (or block or group of
statements) to be executed when the condition
associated with the IF statement is false.
Rewriting the previous program using an IF THEN
ELSE statement,
program ProgIF_ELSE(input, output);
var number, guess : integer;
begin
number := 2;
writeln('Guess a number between 1 and 10');
readln( guess );
if guess = number then
writeln('You guessed correctly. Good on you!')
else
writeln('Sorry, you guessed wrong.') 6
end.
EXERCISE
Write a program to find the maximum out of 7
two given numbers
Write a program to find whether given
8
character is digit or not.
Write a program to find the largest of
9
three given numbers
nested if
It is always legal in Pascal programming to nest if-else
statements, which means you can use one if or else if
statement inside another if or else if statement(s).
Pascal allows nesting to any level, however, if depends
on Pascal implementation on a particular system.
Syntax:
The syntax for a nested if statement is as follows:

if( boolean_expression 1) then S1


elseif(boolean_expression 2)then S2
else
S3;
program ProgNested_if;
var n1, n2 : integer;
op:char;
result:real;
begin
writeln(‘Number1’);
readln(n1);
writeln(‘Number2’);
readln(n2);
writeln(‘Operator’); 10
readln(op);
if op = '*' then result := n1 * n2
else if op = '/' then result := n1 / n2
else if op = '+' then result := n1 + n2
else if op = '-' then result := n1 - n2
else writeln(‘invalid operator’); writeln(result);
end.
The CASE statement
You have observed that if-then-else statements enable
us to implement multiple decisions in a program. This
can also be achieved using the case statement in
simpler way.
Syntax:
The syntax of the case statement is:
case (expression) of
L1 : S1;
L2: S2;
...
...
Ln: Sn;
end;
program ProgCase;
var grade: char;
begin
writeln(‘grade(A/B/C/D/F)’);
readln(grade);
case (grade) of
'A' : writeln('Excellent!' );
11
'B', 'C': writeln('Well done' );
'D' : writeln(‘Good’ );
'F' : writeln(‘Need to work harder' );
end;
end.
The case statement allows you to rewrite code which uses a lot of
if else statements, making the program logic much easier to read.
Consider the following code portion written using if else
statements,
if operator = '*' then result := number1 * number2
else if operator = '/' then result := number1 / number2
else if operator = '+' then result := number1 + number2
else if operator = '-' then result := number1 - number2
else invalid_operator:=1;

Rewriting this using case statements,


case operator of
'*' : result:= number1 * number2;
'/' : result:= number1 / number2;
'+' : result:= number1 + number2;
'-' : result:= number1 - number2;
otherwise invalid_operator := 1
end;
Repetition(Loop)
The most common loop in Pascal is the FOR loop.
The statement inside the for block is executed a
number of times depending on the control
condition.
The format's for the FOR command is,
FOR var_name := initial_value TO final_value DO
program_statement;
FOR var_name := initial_value TO final_value DO
begin
program_statement;{to execute more than one statement in a for}
program_statement; {loop, you group them using the begin and }
program_statement {end statements }
end; {semi-colon here depends upon next keyword }

FOR var_name := initial_value DOWNTO final_value DO


program_statement;
program ProgForLoop;
var a: integer;
begin

for a := 1 to 10 do
begin
writeln('value of a: ', a);
12
end;

for b:= 10 downto 1 do


writeln(b);

end.
NESTED LOOPS
A for loop can occur within another, so that the inner
loop (which contains a block of statements) is
repeated by the outer loop.

RULES RELATED TO NESTED FOR LOOPS


1. Each loop must use a separate variable
2. The inner loop must begin and end entirely within
the outer loop.
program ProgNESTED_FOR_LOOPS (output);
var l, c : integer;
begin
writeln('******');
for l := 1 to 5 do
begin
for c := 1 to 3 do
begin 13
write(c:2);
end;
writeln
end
end.
EXERCISE

Write a program output the following pattern.


*
**
***
****
*****
******
WHILE- DO LOOP
Repeats a statement or group of statements until a
given condition is true. It tests the condition before
executing the loop body

Syntax:
The syntax of a while-do loop is:
While (condition) do s;
program ProgWHILE_DO (output);
var i : integer;
begin
i=1;
while i<= 10 do
begin
writeln(i); 14

i=i+1;
end
end.
Repeat-Until Loop
Like a while statement, except that it tests the condition
at the end of the loop body.

Syntax:

repeat
S1;
S2;
...
...
Sn;
until condition;
EXERCISE
1. Write Repeat-Until Loop to display the following output 15
1 2 3 4 5 6 7 8 9 10

2. Write a for loop to display the following output


1
22
16
333
4444
55555
3. Write a program to read a number N and print all its divisors.
17

4. Write a program to calculate the summation of even numbers between 100 and 200.

18

5. Write a program to find prime numbers between 1 and 50. 19

You might also like