Skip to content

Conditionals and Control Flow

Intoduction to Control Flow

Imagine we're writing a program that enrolls students in courses.

  • If a student has completed the prerequisites, then they can enroll in a course.

  • Else, they need to take the prerequisite courses.

For example, a student cannot take Physics II without first completing Physics I.

We represent this kind of decision-making in our program using conditional or control flow statements. Before this point, our code runs line-by-line from the top down, but conditional statements allow us to be selective in which portions will run.

Conditional statements check a boolean condition and run a block of code depending on the condition. Curly braces mark the scope of a conditional block similar to a method or class.

Here's a complete conditional statement:

1
2
3
if (true) {
    System.out.println("Hellow World!");
}

If the condition is true, then the block is run, which results in Hello World! being printed.

But suppose the condition is different:

1
2
3
if (false) {
    System.out.println("Hello World!");
}

If the condition is false, then the block does not run.

This code is also called if-then statements: "If (condition) is true, then do something".

If-Then Statement

The if-then statement is the most simple control flow we can write. It tests an expression for truth and executes some code based on it.

1
2
3
if (flip == 1) {
    System.out.println("Heads!");
}
  • the if keyword marks the beginning of the conditional statement, followed by parentheses ().

  • The parentheses hold a boolean datatype.

For the condition in parentheses we can also use variables that reference a boolean, or comparisons that evaluate to a boolean.

The boolean condition is followed by opening and closing curly braces that mark a block of code. This block runs if, and only if, the boolean is true.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
boolean isValidPassword = true;

if (isValidPassword) {

    System.out.println("Password Accepted!");
}

// prints "Password accepted!"

int numberOfItemsInCart = 9;

if (numberOfItemsInCart > 12) {
    System.out.println("Express checkout not available");
}

// nothing is printed

If a conditional is brief we can omit the curly braces entirely:

1
if (true) System.out.println("Brevity is the soul of wit");

If-Then-Else

We've seem how to conditionally execute one block of code, but what if there are two possible blocks of code we'd like to execute?

Let's say if a student has the required prerequisite, then they enroll in the selected course, else they're enrolled in the prerequisite course instead.

We create an alternate conditional branch with the else keyword:

1
2
3
4
5
if (hasPrerequisite) {
    // enroll in course
} else {
    // enroll in prerequisite
}

This conditional statement ensures that exactly one code block will be run. If the condition hasPrerequisite, is false, the block after else runs.

There are now two separate code blocks in our conditional statement. The first block runs if the condition evaluates to true, the second block runs if the condition evaluates to false.

This code is also called an if-then-else statement:

  • If condition is true, then do something.

  • Else, do a different thing.

If-Then-Else-If

The conditional structure we've learned can be chained together to check as many conditions as are required by our program.

Imagine our program is now selecting the appropriate course for a student. We'll check their submission to find the correct course enrollment.

The conditional statement now has multiple conditions that are evaluated from the top down:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
String course = "Theatre";

if (course.equals("Biology")) {
    // enroll in Biology course
} else if (course.equals("Algebra")) {
    // enroll in Algebra course
} else if (course.equals("Theatre")) {
    // enroll in Theatre course
} else {
    System.out.println("Course not found!");
}

The first condition to evaluate to true will have that code block run. Here's an example demonstrating the order:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
int testScore = 72;

if (testScore >= 90){
    System.out.println("A");
} else if (testScore >= 80) {
    System.out.println("B");
} else if (testScore >= 70) {
    System.out.println("C");
} else if (testScore >= 60) {
    System.out.println("D");
} else {
    System.out.println("F");
}

This chained conditional statement has two conditions that evaluate to true. Because testScore >= 70 comes before testScore >= 60, only the earlier code block is run.

Note: Only one of the code blocks will run.

Nested Conditional Statements

We can create more complex conditional structures by creating nested contitional statements, which is created by placing conditional statements inside other conditional statements:

1
2
3
4
5
if (outer condition) {
    if (nested condition) {
        // instruction to execute if both conditions are true
    }
}

When we implement nested conditional statements, the outer statement is evaluated first. If the outer condition is true, then the inner, nested statement is evaluated.

Let's create a program that helps us decide what to wear based on the weather:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
int temp = 45;
boolean raining = true;

if (temp < 60) {
    System.out.println("Wear a jacket!");
    if (raining == true) {
        System.out.println("Bring your umbrella!");
    } else {
        System.out.println("Leave your umbrella at home.");
    }
}

In the code snippet above, our compiler will check the condition in the first if-then statement: temp < 60. Since temp has a value of 45, this condition is true; therefore, our program will print Wear a jacket!.

Then, we'll evaluate the condition in the nested if-then statement: raining == true. This condition is also true, so Bring your umbrella is also printed to the screen.

Note that, if the first condition was false, the nested condition would not be evaluated.

Switch Statement

An alternative to chaining if-then-else conditions together is to use the switch statement. This conditional will check a given value against any number of conditions and run the code block where there is a match.

Here's an example of our course selection conditional as a switch statement instead:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
String course = "History";

switch (course) {
    case "Algebra":
        // Enroll in Algebra
        break;
    case "Biology":
        // Enroll in Biology
        break;
    case "History":
        // Enroll in History
        break;
    case "Theatre":
        // Enroll in Theatre
        break;
    default:
        System.out.println("Course not found");
}

This example enrolls the student in History class by checking the value contained in the parentheses, course, against each of the case labels. If the value after the case lable matches the value within the parentheses, the switch block is run.

In the above example, course references the string "History", which matches case "History":.

