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
- 1.2.B.3 Ternary Conditional Operator
Skills
- S2.A Write program code and implement algorithms.
- S2.C Analyze an algorithm and program code for correctness.
Student Outcomes
Students will be able to:
- use the ternary conditional operator to make decisions in the program
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
- Have students modify if statements they have written in other projects to use the Ternary Conditional Operator instead.
- Create your own parsons type problems. Put a code description along with the expressions needed to write the code into a zip-lock bag and have students assemble as part of a class warm-up.
- Provide students with prompts and have them write both the if and ternary solutions side-by-side.
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
- Ternary Conditional Operators are OUTSIDE the #APCSA Java Subset.
- Students can write correct solutions using the Ternary Conditional Operator
- This operator can be used in any FRQ, but may be seen more often in Question 1.
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:
- generate a random number to determine if a bear visited the bird feeder.
- If a bear came to the feeder, the value of currentFood would be set to 0.
- If there is no bear, then another number is simulate the amount of food each bird will consume.
- If the amount of food the birds consumed exceeds the amount in the feeder, the value of currentFood is 0, otherwise the amount consumed is subtracted from currentFood.
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
- Student solutions that use correct Java syntax can earn full credit for their solution
- Questions are written with an in AP Java Subset solution in mind
- Students should be encouraged to write solutions within the AP Java Subset