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

bookTernary 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.

Task

Swipe to start coding

  1. Find the largest value of variables x, y and z using nested ternary operators.
  2. Output it in the console.

Solution

solution.cpp

solution.cpp

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you give an example of a ternary operator in code?

What are some common mistakes when using ternary operators?

When should I use an if-else statement instead of a ternary operator?

close

Awesome!

Completion rate improved to 7.69

bookTernary Operator

Swipe to show menu

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.

Task

Swipe to start coding

  1. Find the largest value of variables x, y and z using nested ternary operators.
  2. Output it in the console.

Solution

solution.cpp

solution.cpp

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 1
single

single

some-alt