|
|
"<", ">", "<=" 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.
| TRUE | |||
| FALSE | |||
| TRUE |
| TRUE | |||
| FALSE | |||
| FALSE |
"<>"
Not Equal to
This returns True if and only if both variables are different.
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.
OR
The OR operator returns TRUE if either one of the two variables is TRUE. OR only returns FALSE if both variables are FALSE.
XOR
The XOR operator returns TRUE if only one of the two variables is TRUE. If both variables are true XOR returns FALSE.
NOT
The NOT operator only takes one variable. It returns the complete opposite of the variable that follows it.
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;
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');
Readln(x);
{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');
Readln(x);
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}
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');
Readln(x);
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}
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.