| |
This lesson will cover:
- AND
- OR
- NOT
- AND
- OR
- NOT
|
| |
| |
| |
The logical operators are expressions which return a false or true result over a conditional expression. They are also used in assignments (an example of this would be shown later). Such operators consist of simple logical operators, such as 'Not' or 'And'. They should be used between two conditional expressions ; for example:
If (x = 0) AND (a = 2) then... 
|
The Logical Operators |
|
There are three types of logical operators, each of which are concerned with conditional expressions. These are:
AND
OR
NOT
All of these logical operators have a different effect on the conditional expressions. Let's see what each of the logical operator does two (or more) conditional expressions...

|
| And |
If *1(Str1 = 'a') AND *2(Str2 = 'b') then writeln('Yes, you got it right.');
Expression 1 |
Expression 2 |
AND (result) |
| true |
true |
true |
| false |
true |
false |
| true |
false |
false |
| false |
false |
false |
If expression 1 and expression 2 are both true(i.e. the user inputs 'a' and 'b' into variables 'Str1' and 'Str2' respectively), the message will be displayed. Above is a table showing the possible combinations.
So, from the above table, one can conclude that for a logical operation such as AND, to give out a true result, both conditional expressions should be true.

|
| OR |
|
If *1(Str1 = 'a') OR *2(Str2 = 'b') then writeln('Yes, you got it right.');
Expression 1 |
Expression 2 |
OR (result) |
true |
true |
true |
false |
true |
true |
true |
false |
true |
false |
false |
false |
Either expression 1 or expression 2 should be true to display the message. If for example expression 1 is true and any other conditional expressions are false, the result is true! Above is a table(the truth table) showing the possible combinations.
So, from the above table, one can conclude that for a logical operation such as OR, to give out a true result, only one of the conditional expressions should be true.

|
| NOT |
Not is almost different from the two logical gates. It only accepts one input and is well-known as the 'inverter'. If for example the result of two conditional expressions is true, the 'not' operator would invert the result to false! So, the of the logical operator, 'not', is to output the inverse of the input. The simple truth table for the not operator is:
Input |
Output |
true |
false |
false |
true |

|
Example of the AND Operator |
Program Lesson6_Program1;
Uses Crt;
Var n1, n2 : string;
Begin
Writeln('Enter two numbers: (''0'' & ''0'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '0') AND (n2 = '0') then Halt(0);
Until (n1 = '0') AND (n2 = '0');
End. |

|
Example of the OR Operator |
Program Lesson6_Program2;
Uses Crt;
Var n1, n2 : String;
Begin
Writeln('Enter two numbers: (''1'' & ''2'' to exit)');
Repeat
Write('No.1: ');
Readln(n1);
Write('No.2: ');
Readln(n2);
If (n1 = '1') OR (n2 = '2') then Halt;
Until (n1 = '1') OR (n2 = '2');
End. |

|
| Example of the NOT Operator |
Program Lesson6_Program3;
Uses Crt;
Var n1 : String;
Begin
Writeln('Enter two numbers: (any number except 0 to exit)');
Repeat
Write('No.1: ');
Readln(n1);
If not(n1 = '0') then Halt;
Until not(n1 = '0');
End. |

|
The Boolean Expressions |
The boolean expressions are the terms 'true' and 'false'. These are simply similar to 1's (for true) and 0's(for false). They describe an expression whether it is false or true. The variable types over boolean expressions is the 'boolean' type. Example:
Var bool : Boolean;
Example Program:
Program Lesson6_Program4;
Var quit : Boolean;
a : String;
Begin
Repeat
Write('Type ''exit'' to quit:');
Readln(a);
If a = 'exit' then quit := True else quit := False;
If quit = True then Halt;
Until quit = True;
End. |

|
| |
|
| |
User Comments No user comments yet.
|
| |
|
[HOME][LESSON INDEX][INVITE A FRIEND][FORUM][ABOUT ME]
modified last: September 2005 (Article Version: 2)
mail to: victorsaliba@hotmail.com
© Victor John Saliba 2006 | Privacy Statement | Disclaimer | Contact Us |
|
| |