Bird
Raised Fist0
Swiftprogramming~10 mins

Ternary conditional operator in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Ternary conditional operator
Evaluate condition
Yes↓
Use value if true
↓
End
No↓
Use value if false
↓
End
The ternary operator checks a condition and chooses one of two values based on true or false.
Execution Sample
Swift
let age = 20
let canVote = age >= 18 ? "Yes" : "No"
print(canVote)
This code checks if age is 18 or more and sets canVote to "Yes" or "No" accordingly.
Execution Table
StepCondition (age >= 18)ResultValue chosenOutput
120 >= 18true"Yes"canVote = "Yes"
2End--Prints: Yes
💡 Condition true, so "Yes" chosen and printed.
Variable Tracker
VariableStartAfter Step 1Final
age202020
canVoteundefined"Yes""Yes"
Key Moments - 2 Insights
Why does canVote get "Yes" and not "No"?
Because the condition age >= 18 is true at step 1 in the execution table, so the true value "Yes" is chosen.
What if age was 16? What value would canVote get?
If age was 16, the condition would be false, so the false value "No" would be chosen instead, as shown by the ternary operator logic in the concept flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of canVote after step 1?
A"No"
B"Yes"
Cundefined
D20
💡 Hint
Check the 'Value chosen' and 'Output' columns in row 1 of the execution table.
At which step does the program print the output?
AStep 2
BBefore step 1
CStep 1
DNo output printed
💡 Hint
Look at the 'Output' column in the execution table rows.
If age was 17, what would the ternary operator choose?
A"Yes"
Bundefined
C"No"
DError
💡 Hint
Refer to the concept flow and key moments about condition false choice.
Concept Snapshot
Ternary conditional operator syntax:
condition ? valueIfTrue : valueIfFalse
It evaluates the condition once.
If true, returns valueIfTrue.
If false, returns valueIfFalse.
Used for simple if-else in one line.
Full Transcript
The ternary conditional operator in Swift lets you pick one of two values based on a condition. First, the condition is checked. If it is true, the value after the question mark is chosen. If false, the value after the colon is chosen. For example, if age is 20, the condition age >= 18 is true, so canVote becomes "Yes". Then the program prints "Yes". If age was less than 18, canVote would be "No". This operator is a shortcut for simple if-else statements and helps write cleaner code.

Practice

(1/5)
1. What does the ternary conditional operator do in Swift?
condition ? valueIfTrue : valueIfFalse
easy
A. Declares a constant value
B. Repeats a block of code multiple times
C. Defines a new function
D. Chooses one of two values based on a condition

Solution

  1. Step 1: Understand the operator structure

    The ternary operator uses a condition followed by a question mark, then two values separated by a colon.
  2. Step 2: Identify its purpose

    It selects the first value if the condition is true, otherwise the second value.
  3. Final Answer:

    Chooses one of two values based on a condition -> Option D
  4. Quick Check:

    Ternary operator = chooses between two values [OK]
Hint: Remember: condition ? trueValue : falseValue picks one [OK]
Common Mistakes:
  • Confusing ternary with loops
  • Thinking it declares variables
  • Mixing it with function syntax
2. Which of the following is the correct syntax for the ternary conditional operator in Swift?
easy
A. condition ? valueIfTrue : valueIfFalse
B. condition : valueIfTrue ? valueIfFalse
C. condition ? (valueIfTrue, valueIfFalse)
D. condition ? valueIfTrue; valueIfFalse

Solution

  1. Step 1: Recall the ternary syntax

    The correct syntax places the condition first, then a question mark, then the true value, a colon, and the false value.
  2. Step 2: Compare options

    Only condition ? valueIfTrue : valueIfFalse matches this exact pattern.
  3. Final Answer:

    condition ? valueIfTrue : valueIfFalse -> Option A
  4. Quick Check:

    Syntax is condition ? true : false [OK]
Hint: Question mark before colon is key in ternary syntax [OK]
Common Mistakes:
  • Swapping question mark and colon
  • Using commas instead of colon
  • Adding semicolons inside ternary
3. What is the output of this Swift code?
let age = 20
let canVote = age >= 18 ? "Yes" : "No"
print(canVote)
medium
A. No
B. Yes
C. true
D. false

Solution

  1. Step 1: Evaluate the condition

    age is 20, which is greater than or equal to 18, so the condition is true.
  2. Step 2: Apply ternary operator

    Since the condition is true, canVote is assigned "Yes".
  3. Final Answer:

    Yes -> Option B
  4. Quick Check:

    age >= 18 is true, so output is "Yes" [OK]
Hint: Check condition result first, then pick true or false value [OK]
Common Mistakes:
  • Confusing true and false branches
  • Assuming boolean output instead of string
  • Ignoring the condition value
4. Identify the error in this Swift code using the ternary operator:
let score = 85
let grade = score > 90 ? "A" : "B" else "C"
medium
A. Using double quotes instead of single quotes
B. Missing parentheses around condition
C. Extra 'else' keyword after ternary operator
D. Incorrect variable declaration

Solution

  1. Step 1: Analyze ternary operator usage

    The ternary operator already includes both true and false values separated by a colon; adding 'else' is invalid syntax.
  2. Step 2: Identify the syntax error

    The 'else' keyword after the ternary expression causes a syntax error.
  3. Final Answer:

    Extra 'else' keyword after ternary operator -> Option C
  4. Quick Check:

    No 'else' after ternary operator [OK]
Hint: Ternary uses colon, never 'else' keyword [OK]
Common Mistakes:
  • Adding 'else' after ternary
  • Confusing ternary with if-else blocks
  • Misplacing colons and question marks
5. Given a dictionary of student scores, use a ternary operator in Swift to create a new dictionary that marks each student as "Pass" if their score is 50 or more, otherwise "Fail".
What is the correct way to write this using dictionary comprehension?
let scores = ["Anna": 75, "Ben": 45, "Cara": 50]
let results = scores.mapValues { score in
    // Fill in here
}
hard
A. score >= 50 ? "Pass" : "Fail"
B. if score >= 50 { "Pass" } else { "Fail" }
C. score > 50 ? "Pass" : "Fail"
D. score >= 50 ? "Fail" : "Pass"

Solution

  1. Step 1: Understand the condition for passing

    Students pass if their score is 50 or more, so condition is score >= 50.
  2. Step 2: Apply ternary operator correctly

    Use ternary to return "Pass" if true, else "Fail".
  3. Final Answer:

    score >= 50 ? "Pass" : "Fail" -> Option A
  4. Quick Check:

    Use condition ? "Pass" : "Fail" for mapping [OK]
Hint: Use mapValues with ternary: condition ? trueVal : falseVal [OK]
Common Mistakes:
  • Using if-else instead of ternary in mapValues
  • Wrong comparison operator (>)
  • Swapping Pass and Fail values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard