Debugging Loops with a Hand Trace
The following code fragment is supposed to display the even numbers
between 1 and 15. However, it is defective. Use a hand trace table to isolate the defect.
1 int Number = 2;
2 WHILE (Number != 15)
3 BEGIN
4 Number = Number + 2;
5 Print( Number );
6 Print( " " );
7 END
#1. What is the actual output of the code as written?
#2. Explain the defects that are causing the code
to malfunction, and explain how you would correct them.
#3. There is a defect in the logic of the following
nested loop structure.
Use a hand trace table to isolate the defect.
How can this defect be repaired?
1 int MaxRows = 5;
2 int RowCount = 1;
3 WHILE (RowCount <= MaxRows)
4 BEGIN
5 int LoopCount = 1;
6 WHILE (LoopCount
<= RowCount)
7 BEGIN
8
Print ("*");
9
LoopCount = LoopCount + 1;
10 END
11 Print new line;
12 END