Hand Tracing Technique for Iteration

Step 1: Number each executable statement in your source code.

1     int Counter = 1;
2     int Sum = 0;
3     int Limit = getInt("Enter a integer limit amount");
4     WHILE (Counter <= Limit)
      BEGIN
5       Sum = Sum + Counter;
6       Counter = Counter + 1;
      END
7     PRINT ("The Sum is " + Sum );
 

Step 2: Draw a table with each variable in the program shown at the top of a column. Draw a column for Statement number on the left and a column for Output on the right. It's also helpful to draw a separate column for each boolean expression.
 
Stmt Limit Counter Sum Counter <= Limit Output

Step 3: Starting with statement 1, simulate the action the computer will take when it executes each statement. Draw one statement per row. In the appropriate column, write the value that is assigned to the variable (or boolean expression).
 
Stmt Limit Counter Sum Counter <= Limit Output
1   1      
2     0    
3 3        
4       True  
5     1    
6   2      
4       True  
5     3    
6   3      
4       True  
5     6    
6   4      
4       False  
7         The Sum is 6
  

Practice Hand Tracing with Iteration

Practice the hand trace technique on your own paper with these pseudocode fragments.

1     Sum = 0
2     READ Number
3     WHILE Number not equal -1
4       READ Number
5       Sum = Sum + Number
6     END WHILE
7     PRINT Sum
 
 
 

1     Length = 5
2     Count = 4
3     WHILE Count <= 6
4        IF Length >= 100 THEN
5           Length = Length - 2
         ELSE
6           Length = Count * Length
         ENDIF
7     Count = Count + 1
      END WHILE
8     PRINT Length
 
 


 

1     READ StarCount
2     WHILE StarCount > 0
3        LoopCount = 1
4        WHILE LoopCount <= StarCount
5           Print "*"
6           LoopCount = LoopCount + 1
         END WHILE
7        PRINT NEWLINE
8        READ StarCount
      END WHILE
9     PRINT "GoodBye"