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-09.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-09.p
}
{ The purpose of this program is to:
1. Demonstrate the selection symbols: <, >, <=, >=, <>.
1. Demonstrate the process used to create a nested loop.
2. Demonstrate conditional statements within a nested loop.
}
program Demonstrate_If_Statement_in_a_Nested_Loop;
var A, B : integer;
begin
for A := 1 to 4 do begin
if A <= 2 then
writeln('A = ',A:4);
if A >= 3 then begin
for B := 2 to 4 do begin
write('A = ',A:4);
write(' and B = ',B:4);
if A > 5 then write (' A > 5');
if B <> 4 then write (' because B <> 4');
writeln;
end;
end;
end;
end. { intro_pencil-09.p }
% pc PASCAL/intro_pencil-09.p
% a.out
A = 1
A = 2
A = 3 and B = 2 because B <> 4
A = 3 and B = 3 because B <> 4
A = 3 and B = 4
A = 4 and B = 2 because B <> 4
A = 4 and B = 3 because B <> 4
A = 4 and B = 4