Sign Guestbook
View Guestbook
Invite Your Friends
Website Awards
Message Forum
Source Codes
Downloads
Links
About Me
 
     
Lesson 1 - The first few steps in Pascal Programming
Lesson 2 - Variables, Constants and the Assignment  Statement
Lesson 3 - Special Functions: ClrScr(), GotoXy(), etc...
Lesson 4 - Program Control
Lesson 5 - The CASE-OF Statement
Lesson 6 - Logical Operators and Boolean Expressions
Lesson 7 - Procedures and Functions
Lesson 8 - BGI Graphics
Lesson 9 - File Handling
Lesson 10 - Arrays
Lesson 11 - Record Data Structure
Lesson 12 - Strings

Add this site to your Favourites

   



 


Lesson 6 - Logical Operators and Boolean Expressions

 
This lesson will cover:
  1.   AND
  2.   OR
  3.   NOT
  1.   AND
  2.   OR
  3.   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

Post By: Nanggy Posted On: 2009-09-30
I dont know if am overwhelmed or maybe programming is not just for me, i dnt know but can anybody pls help me on how to write a program declaring a "one dimmensional arrey"..and thanx in advance..frm Namibia..and pls may be u can even email me,mnanggy@gmail.com

Post By: ashamideAngel Posted On: 2009-08-27
I have a dream that one day i become a good programmer...Thanks so much,i'm very greatful.

Post By: ramsey mnjala Posted On: 2009-07-15
i will continue onethr day am exhausted by the much i have learnt today be blessed 'kabisaaaaa' tha was some kiswahili call me if you want to learn more about it ohh n dont 4get am a kenyan 4ro coast province

Post By: codecaine Posted On: 2009-02-16
Sunny remember delphi has 2 divisions. div is for integer values \ is for real values

Post By: ogii Posted On: 2009-01-12
Thanks a lot

Post By: Sunny Posted On: 2008-12-11
thanks a million times to this site. Can anybody help me making a calculator as i have made it. Addtion,subtraction, multiplication works fine but it says type mismatch for divion. i checked it again but still it says the same thing. can anybody help me plz. thanks in advance

Post By: @ms Posted On: 2008-08-05
Thanks a million times, those topics really helped.

Post By: Max Posted On: 2008-07-11
so far, so good I'm impressed on the topic and content too, maybe you need an easy tool too (pascal in a box): http://www.softwareschule.ch/download/maxbox2.zip

Post By: TarantulaS Posted On: 2008-04-14
Very helpfull site it is pretty nice ! thanks a lot

Post By: James Posted On: 2007-08-10
good site ; it is very simple to understand.

Post By: CatCat Posted On: 2007-03-22
I think this page can help me to my program.

Post By: Jake Posted On: 2007-03-20
Great site! But I just wanted to point out that you dodn't include XOR, which is default with most compilers.

Post By: Lucky Wickramasinghe-sri lanka Posted On: 2006-12-12
I have no words to express what I feel! I think this will help me to do my BCS studies well.thanks a lot!

Post By: Gift Posted On: 2006-07-20
This site is probably the best on the net so far. As an upcoming programmer i am finding it useful and helpful in my programming studies. i dont regret associating myself with this site.


Post Your Comment on this Article!
Your Name/Nickname:
Your Email:
Your Comment:
Insert the BLUE numbers shown below in the text field on the right. [ This is an anti-spam counter-measure ]
552289880
 
 
[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 |
 
 
 
www.pascalprogramming.byethost15.com © Victor John Saliba 2006