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-18.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-18.p
}
{ The purpose of this program is to:
1. Demonstrate the use of multiple variable types in one
program: integer, real, boolean, char.
2. Demonstrate mathematical operations with integer, real, and
boolean.
3. Demonstrate program output when attempting division by zero.
4. Demonstrate program output in the form of a table.
}
program Demonstrate_Mixed_Variable_Types_in_One_Program;
var I_1, I_2, I_3, I_4 : integer;
R_1 : real;
B_1 : boolean;
C_1 : char;
begin
writeln;
write(' I_1'); write(' I_2'); write(' I_3');
write(' I_4'); write(' R_1'); write(' B_1');
write(' C_1');
writeln;
writeln(' ---------------------------------------------------');
I_1 := 1;
I_2 := I_1 + 3;
I_3 := I_2 - 2;
I_4 := 7;
R_1 := 10;
B_1 := I_1 < I_3;
C_1 := 'z';
write(' ', I_1 : 6); write(' ', I_2 : 6); write(' ', I_3 : 6);
write(' ', I_4 : 6); write(' ', R_1 : 6:2); write(' ', B_1 : 6);
write(' ', C_1 : 6); writeln; writeln;
I_1 := 9;
I_2 := I_1 mod 6;
I_3 := I_2 div 2;
I_4 := I_1 - I_2;
R_1 := ((I_1 * I_2) / (I_3 / I_4));
B_1 := I_1 > R_1;
C_1 := 'a';
write(' ', I_1 : 6); write(' ', I_2 : 6); write(' ', I_3 : 6);
write(' ', I_4 : 6); write(' ', R_1 : 6:2); write(' ', B_1 : 6);
write(' ', C_1 : 6); writeln; writeln;
I_1 := 9;
I_2 := I_1 mod 6;
I_3 := I_2 div 8;
I_4 := I_1 - I_2;
R_1 := ((I_1 * I_2) / (I_3 / I_4));
B_1 := I_1 > R_1;
C_1 := '8';
write(' ', I_1 : 6); write(' ', I_2 : 6); write(' ', I_3 : 6);
write(' ', I_4 : 6); write(' ', R_1 : 6:2); write(' ', B_1 : 6);
write(' ', C_1 : 6); writeln;
writeln(' ---------------------------------------------------');
writeln;
end. { intro_pencil-18.p }
% pc PASCAL/intro_pencil-18.p
% a.out
I_1 I_2 I_3 I_4 R_1 B_1 C_1
---------------------------------------------------
1 4 2 7 10.00 true z
9 3 1 6 162.00 false a
9 3 0 6 Inf false 8
---------------------------------------------------
Note: the following IEEE floating-point arithmetic exceptions
occurred and were never cleared; see ieee_flags(3M):
Inexact; Division by Zero;
Sun's implementation of IEEE arithmetic is discussed in
the Numerical Computation Guide.