 ..:: A guide for You who wanna be a programming master!! ::..

Expressions
Tips 'n Tricks

---------------------
Home
---------------------
History
---------------------
Program Structure
---------------------
Compilers
---------------------

Materials :
1. Basics
2. Input-output
3. Expressions
4. Procedures and Functions

 Boolean Expressions CASE WHILE DO IF FOR DO

Boolean Expressions

Boolean expressions are used to compare two values and get a true-or-false answer:

value1 relational_operator value2

The following relational operators are used:

 < less than > greater than = equal to <= less than or equal to >= greater than or equal to <> not equal to

You can assign Boolean expressions to Boolean variables. Here we assign a true expression to some_bool:

some_bool := 3 < 5;

Complex Boolean expressions are formed by using the Boolean operators:

 not negation (~) and conjunction (^) or disjunction (v) xor exclusive-or

NOT is a unary operator — it is applied to only one value and inverts it:

• not true = false
• not false = true

AND yields TRUE only if both values are TRUE:

• TRUE and FALSE = FALSE
• TRUE and TRUE = TRUE

OR yields TRUE if at least one value is TRUE:

• TRUE or TRUE = TRUE
• TRUE or FALSE = TRUE
• FALSE or TRUE = TRUE
• FALSE or FALSE = FALSE

XOR yields TRUE if one expression is TRUE and the other is FALSE. Thus:

• TRUE xor TRUE = FALSE
• TRUE xor FALSE = TRUE
• FALSE xor TRUE = TRUE
• FALSE xor FALSE = FALSE

When combining two Boolean expressions using relational and Boolean operators, be careful to use parentheses.

(3>5) or (650<1)

This is because the Boolean operators are higher on the order of operations than the relational operators:

1. not
2. * / div mod and
3. + - or
4. < > <= >= = <>

So 3 > 5 or 650 < 1 becomes evaluated as      3 > (5 or 650) < 1, which makes no sense, because the Boolean operator or only works on Boolean values, not on integers.

The Boolean operators (AND, OR, NOT, XOR) can be used on Boolean variables just as easily as they are used on Boolean expressions.

Whenever possible, don't compare two real values with the equals sign. Small round-off errors may cause two equivalent expressions to differ.

IF

The IF statement allows you to branch based on the result of a Boolean operation. The one-way branch format is:

if BooleanExpression then
StatementIfTrue;

If the Boolean expression evaluates to true, the statement executes. Otherwise, it is skipped.

The IF statement accepts only one statement. If you would like to branch to a compound statement, you must use a begin-end to enclose the statements:

if BooleanExpression then
begin

Statement1;
Statement2
end;

There is also a two-way selection:

if BooleanExpression then
StatementIfTrue
else
StatementIfFalse;

If the Boolean expression evaluates to FALSE, the statement following the else will be performed. Note that you may not use a semicolon after the statement preceding the else. That causes the computer to treat it as a one-way selection, leaving it to wonder where the else came from.

If you need multi-way selection, simply nest if statements:

if Condition1 then
Statement1
else
if Condition2 then
Statement2
else
Statement3;

Be careful with nesting. Sometimes the computer won't do what you want it to do:

if Condition1 then
if Condition2 then
Statement2
else
Statement1;

The else is always matched with the most recent if, so the computer interprets the preceding block of code as:

if Condition1 then
if Condition2 then
Statement2
else
Statement1;

You can get by with a null statement:

if Condition1 then
if Condition2 then
Statement2
else
else
Statement1;

or you could use a begin-end block. But the best way to clean up the code would be to rewrite the condition.

if not Condition1 then
Statement1
else
if Condition2 then
Statement2;

This example illustrates where the not operator comes in very handy. If Condition1 had been a Boolean like: (not(a < b) or (c + 3 > 6)) and g, reversing the expression would be more difficult than NOTting it.

Also notice how important indentation is to convey the logic of program code to a human, but the compiler ignores the indentation.

CASE

Suppose you wanted to branch one way if b is 1, 7, 2037, or 5; and another way if otherwise. You could do it by:

if (b = 1) or (b = 7) or (b = 2037) or (b = 5) then
Statement1
else
Statement2;

But in this case, it would be simpler to list the numbers for which you want Statement1 to execute. You would do this with a case statement:

case b of
1,7,2037,5: Statement1;
otherwise   Statement2
end;

The general form of the case statement is:

case selector of
List1:    Statement1;
List2:    Statement2;
...
Listn:    Statementn;
otherwise Statement
end;

The otherwise part is optional. When available, it differs from compiler to compiler. In many compilers, you use the word else instead of otherwise.

selector is any variable of an ordinal data type. You may not use reals!

Note that the lists must consist of literal values. That is, you must use constants or hard-coded values -- you cannot use variables.

FOR DO

Looping means repeating a statement or compound statement over and over until some condition is met.

There are three types of loops:

• fixed repetition - only repeats a fixed number of times
• pretest - tests a Boolean expression, then goes into the loop if TRUE
• posttest - executes the loop, then tests the Boolean expression

In Pascal, the fixed repetition loop is the for loop. The general form is:

for index := StartingLow to EndingHigh do
statement;

The index variable must be of an ordinal data type. You can use the index in calculations within the body of the loop, but you should not change the value of the index. An example of using the index is:

sum := 0;
for count := 1 to 100 do
sum := sum + count;

The computer would do the sum the long way and still finish it in far less time than it took the mathematician Gauss to do the sum the short way (1+100 = 101. 2+99 = 101. See a pattern? There are 100 numbers, so the pattern repeats 50 times. 101*50 = 5050. This isn't advanced mathematics, its attribution to Gauss is probably apocryphal.).

In the for-to-do loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the for-downto-do loop:

for index := StartingHigh downto EndingLow do
statement;

In Pascal, the for loop can only count in increments (steps) of 1.

WHILE DO

The pretest loop has the following format:

while BooleanExpression do
statement;

The loop continues to execute until the Boolean expression becomes FALSE. In the body of the loop, you must somehow affect the Boolean expression by changing one of the variables used in it. Otherwise, an infinite loop will result:

a := 5;
while a < 6 do
writeln (a);

Remedy this situation by changing the variable's value:

a := 5;
while a < 6 do
begin
writeln (a);
a := a + 1
end;

The WHILE ... DO lop is called a pretest loop because the condition is tested before the body of the loop executes. So if the condition starts out as FALSE, the body of the while loop never executes.