When no value matches, the default block runs. Think of this as the else equivalent.

Switch blocks are different than other code blocks because they are not marked by curly braces and we use the break keyword to exit the switch statement.

Without break, code below the matching case label is run, including code under other case labels, which is rareley the desired behavior.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
String course = "Biology";

switch (course) {
  case "Algebra":
    // Enroll in Algebra
  case "Biology":
    // Enroll in Biology
  case "History":
    // Enroll in History
  case "Theatre":
    // Enroll in Theatre
  default:
    System.out.println("Course not found");
}

// enrolls student in Biology... AND History and Theatre!

Conditional/Control Flow Review

Conditional statements add branching paths to our programs. We use conditionals to make decisions in the program so that different inputs will produce different results.

Conditionals have the general structure:

1
2
3
4
5
if (condition) {
    // consequent path
} else {
    // alternate path
}

Specific conditional statements have the following behavior:

  • if-then:

    • Code block runs if the condition is true.
  • if-then-else:

    • One block runs if condition is true.

    • Another block runs if condition is false.

  • if-then-else chained:

    • Same as if-then but an arbitrary number of conditions.
  • switch:

    • Switch block runs if condition matches the case value.

Introduction to Conditional Operators

Java includes operators that only use boolean values. These conditional operators help simplify expressions containing complex boolean relationships by reducing multiple boolean values to a single use: true or false.

For example, what if we want to run a code block only if multiple conditions are true. We could use the AND operator: &&.

Or, we want to run a code block if at least one of two conditions are true. We could use the OR operator: ||.

Finally, we can produce the opposite value, where true becomes false and false becomes true, with the NOT operator: !.

In the following notes, we'll go over each of these conditional operators to see how they can be implemented into our conditional statements.

AND

A B A&&B
true true true
true false false
false true false
false false false

OR

A B A||B
true true true
true false true
false true true
false false false

NOT

A !A
true false
false true

Conditional-AND: &&

Let's return to our student enrollment program. We've added an additional requirement: not only must students have the prerequisite, but their tuition must be paid up as well. We have two conditionals that must be true before we enroll the student.

Here is one way we can write the code:

1
2
3
4
5
if (tuitionPaid) {
    if (hasPrerequisite){
        // enroll student
    }
}

We've nested two if-then statements. This does the job but we can be more concise with the AND operator:

1
2
3
if (tuitionPaid && hasPrerequisite){
    // enroll student
}

The AND operator, &&, is used between two boolean values and evaluates to a single boolean value. If the values on both sides are true, then the resulting value is true, otherwise the resulting value is false.

This code illustrates every combination:

true && true
// true
false && true
// false
true && false
// false
false && false
// false

Conditional-OR: ||

The requirements of our enrollment program have changed again. Certain courses have prerequisites that are satisfied by multiple courses. As long as students have taken at least one prerequisite, they should be allowed to enroll.

Here's one way we could write the code:

1
2
3
4
5
6
7
if (hasAlgebraPrerequisite){
    // enroll student
}

if (hasGeometryPrerequisite){
    // enroll student
}

We're using two if-then statements with the same code block. We can be more concise with the OR operator:

1
2
3
if (hasAlgebraPrerequisite || hasGeometryPrerequisite){
    // enroll student
}

The OR operator, ||, is used between two boolean values and evaluates to a single boolean value. If either of the two values are true, then the resulting value is true, otherwise the resulting value is false.

This code illustrates every combination:

true || false
// true
true || false
// true
false || true
// true
false || false
// false

Logical NOT: !

The unary operator NOT, !, works on a single value. This operator evaluates to the opposite boolean to which it is applied:

!false
// true
!true
// false

NOT is useful for expressing our intent clearly in programs. For example sometimes we need the opposite behavior of an if-then: run a code block only if the condition is false.

1
2
3
4
5
6
7
boolean hasPrerequisite = false;

if (hasPrerequisite) {
    // do nothing
} else {
    System.out.println("Must complete prerequisite course!");
}

This code does what we want but it's strange to have a code block that does nothing!

The logical NOT operator cleans up our example:

1
2
3
4
5
boolean hasPrerequisite = false;

if (!hasPrerequisite){
    System.out.println("Must complete prerequisite course!");
}

We can write a succint conditional statement without an empty code block.

Combining Conditional Operators

We have the ability to expand our boolean expressions by using multiple conditional operators in a single expression.

For example:

boolean foo = true && !(false || !true)

How does an expression like this get evaluated by the compiler? The order of evaluation when it comes to conditional operators is as follows:

  1. Conditions placed in parentheses - {}

  2. NOT - !

  3. AND - &&

  4. OR - ||

Using this information, let's dissect the expression above to find the value of foo:

true && !(false || !true)

First, we'll evaluate the (false || !true) because it is enclosed within parentheses. Following the order of evaluation, we will evaluate !true, which equals false:

true && !(false || false)

Then, we'll evaluate (false || false) which equals false. Now our expression looks like this:

true && !false

Next, we'll evaluate !false because it uses the NOT operator. This expression equals true making our expression the following:

true && true

true && true evaluates to true; therefore, the value of foo is true.

Conditional Operators Review

Conditional Operators work on boolean values to simplify our code. They're often combined with conditional statements to consolidate the branching logic.

Conditional-AND, &&, evaluates to true if the booleans on both sides are true.

Conditional-OR, ||, evaluates to true if one or both oif the booleans on either side is true.

Conditional-NOT, !, evaluates to the opposite boolean value to which it is applied.