Introduction to the Pascal Programming Language
© 1998 by Dr. Thomas W. MacFarland -- All Rights Reserved
Instructions: Review the following program and see if you can predict the outcome by completing a "pencil trace" of the code. Program output is provided at the end of this page. Compiler: The program was prepared using Standard Pascal on a UNIX-based host computer.
% cat PASCAL/intro_pencil-10.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-10.p
}
{ The purpose of this program is to:
1. Demonstrate the Boolean operators: and, or, not.
2. Demonstrate the use of a negative number in a counter-
type loop.
}
program Demonstrate_Boolean_Operators_in_a_Loop;
var X : integer;
begin
for X := -10 to -5 do begin
if (X <> -7) and (X <> -6) then
write(' X = ',X:3);
end; writeln; writeln;
for X := -10 to -5 do begin
if (X <> -7) or (X <> -6) then
write(' X = ',X:3);
end; writeln; writeln;
for X := -10 to -5 do begin
if not (X <> -7) and (X <> -6) then
write(' X = ',X:3);
end; writeln; writeln;
end. { intro_pencil-10.p }
% pc PASCAL/intro_pencil-10.p
% a.out
X = -10 X = -9 X = -8 X = -5
X = -10 X = -9 X = -8 X = -7 X = -6 X = -5
X = -7