Encourage students to replace variables with values when tracing through code to reduce the necessary cognitive load.

Like 
  • The fewer variables students have to keep track of when tracing through code, the easier it will be.
  • For example, in the following Java code from the 2009 AP CS A exam multiple choice, what value is returned as a result of the call sol(10)?

     

    public int sol(int lim) {
      int s = 0; for (int outer = 1; outer <= lim; outer++) {
        for (int inner = outer; inner <= lim; inner++) {
          s++;
        }
      } return s;
    }
    • In the Java code above, students can cross out lim and write in 10 to make tracing through this code easier. Each time they trace through the inner loop, students can cross out outer and replace it with the current value of outer.
      • This way students don’t have to keep track of the values of lim, outer, inner, and s in their head at the same time. When tracing through the inner loop, they can focus on the values of inner and s.
External Source: 

Leigh Ann Sudol-DeLyser’s notes from 2009 AP CS A exam.

Other Tips By: