3.1. Conditionals

Control flow statements in Java — conditionals and loops — are very straightforward.

3.1.1. Operators

Before we review the syntax for conditionals, let’s go over the comparison and logical operators that we need to use in control flow statements.

3.1.1.1. Comparison Operators

Comparison Operators

Operator

Description

==

Checks if two items are equal

!=

Checks if two items are not equal

<

Checks if item on left is lesser than item on right

<=

Checks if item on left is lesser than or equal to item on right

>

Checks if item on left is greater than item on right

>=

Checks if item on left is greater than or equal to item on right

3.1.1.2. Logical Operators

Logical Operators

Operator

Description

&&

Combines two expressions with AND, returns true if both statements are true

||

Combines two expressions with OR, returns true if at least one of the statements is true

!

Reverses the evaluation of the operand, returns false if the result is true

3.1.2. if Statements

Let’s consider an if statement with no else clause.

In Java this pattern is simply written as:

1
2
3
4
5
if (condition) {
   statement1
   statement2
   ...
}

You can see that in Java the curly braces define a block. Parentheses around the condition are required.

3.1.3. if else

Adding an else clause, we have:

1
2
3
4
5
6
7
8
9
if (condition) {
   statement1
   statement2
   ...
} else {
   statement1
   statement2
   ...
}

3.1.4. else if

An else if construction in Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
 import java.util.Scanner;

 public class ElseIf {
     public static void main(String args[]) {
         Scanner in = new Scanner(System.in);
         System.out.println("Enter a grade: ");
         int grade = in.nextInt();
         if (grade < 60) {
             System.out.println('F');
         } else if (grade < 70) {
             System.out.println('D');
         } else if (grade < 80) {
             System.out.println('C');
         } else if (grade < 90) {
             System.out.println('B');
         } else {
             System.out.println('A');
         }
     }
 }

3.1.5. switch Statements

Java also supports a switch statement that acts something like an else if statement under certain conditions, called cases. The switch statement is not used very often, and we generally recommend you avoid using it. It is not as powerful as the else if model because the switch variable can only be compared for equality with a very small class of types.

Here is a quick example of a switch statement:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;

public class DayPrinter {
   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter an integer: ");
      int dayNum = in.nextInt();

      String day;
      switch (dayNum) {
         case 0:
            day = "Sunday";
            break;
         case 1:
            day = "Monday";
            break;
         case 2:
            day = "Tuesday";
            break;
         case 3:
            day = "Wednesday";
            break;
         case 4:
            day = "Thursday";
            break;
         case 5:
            day = "Friday";
            break;
         case 6:
            day = "Saturday";
            break;
         default:
            // in this example, this block runs if none of the above blocks match
            day = "Int does not correspond to a day of the week";
      }
      System.out.println(day);
   }
}

In the example above, here’s the output if a user enters the number 4.

Enter an integer: 4
Thursday

And the output if that user enters 10? Below:

Enter an integer: 10
Int does not correspond to a day of the week

Here’s how the above example looks using the else if construction:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;

public class DayPrinter {
   public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      System.out.println("Enter an integer: ");
      int dayNum = in.nextInt();

      String day;
      if (dayNum == 0) {
        day = "Sunday";
      } else if (dayNum == 1){
        day = "Monday";
      } else if (dayNum == 2){
        day = "Tuesday";
      } else if (dayNum == 3){
        day = "Wednesday";
      } else if (dayNum == 4){
        day = "Thursday";
      } else if (dayNum == 5){
        day = "Friday";
      } else if (dayNum == 6){
       day = "Saturday";
      } else {
       day = "Int does not correspond to a day of the week";
      }
      System.out.println(day);
   }
}

3.1.5.1. Fallthrough

Additionally, if break statements are omitted from the individual cases on accident, a behavior known as fallthrough is carried out. Fallthrough can be quite unintuitive, and is only desirable in very specific circumstances. We will discuss break statements in more detail in the loop section below. For now, just know that when used in a switch block, they terminate the switch statement they are in, so the flow of control in your program moves to the next statement after the switch block.

Here’s a quick example of how fallthrough works:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class DayPrinter {
   public static void main(String[] args) {

      System.out.println("Enter an integer: ");
      Scanner in = new Scanner(System.in);
      int dayNum = in.nextInt();

      String day;
      switch (dayNum) {
         case 0:
            day = "Sunday";
         case 1:
            day = "Monday";
         case 2:
            day = "Tuesday";
         case 3:
            day = "Wednesday";
         case 4:
            day = "Thursday";
         case 5:
            day = "Friday";
         case 6:
            day = "Saturday";
         default:
            // in this example, this block runs even if one of the above blocks match
            day = "Int does not correspond to a day of the week";
      }
      System.out.println(day);
   }
}

This time, without the break statements in each case, if the user enters 4, they will see the default output:

Enter an integer: 4
Int does not correspond to a day of the week

This is because after the switch statement matches the case for 4 and assigns the value Thursday to the variable day, it proceeds to execute every statement in every case that follows, all the way through the default case. So the String that ends up being printed will reflect the last executed statement in the switch block.

Along similar lines, consider this variation on the code block above:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class DayPrinter {
   public static void main(String[] args) {

      System.out.println("Enter an integer: ");
      Scanner in = new Scanner(System.in);
      int dayNum = in.nextInt();

      String day;
      switch (dayNum) {
         case 0:
            day = "Sunday";
         case 1:
            day = "Monday";
         case 2:
            day = "Tuesday";
         case 3:
            day = "Wednesday";
         case 4:
            day = "Thursday";
         case 5:
            day = "Friday";
         case 6:
            day = "Saturday";
            break;
         default:
            day = "Int does not correspond to a day of the week";
      }
      System.out.println(day);
   }
}

Here, we have a break statement in case 6 after day = "Saturday";. If the user enters 4, the execution will fallthrough until it reaches that break statement and Saturday is printed instead of Thursday. The output:

Enter an integer: 4
Saturday

3.1.7. Check Your Understanding

Question

When does fallthrough occur in Java?

  1. Omitting an else clause from a conditional.

  2. Omitting an else clause from switch statement.

  3. Omitting a default case from a switch statement.

  4. Omitting a break line from a switch statement.

Question

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;

public class QuizQuestion {
   public static void main(String[] args) {

      System.out.println("Are you a space cadet? yes or no");
      Scanner in = new Scanner(System.in);
      String response = in.next();

      switch (response) {
         case "yes":
            System.out.println("Greetings cadet.");
         case "no":
            System.out.println("Greetings normie.");
         default:
            System.out.println("Are you an alien?");
      }
   }
}

Given the code above, what prints if the user enters no after the prompt?

  1. Greetings cadet.
    
  2. Greetings normie.
    
  3. Greetings normie.
    Are you an alien?
    
  4. Greetings cadet.
    Greetings normie.