Substitute numbers for variables when tracing through loops in order to help students reason about loop iteration patterns.

  • Substitute various values for n to demonstrate a pattern.
    • For example, determine how many times “Hello!” is printed in the following Java code from the 2009 AP CS A exam multiple choice. k is a random number such that 1 ≤ k ≤ n.
      for (int p=2; p<=k; p++) {
        for (int r=1; r<k; r++) {



    • In the Java code above, encourage students to cross out variables and fill with exact values before tracing through the loop to avoid confusion and reduce the number of things they need to hold in their working memory.
  • Don’t substitute 1 or 2 for variables since these values can lead to additional misconceptions.
    • If we substitute n=2 in the code above, it would execute the same number of times as two sequential for loops with the same bounds since 2+2=2*2.
External Source: 

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

Other Tips By: