Create boolean variables to use as the condition when introducing if statement to highlight for students that the condition is any expression that evaluates to true or false.

  • Instead of using common conditional phrases, like x < 4, use descriptive variables that make what is being evaluated clear, like isXLessThan4.
  • This shift makes the execution that will happen when the code runs clear to the student reading/writing the program.
  • Below is example code that replaces the common conditional phrase with the more descriptive boolean variable.
    • Original Example: if (x < 4) { … }
    • Changed Example: boolean isXLessThan4 = x < 4; if (isXLessThan4){ … }
  • Next, show students a compound condition. Demonstrate how these conditions are still evaluating to true or false.
    • Run through examples like the one below with multiple values for x to help students walk through evaluating compound conditionals.
    • Compound Conditional Example: Random randGen = new Random() // Use Random to create a random number generator int x = randGen.nextInt(100); // get a random number between 0 and 100 // odd number divisible by 3, or in the 70s if ((x % 2 == 1 && x % 3 == 0) or (x // 7 == 10)) { System.out.println("We found an appropriate number!"); }

More about this tip

External Source
Tip-A-Thon