Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Challenge: Ternary Operator | Advanced Topics
C++ Conditional Statements

bookChallenge: Ternary Operator

The ternary operator is a powerful tool that often used to make quick decisions in your code and can greatly improve code readability when used appropriately.

ternary.h

ternary.h

copy
1
condition ? expression_if_true : expression_if_false;

The condition is evaluated. If the condition is true, the expression before the : is executed, otherwise, the expression after the : is executed.

The ternary operator is essentially just an alternative to the if-else statement, providing the code readability and convenience.

if_else.h

if_else.h

ternary.h

ternary.h

copy
123456789
int variable; if (condition) { variable = 25; } else { variable = 10; }

As you can see it using ternary operator can save code space and improve readability and efficiency. However, it is important to save a balance and maintain code maintainability and understandability.

Ternary operators can also be nested to handle more complex conditions. But don't fall into a pitfall full of ternary operatorsю

nested_ternary.h

nested_ternary.h

copy
12345678910
std::string eligibility = age < 18 ? "Too young" : (isStudent ? (hasJob ? "Eligible for student discount" : "Eligible for student perks") : (hasJob ? "Eligible for job-related benefits" : "Not eligible"));

It's really hard to understand what is going on. So it is better to limit yourself to a single ternary operator, with a maximum of one level of nesting at any given time.

Uppgift

Swipe to start coding

You are building a student performance evaluator. Given three test scores, the program should calculate the average score and determine the performance category using a ternary operator.

  1. Inside the function evaluatePerformance, calculate the average of the three scores (score1, score2, score3) by adding them and dividing by 3.0.
  2. Use a ternary operator to determine the performance category based on the average:
    • If the average is 85 or higher -> "Excellent".
    • If the average is 70 or higher but less than 85 -> "Good".
    • If the average is 50 or higher but less than 70 -> "Average".
    • Otherwise -> "Fail".
  3. Return the performance category from the function.

Example

evaluatePerformance(90, 87, 92)"Excellent"
evaluatePerformance(75, 70, 72)"Good"
evaluatePerformance(55, 60, 50)"Average"
evaluatePerformance(40, 45, 30)"Fail"

Lösning

solution.cpp

solution.cpp

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

close

Awesome!

Completion rate improved to 7.69

bookChallenge: Ternary Operator

Svep för att visa menyn

The ternary operator is a powerful tool that often used to make quick decisions in your code and can greatly improve code readability when used appropriately.

ternary.h

ternary.h

copy
1
condition ? expression_if_true : expression_if_false;

The condition is evaluated. If the condition is true, the expression before the : is executed, otherwise, the expression after the : is executed.

The ternary operator is essentially just an alternative to the if-else statement, providing the code readability and convenience.

if_else.h

if_else.h

ternary.h

ternary.h

copy
123456789
int variable; if (condition) { variable = 25; } else { variable = 10; }

As you can see it using ternary operator can save code space and improve readability and efficiency. However, it is important to save a balance and maintain code maintainability and understandability.

Ternary operators can also be nested to handle more complex conditions. But don't fall into a pitfall full of ternary operatorsю

nested_ternary.h

nested_ternary.h

copy
12345678910
std::string eligibility = age < 18 ? "Too young" : (isStudent ? (hasJob ? "Eligible for student discount" : "Eligible for student perks") : (hasJob ? "Eligible for job-related benefits" : "Not eligible"));

It's really hard to understand what is going on. So it is better to limit yourself to a single ternary operator, with a maximum of one level of nesting at any given time.

Uppgift

Swipe to start coding

You are building a student performance evaluator. Given three test scores, the program should calculate the average score and determine the performance category using a ternary operator.

  1. Inside the function evaluatePerformance, calculate the average of the three scores (score1, score2, score3) by adding them and dividing by 3.0.
  2. Use a ternary operator to determine the performance category based on the average:
    • If the average is 85 or higher -> "Excellent".
    • If the average is 70 or higher but less than 85 -> "Good".
    • If the average is 50 or higher but less than 70 -> "Average".
    • Otherwise -> "Fail".
  3. Return the performance category from the function.

Example

evaluatePerformance(90, 87, 92)"Excellent"
evaluatePerformance(75, 70, 72)"Good"
evaluatePerformance(55, 60, 50)"Average"
evaluatePerformance(40, 45, 30)"Fail"

Lösning

solution.cpp

solution.cpp

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
single

single

some-alt