Introduction

Basics of Programming

Variables

Input and Output

Boolean Expressions and Branching

Loops

Functions and Procedures

Files

Arrays

HOME

# Chapter 5

## Boolean expressions and Branching

### Boolean Algebra

Boolean algebra is a simple but important concept used in computer science. In boolean algebra variable can only contain one of two possible values: TRUE or FALSE. Boolean algebra has several operators called LOGICAL OPERATORS They are: =, <, >, <=, >=, <>, AND, OR, NOT, & XOR.

"=" Equals

The equals operator is used for comparing two variables. They may be variables of any type just as long as both variables are of the same type.

 FIRST OPERATOR SECOND RESULT 1 = 1 TRUE 1 = 5 FALSE

"<", ">", "<=" and ">="

Less then, Greater than, Less than or equal to,

Greater than or equal to

The greater than and less than symbols are also used for comparing variables.

 FIRST OPERATOR SECOND RESULT

 1 < 2 TRUE 1 < 0 FALSE 1 < 1 FALSE

 2 > 1 TRUE 2 > 3 FALSE 2 > 2 FALSE

 1 <= 2 TRUE 1 <= 0 FALSE 1 <= 1 TRUE

 2 >= 1 TRUE 2 >= 3 FALSE 3 >= 4 FALSE

"<>"

Not Equal to

This returns True if and only if both variables are different.

 FIRST OPERATOR SECOND RESULT 1 <> 1 FALSE 1 <> 0 TRUE

AND

The AND operator returns TRUE if and only if both the second and the first variables are true. If one of the two other variables happens to be FALSE, the result will be FALSE.

 FIRST OPERATOR SECOND RESULT TRUE AND TRUE TRUE TRUE AND FALSE FALSE FALSE AND FALSE FALSE FALSE AND TRUE FALSE

OR

The OR operator returns TRUE if either one of the two variables is TRUE. OR only returns FALSE if both variables are FALSE.

 FIRST OPERATOR SECOND RESULT TRUE OR TRUE TRUE TRUE OR FALSE TRUE FALSE OR FALSE FALSE FALSE OR TRUE TRUE

XOR

The XOR operator returns TRUE if only one of the two variables is TRUE. If both variables are true XOR returns FALSE.

 FIRST OPERATION SECOND RESULT TRUE XOR TRUE FALSE TRUE XOR FALSE TRUE FALSE XOR FALSE FALSE FALSE XOR TRUE TRUE

NOT

The NOT operator only takes one variable. It returns the complete opposite of the variable that follows it.

 OPERATOR FIRST RESULT NOT TRUE FALSE NOT FALSE TRUE

### The Boolean Datatype

Pascal includes a Boolean datatype. This data type can only hold either TRUE or FALSE. The following is an example of a boolean variable declaration.

```  Var
True_Or_False  :  Boolean;```

### THE IF STATEMENT

For a program to be useful it must be able to make decisions based on input. The "IF" statement is used for decision making. The format for the "IF" statement is:

```
IF  boolean expression  THEN  some action```

Take the follwing program for example.

```Program If_Statement (input, output);

Var
x : integer

Begin

{This program asks for an integer}
{ then it displays a message if the }
{number entered is equal to 10. }

Writeln('Enter an Integer');

{compare the number entered with 10 }
{if they're equal print a message}

IF x = 10  THEN  Writeln('You Entered 10');

Writeln('THE END.');

End.```

Pascal also allows more than one statement to be executed after an IF statement. Multiple statements may be included inside a Begin and End. This is called a "Block IF statement." See the following example.

```Program If_Statement2 (Input, Output);

Var
x : integer;

Begin

{This program does the same as the }
{previous program, except that it }
{uses a block IF statement   }

Writeln('Enter an Integer');

IF x = 10  THEN  Begin
{This is inside the IF BLOCK}
{Everything in this block will}
{ execute if x = 10}

Writeln('*************************');
Writeln('You Entered the number 10');
Writeln('*************************');

End;   {End of the IF BLOCK}

Writeln('THE END');

End.   {END of the Program}```

### The ELSE Clause

Perhaps, in the example above, we may want to print a message if the integer entered is not ten. We can use the ELSE clause to the IF statement.

```  IF  boolean expression  THEN
do this
ELSE  do something else```

Pascal checks the boolean expression after the if. If its TRUE, it does everything after the THEN and befor the ELSE. If its FALSE it executes the statement after the ELSE clause.

The ELSE clause can also be used in block IF statements take the following for example.

```Program If_Statement2 (Input, Output);

Var
x : integer;

Begin

{This program does the same as the}
{previous program, except that it }
{uses a block IF statement and }
{ includes the ELSE clause}

Writeln('Enter an Integer');

IF x = 10  THEN  Begin
{This is inside the IF BLOCK}
{Everything in this block will }
{execute if x = 10}

Writeln('*************************');
Writeln('You Entered the number 10');
Writeln('*************************');

End  ELSE BEGIN

{This is inside the ELSE clause}
{clause everything in this block}
{ will execut if x <> 10}

Writeln('**************************');
Writeln('You did not Enter 10');
Writeln('**************************');

End;  {End of the IF-THEN-ELSE statement

Writeln('THE END');

End.   {END of the Program}```

### Using AND, OR, NOT, & XOR in IF-THEN statements

You may be wondering what the "AND, OR, NOT, & XOR" operators are for. They allow for more control in an IF-THEN statement. Take the following code fragments for instance.

```  IF (4 > 2) AND(5 <= 7) THEN
Writeln('true');

IF (4 = 9)  OR  (9 = 9)  THEN
Writeln(' true ');

IF  NOT (4 = 5)  THEN
Writeln(' true ');

IF (4 = 9)  XOR  (9 = 9) THEN
Writeln(' true ');```

All the boolean expressions in these statements return true. These operators allow for more than one condition to be tested in a single IF-THEN statement.