Skip Top Navigation Bar

Ternary Conditional Operator Lesson Plan

Overview

In this lesson, students will learn how to use ternary conditional operator instead of if statements in programs.

Learning Objectives

Skills

Student Outcomes

Students will be able to:

Duration: 1 class periods

Resources

Videos


Warm-up / Motivate

Have students work in pairs to write out the if statement that could be used to determine the cost of the an item ordered at an ice cream shop based on whether the item is ice cream or sorbet. An ice cream shop has two options for customers: ice cream and sorbet. If the customer chooses ice cream, they are charged $5. If the customer chooses sorbet, they are charged $6.

There are many ways that students can choose to solve this problem. For example, students might choose to use a String variable for the item choice or identify int values to represent these String options: 1 for ice cream, 2 for sorbet. Allow the students to make these decisions on their own. Provide an opportunity for students to share the variety of solutions they developed. Some examples are below:

String item = "ice cream";
double cost = 0;
if (item.equals("ice cream")) {
    cost = 5;
} else {
    cost = 6;
}

Here is an alternative solution that would also work.

int item = 1; //if item is 1 it is ice cream, if it is 2 it is sorbet.
double cost = 6;
if (item == 1) {
    cost = 5;
}

While this solution is correct, it opens the door to discuss how the code would need to be altered if the ice cream shop decided to start serving another item, such as frozen yogurt for $5.50.

The solutions that students develop will probably consist of about 5 lines for the if statement. For simple tasks, such as assign a value to a variable based on a choice, there is a more readable and simplified option, that is using a ternary conditional operator instead.

Learn

Either as a group or on their own, have students complete the tutorial: Learn: Ternary Conditional Operator

Instructional Ideas to Engaging Students

Wrap-up / Extension

Have students revisit some of the programming projects or exercises that they completed with if..statements and determine which could be re-written using the ternary conditional operator. For those where a student would not recommend using the ternary conditional operator instead, have them justify this decision.

Ternary Conditional Operators and the AP CSA Free Response Questions

Things to Note on Using Ternary Conditional Operator and the AP CSA FRQs

Example 1: 2024 Free Response Question 1

In 2024, question 1 can be re-written using the ternary conditional operator. The full question and solution can be found here.

To summarize, this question has a class called Feeder that has an instance variable currentFood and two methods simulateOneDay and simulateManyDays. Students are asked to implement these two methods. We are looking at writing the simulateOneDay method.

To simulate a day at the bird feeder, students will need to do the following:

An example solution is as follows. Run it several times to see the results:

This following part of the if statement can be re-written using the ternary conditional operator.

The original if statement:

if (birdConsumed >= currentFood) {
    currentFood = 0;
}
else {
    currentFood = currentFood - birdConsumed;
}

Re-written as:

currentFood = (birdConsumed >= currentFood) ? 0 : (currentFood – birdConsumed);

This ternary conditional operator expression is nested inside another if statement as follows:

if (bearPresent) {
    currentFood = 0;
}
else {
    currentFood = (birdConsumed >= currentFood) ? 0 : (currentFood – birdConsumed);
}

This can be re-written using nested ternary conditional operators as following. Run it several times to see the results:

Scoring and Teaching