Selection

A fundamental concept in programming is the idea that it is possible to evaluate a boolean expression, and depending on whether it evaluates to true or not a line or block of code runs: the so called if… statement.

if ( age < 18 ){ // if age is less than 18, line below runs
   System.out.println("You are too young to vote");
}

if…else

It is also possible to have a line or block of code run if the boolean expression evaluates to false. This is the else… statement, and must be used alongside an if

if (moneyAvailable < 399){
   System.out.println("Not enough money to buy an iPad!");
} // end of if block
else{ // line below runs if moneyAvailable is >= 399     
   System.out.println("Enough money to buy an iPad");
} // end of else block

if…else if…

Some languages also have an else if statement, which like else only executes if the boolean expression in if… evaluated to false

if (cash < 399){
   System.out.println("Not enough money to buy an iPad!");
} // end of if block
else if (cash > 599) { // another condition, runs when cash is not < 399    
   System.out.println("Enough for high spec. iPad");
} // end of else if block
else{ // no condition here, runs when cash is not < 399 or > 599   
   System.out.println("Enough money to buy an iPad");
} // end of else if block

Nesting

Not all languages support else…if but instead it is possible to have if statements inside other if statements (as in the example below), in which case the inner if will only run if the outer if condition is true.

if (cash < 399){
   System.out.println("Not enough money to buy an iPad!");
}
else{
   if (cash > 599){ //nested if: i.e. if inside another if. 
      System.out.println("Enough for high spec. iPad");
   } // end of inner if block      
// will only get to block below for cash between 399 & 599
   else{ 
      System.out.println("Enough money to buy an iPad"); 
   } // end of inner if...else
} // end of outer if...else

This starts to get complex if you have more than one nested if, so another solution exists: the so called case statement.

Switch/Case

The alternative to nested ifs is the switch keyword, alongside its associated case statements. Type in the code below and compile it. Note it’s a screenshot on purpose: at this point when typing you will still make silly errors with missing brackets and semi-colons, so the more practice working through those silly errors the better.

Screenshot of example Java code to be typed in by beginner programmers to explore syntax of switch/case
Type in and experiment with this code
With no changes, this is the output your would expect to see.

What is happening in the example is that whatever the value of level is determines which matching case will run. Two cases can perform exactly the same action if you want, for example BOSS_LEVEL and PENULTIMATE_LEVEL generate exactly the same output. The case statement can be followed by a break statement at which point it will stop. In the case of BOSS_LEVEL it will run all the statements because the break clause is last.

Modify the program so that there is a break clause is after each println call.

Try calling the printPraise method with the value 5 to see what happens.

Exercises

  1. Write a program that uses an integer value stored in a variable as a month number. It should use a switch/case structure to display the name of the month, e.g for month=9 it should print September.
  2. Change the program you just wrote above so that it prints the days in that month, e.g. for month=9 or month=4 it should print 30.
  3. Extend your program further so that it takes an integer month and an integer year. Using both values it calculates the number of days in a month correctly even for leap years (a leap year is exactly divisible by 4, but not a century unless it is divisible by 400, so 1600 is a leap year but 1700 is not).

Knowledge Check

  • What is a selection statement?
  • What are two different types of selection statements in Java?
  • Under which circumstances would you use each type of selection statement?
  • Is there a possible speed advantage to switch/case compared with multiple ifs?