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-11.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-11.p
}
{ The purpose of this program is to:
1. Demonstrate integer math.
2. Demonstrate a for/do loop, a loop with a set number of
iterations.
3. Demonstrate the use of ( and ) to direct the order of
operations.
}
program Demonstrate_More_Activities_With_Integers;
var A, B, C, D: integer;
begin
writeln;
for A := -2 to 3 do begin
B := 10 + 5 * A;
C := (10 + 5) * A;
D := 10 + ((A * 7) mod 2);
write(' A = ', A :5); write(' B = ', B :5);
write(' C = ', C :5); write(' D = ', D :5);
if A >= 2 then
write(' A >= 2') else write(' A < 2'); writeln;
end;
end. { intro_pencil-11.p }
% pc PASCAL/intro_pencil-11.p
% a.out
A = -2 B = 0 C = -30 D = 10 A < 2
A = -1 B = 5 C = -15 D = 9 A < 2
A = 0 B = 10 C = 0 D = 10 A < 2
A = 1 B = 15 C = 15 D = 11 A < 2
A = 2 B = 20 C = 30 D = 10 A >= 2
A = 3 B = 25 C = 45 D = 11 A >= 2