The Ternary Conditional Operator
Choose between two values concisely.
The Ternary Conditional Operator is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.
What Is the Ternary Operator
The ternary conditional operator chooses between two values based on a condition. Its form is condition ? valueIfTrue : valueIfFalse.
It is a compact alternative to a simple if/else.
let isMember = true
let price = isMember ? 8 : 12
print(price)How It Reads
Read the ternary as a question: "Is the condition true? If yes, use the first value, otherwise use the second."
let temperature = 30
let message = temperature > 25 ? "Hot" : "Mild"
print(message)Replacing if/else
Anywhere you assign a value in both branches of an if/else, a ternary can do the same in one line.
let count = 0
let status = count == 0 ? "Empty" : "Has items"
print(status)Ternary in print
Because a ternary is an expression that produces a value, you can use it directly inside a function call like print.
let hour = 14
print(hour < 12 ? "Morning" : "Afternoon")Numeric Ternaries
The ternary works with any type, including numbers. Here we pick a discount amount based on a condition.
let quantity = 12
let discount = quantity >= 10 ? 5 : 0
let total = quantity * 2 - discount
print(total)Both Branches Same Type
Both result values of a ternary must be the same type. You cannot return a String in one branch and an Int in the other.
let pass = true
let grade = pass ? "Pass" : "Fail"
print(grade)Ternary With Bool Result
A ternary can even produce a Bool, though for pure Bool logic the operators are usually clearer.
let level = 3
let unlocked: Bool = level >= 3 ? true : false
print(unlocked)Using Computed Conditions
The condition can be any Boolean expression, including a combination with && or ||.
let age = 17
let hasPermit = true
let canDrive = (age >= 18 || hasPermit) ? "Yes" : "No"
print(canDrive)Ternary in String Interpolation
You can embed a ternary inside string interpolation to choose a word inline.
let apples = 1
let word = apples == 1 ? "apple" : "apples"
print("You have \(apples) \(word)")Assigning to a Variable
A common use is computing an initial value for a variable based on some input.
let isDark = true
var background = isDark ? "black" : "white"
print(background)
background = "gray"
print(background)Ternary Returning From a Function
A function can return a ternary expression directly, keeping the body to a single line.
func label(for score: Int) -> String {
return score >= 50 ? "Pass" : "Fail"
}
print(label(for: 72))
print(label(for: 30))Quick Check
Check your ternary knowledge.
Recap: The Ternary Operator
The ternary condition ? a : b is a concise expression for picking one of two values. Both branches must share a type, and it works great inside print, interpolation, and return statements.
let online = false
print(online ? "Connected" : "Offline")Learn Swift with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 122
- Lessons
- 409
Frequently Asked Questions
Is the “The Ternary Conditional Operator” lesson free?
Yes — any 3 lessons of the Swift Academy learning path, including “The Ternary Conditional Operator”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.
What will I learn in “The Ternary Conditional Operator”?
Choose between two values concisely. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Swift Academy?
No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “The Ternary Conditional Operator” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Swift Academy lesson?
Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Bool Values and Expressions
- The Ternary Conditional Operator
- Avoiding Nested Ternaries
- Boolean Methods and Toggling