Recall & Review
beginner
What is the ternary operator in C?
The ternary operator is a shorthand way to write an if-else statement. It uses the syntax: condition ? expression_if_true : expression_if_false;
Click to reveal answer
beginner
How does the ternary operator work in C?
It evaluates the condition first. If the condition is true, it returns the first expression; if false, it returns the second expression.
Click to reveal answer
beginner
Rewrite this if-else using the ternary operator:<br>
if (a > b) {
max = a;
} else {
max = b;
}Using ternary operator:<br>
max = (a > b) ? a : b;
Click to reveal answer
intermediate
Can the ternary operator be nested in C?
Yes, you can nest ternary operators inside each other, but it can make code harder to read. Example:<br>
result = (a > b) ? a : ((b > c) ? b : c);
Click to reveal answer
beginner
What is a common use case for the ternary operator?
It is often used for simple conditional assignments or returning values in a concise way instead of writing full if-else blocks.
Click to reveal answer
What does the ternary operator syntax look like in C?
✗ Incorrect
The ternary operator uses the syntax: condition ? expression_if_true : expression_if_false.
What value does the ternary operator return if the condition is false?
✗ Incorrect
If the condition is false, the ternary operator returns the second expression after the colon.
Which of these is a correct use of the ternary operator?
✗ Incorrect
Option B correctly uses the ternary operator syntax.
Is it recommended to use nested ternary operators?
✗ Incorrect
Nested ternary operators are allowed but can make code harder to read.
What is a benefit of using the ternary operator?
✗ Incorrect
The ternary operator simplifies writing simple conditional assignments in a concise way.
Explain how the ternary operator works and write a simple example in C.
Think of it as a short if-else that returns a value.
You got /4 concepts.
Describe when you might prefer using the ternary operator instead of a full if-else statement.
Use it when you want to assign a value quickly based on a condition.
You got /3 concepts.