Step through an accumulator pattern on board to make clear how variables change within a while loop to help students understand this confusing topic.

  • Activity:
    • Show students the following code:
      • int i = 1; double result = 1; while (i < 9){ result = result + i; i = i + 2; }
    • Tell students you’re going to trace the value of i during the loop execution.
    • Draw a table on the board that will show the current value for i and result.
    • Cross out each row in the table as the code iterates to make it clear to students that the previous values are no longer available.
      • The table should look like the table below:
        • resulti
          1.01
          2.03
          5.05
          10.07
          17.09

    More about this tip

    External Source

    Interview with Serita Nelesen