Pascal Programming/Boolean Expressions and Control Flow

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Boolean Expressions[edit]

Sometimes the computer needs to make a decision depending on whether certain conditions are met. Remember that "Boolean" is a Pascal data type for the values 'true' and 'false'. Boolean expressions allow the computer to make some judgment about whether some state of affairs is actually the case. Such expressions are useful in writing conditional expressions and loops.

Comparison operators[edit]

A plain equal sign (=) is used to tell whether two values are the as the same. Pascal also recognizes the "greater than" and "less than" signs from mathematics, and combines them into a "not equal" sign. We can also produce "less than or equal" and "greater than or equal" operators with the right combinations.

write(7 > 8);  { false }
write(7 < 8);  { true }
write(7 = 8);  { false }
write(7 <> 8); { true }
write(7 <= 8); { true}
write(7 >= 8); { false }

Be careful not to confuse the equal sign with assignment operator, which always includes a colon. They are different but may be used to together.

money := 0;
(* The variable "money" now contains the number zero. *)
broke := money = 0;
(* The variable "broke" now contains the value "true". *)

Logical operators[edit]

Pascal includes the keywords "and" and "or" for combining expressions. The keyword "not" switches the truth value of whatever comes immediately after it.

(* What's the weather like today? Let's say it's snowing but not raining. *)
raining := false;
snowing := true;

precipitation := raining or snowing; { true }
wintryMix := raining and snowing; { false }

dry := not (raining or snowing); { false }
dry := precipitation = false; { equivalent to the statement above }

The parentheses in the above example were important, because "not" gets evaluated before any other Boolean operator.

(* This would have been a mistake *)
dry := not raining or snowing; { false: it's not raining, but it is snowing! }
dry := (not raining) or snowing; { equivalent to the statement above }

Conditional Statements[edit]

If..Then..Else[edit]

The simplest control structure is the if..then..else statement.

if var1 = 0 then  
    writeln('var1 is 0!')  (*No semicolon before an 'Else' keyword*)
else if var1 = 1 then 
begin
    writeln('var1 is 1!');
    (*More code*)
end  (*No semicolon before an 'Else' keyword*)
else if var1 = 2 then 
begin
    writeln('var1 is 2!');
    (*More code*)
end  (*No semicolon before an 'Else' keyword*)
else 
begin
    writeln('var1 is not 0, 1, or 2!');
    (*More code*)
end;  (*Semicolon used to indicate the end of the If-then-else*)

Case[edit]

Case var1 of
  1 : writeln('var1 is 1!');
  2 : Begin
    writeln('var1 is 2!');
    (*More code*)
  End;
  Else writeln('var1 is neither 1 nor 2!'); (*'else' can be used instead of the 'otherwise' keyword*)
end;

Loops[edit]

For[edit]

For var1 := 0 to 12 do  (*For conditional with only one statement:*)
  writeln('var1 is ', var1);

For var2 := 12 downto 0 do Begin  (*For conditional with multiple statements:*)
  writeln('var2 is ', var2);
  (*More code*)
End;

While[edit]

While var1 = 0 do (*While the condition is True, perform the following code*)
  writeln('woo, an infinite loop!');

While var2 = 0 do Begin (*While-do with multiple statements:*)
  writeln('aww, no infinite loop!');
  var2 := 1;
End;

Repeat..Until[edit]

Repeat (*Repeat the following until the condition is True:*)
  writeln('var1 might be 0!');
  (*More code - Begin..End is not required between Repeat..Until*)
  (*Semicolon needed before the 'Until' keyword*)
Until var1 = 0;