Explain nested for loops using a car odometer, the innermost loop controlling the ones digit must cycle fully before the outer loops steps forward, to help students better understand nested loops through a real world example.

  • Nested loops can be a very confusing for students. Especially when it comes to the order in which the loops execute.
    • for(int tensDigit = 0; tensDigit ≤9; tensDigit++){
        for(int onesDigit = 0; onesDigit ≤9; onesDigit++){
          System.out.println(tensDigit + ""+ onesDigit);
        }
      }
    • The concrete nature of the ones’ place digit needing to update all the way around (digits 0-9) before the tens’ place will update can help students with this tricky concept.
  • Additionally, the number of for loops controls the number of digits in this example. There are as many digits as there are for loops.
  • Use the MathBits website Dani borrowed this idea from to help students get a better grasp on this idea.