You are on page 1of 2

UTP/May 2013/FAT 0015 Fundamental of Programming I Week 8 Lab 1

Learning Outcomes
Upon completion of this lab session, learners will be able to:
1. grasp the basic concepts of selection structures,
2. hand trace the program and write the final output,and
3. implement selection structures in Pascal by applying if..else and case

Keywords
Selection structure, if..else, case

Activity #1
Without using a computer, trace the following code snippets. Give your
explanation for the code snippet that produces errors.

a. x := 5;
y := 3;
z := 1;
if z <> 0 then
y := 123
else
x := 321;
//what are the values of x, y and z at this line?

b. x := 5;
y := 3;
z := 1;
if (y + z > 10) then
y := 99;
z := 8;
x := z + 1;
//what are the values of x, y and z at this line?

c. x := 5;
y := 3;
z := 1;
if x > 2 or x < 4 then
y := 5;
//what are the values of x, y and z at this line?

d. x := 5;
y := 3;
z := 1;
if x = 1 then
begin
x := x - 3;
z := z + 3;
end
else
y := 99;
//what are the values of x, y and z at this line?

Page 1 of 3
UTP/May 2013/FAT 0015 Fundamental of Programming I Week 8 Lab 1

e. x := 5;
y := 3;
z := 1;
if 0 < z < 100 then
x := x + 10
else
y := y + 10;
//what are the values of x, y and z at this line?

f. x := 25;
if (x < 100) then
x := x + 5;
if (x < 500) then
x := x - 2;
if (x > 10) then
x := x + 1
else
x := x - 1;
//what is the value of x at this line?

g. x := 1;
case x of
1 : begin
write ('One');
writeln('Satu');
end;
2 : writeln ('Two');
end;
//what is the output?

h. x := 2;
case x of
1 : begin
write ('One');
writeln('Satu');
end;
2 : writeln ('Two');
Writeln (Dua);
end;
//what is the output?

i. y := 1;
x := 2;
case y of
1 : y := y + 1;
2 : y := x + 2;
else
x := y + x;
end;
//what are values of x and y at this line?

Page 2 of 3

You might also like