Misconception: Students have difficulty understanding why a while(true) loop continues forever.

  • Students can struggle to understand the keyword true as being a state.
  • Scaffold students to understanding why a while(true) loop continues forever using a series of expressions that will evaluate to the keyword true.
  • Activity:
    • To introduce while(true) loops, have students consider what will happen if you write: while(1<2)
      • You could also use another always-true conditional expression.
      • The goal is for students to realize that Java is checking a conditional.
    • Ask students what other boolean expressions are always true.
    • Replace while(1<2) with while(true == true).
    • Remind them that the "== true" part is implied in all conditionals and loops.
    • Finally, replace while(true == true) to while(true).

More about this tip

External Source

Interview with Dan Leyzberg.