Show students examples of unreachable code to help them reason about how conditionals are executed.

  • By setting up examples where a conditional will never execute, you can help students reason about the fact that conditionals only execute under certain conditions.
    • For example, in the following Java code, if x<0, x cannot be >100, so the print statement is unreachable.
      • if (x < 0) {
          if (x > 100) {
            System.out.println("Ooops - we can never get here!");

          }

        }

More about this tip

External Source

Interview with Jeff Gray.

Other Tips By