Skip to main content

Student Portal > Contests > 

Programming Contest Central

   
Welcome students Resources Competition ProfilesFAQ
  Contest calendar
  Problem of the month
  Past problems
  Games and brainteasers
  Successes
  Tutorials

Tracing
What is Tracing?
Tracing by Hand
Recursive Tracing


What is tracing?

Tracing is when you go through an entire program and record the value of variables and the output. You trace a program when you want to find out what a program or function does, or when your code isn't working properly. When you trace a program, you can figure out exactly where the problem lies. There are a couple of ways to trace a program. One way is to put multiple print statements at key points. This will tell you what your variables are at certain points in the program, and from there you can figure out if it's working. The other method is tracing by hand.


Tracing by Hand

Tracing by hand is where you manually go through your program and see what it does. Generally, you print out your program and trace either beside it (if there's enough room), or on a seperate sheet of paper. That's right - no computers involved. This method is much more in-depth, but it can also be time consuming. In either case, it's a skill that every programmer needs to learn. Take a look at the following example.

Example

public class Structures
{
      public static void main(String[] args)
     
{
            int m = 1;
            while (m < 3)
            {
                  System.out.println("Less");
                  m++;
            }

            System.out.println("m has reached 3");
      }
}

To trace the program above, make a column of all the variables (in this case, there is only one variable) and another column for Output. Go through the program line-by-line. Whenever a variable changes value, write the new value on the next line (you can cross out the old value, but don't erase it). Whenever the program outputs something, record the output under the Output column. At the end of the tracing exercise, you should be able to determine what the program (or part of the program) does and if there are any logic errors in your code.

Variables

 

m

Output

1

Less

2

Less

3

m has reached 3



Recursive Tracing

When programming recursive functions, tracing becomes much more difficult. A new column for each variable must be made every time the function is called. For more information, see the Recursion Tutorial.





Back to top

 



Quick links

Student forum

Teacher resources

RSS Feed