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-08.p
{ Introduction to the Pascal Programming Language
Dr. Thomas W. MacFarland
intro_pencil-08.p
}
{ The purpose of this program is to:
1. Demonstrate the selection process used with the if/then
conditional branching process.
2. Demonstrate the selection process used with the if/then/else
conditional branching process.
3. Demonstrate how there can be two or more selections within
a broad conditional statement.
4. Demonstrate the begin/end; statements in a conditional block
statement.
5. Demonstrate how the ; character is not used at the end of
a writeln statement in a multiple if/then/else statement.
6. Demonstrate the use of indentation to improve program
readability.
}
program Demonstrate_If_Then_Else;
var A, B, C : integer;
begin
A := 5;
B := 6;
C := 7;
writeln('A = ', A:25);
writeln('B = ', B:25);
if C = (A + 2) then
writeln('C = (A + 02) = (05 + 02) = 07');
if C = (12 - A) then
begin
writeln('C = (12 - A) = (12 - 05) = 07');
end;
if B = C then
writeln('B = C')
else
writeln('B <> C');
if B < 3 then
if A < B then
writeln('B < 3 AND A < B')
else
writeln('B may be > 3 OR A may be > B')
else
if C = 7 then
writeln('At least it looks like C = 07')
else
writeln('I need to go back to square one');
end. { intro_pencil-08.p }
% pc PASCAL/intro_pencil-08.p
% a.out
A = 5
B = 6
C = (A + 02) = (05 + 02) = 07
C = (12 - A) = (12 - 05) = 07
B <> C
At least it looks like C = 07