Conditional And Control Flow

Boolean operators

  1. There are three Boolean operators that we will explore. Let’s start with the first one: and.

    1. The and operator is represented in Java by &&.
    2. It returns a boolean value of true only when the expressions on both sides of && are true.
  2. Great! The second Boolean operator that we will explore is called or.

    1. The or operator is represented in Java by ||.
    2. It returns a Boolean value of true when at least one expression on either side of || is true.
  3. The final Boolean operator we will explore is called not.

    1. The not operator is represented in Java by !.
    2. It will return the opposite of the expression immediately after it. It will return false if the expression is true, and true if the expression is false.

The precedence of each Boolean operator is as follows:

  1. ! is evaluated first
  2. && is evaluated second
  3. || is evaluated third

If Statement

Let’s get familiar with how relational, equality, and Boolean operators can be used to control the flow of our code.

We’ll start by exploring the ifstatement.

  1. In Java, the keyword if is the first part of a conditional expression.
  2. It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to true, the block of code that follows will be run.

Here’s an example of the ifstatement used with a conditional expression:

1
2
3
if (9 > 2) {
    System.out.println("Control flow rocks!");
}

If-Else Statement

Sometimes we execute one block of code when the Boolean expression after the ifkeyword is true. Other times we may want to execute a different block of code when the Boolean expression is false.

We could write a second ifstatement with a Boolean expression that is opposite the first, but Java provides a shortcut called the if/elseconditional.

  1. The if/else conditional will run the block of code associated with the ifstatement if its Boolean expression evaluates to true.
  2. Otherwise, if the Boolean expression evaluates to false, it will run the block of code after the elsekeyword.

Here’s an example of if/elsesyntax:

1
2
3
4
5
if (1 < 3 && 5 < 4) {
    System.out.println("I defy the Boolean laws!")
} else {
    System.out.println("You can thank George Boole!");
}

If-ElseIf-Else Statement

Good work! In some cases, we need to execute a separate block of code depending on different Boolean expressions. For that case, we can use the if/else if/else statement in Java.

  1. If the Boolean expression after the if statement evaluates to true, it will run the code block that directly follows.
  2. Otherwise, if the Boolean expression after the else if statement evaluates to true, the code block that directly follow will run.
  3. Finally, if all previous Boolean expressions evaluate to false, the code within the else block will run.

Here’s an example of control flow with the if/else if/elsestatement:

1
2
3
4
5
6
7
8
9
int shoeSize = 10;

if (shoeSize > 12) {
    System.out.println("Sorry, your shoe size is currently not in stock.");
} else if (shoeSize >= 6) {
    System.out.println("Your shoe size is in stock!");
} else {
    System.out.println("Sorry, this store does not carry shoes smaller than a size 6.");
}

Ternary Conditional

if/else statements can become lengthy even when you simply want to return a value depending on a Boolean expression. Fortunately, Java provides a shortcut that allows you to write if/elsestatements in a single line of code. It is called the ternary conditional statement.

The term ternary comes from a Latin word that means “composed of three parts”.

These three parts are:

  1. A Boolean expression
  2. A single statement that gets executed if the Boolean expression is true
  3. A single statement that gets executed if the Boolean expression is false

Here is an example of a ternary conditional statement:

1
2
3
4
int pointsScored = 21;

char gameResult = (pointsScored > 20) ? 'W' : 'L';
System.out.println(gameResult);

Switch Statement

The conditional statements that we have covered so far require Boolean expressions to determine which code block to run. Java also provides a way to execute code blocks based on whether a block is equal to a specific value. For those specific cases, we can use the switchstatement, which helps keep code organized and less wordy.

The switch statement is used as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
int restaurantRating = 3;

switch (restaurantRating) {

    case 1: System.out.println("This restaurant is not my favorite.");
      break;

    case 2: System.out.println("This restaurant is good.");
      break;

    case 3: System.out.println("This restaurant is fantastic!");
      break;

    default: System.out.println("I've never dined at this restaurant.");
      break;
}