FAQ: Conditionals and Control Flow - Ternary Conditional


This community-built FAQ covers the “Ternary Conditional” exercise from the lesson “Conditionals and Control Flow”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Java

FAQs on the exercise Ternary Conditional

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


why they mentioned ? mark in trenary operator


its part of the ternary operator syntax:

condition ? true : false
1 Like

Is the ternary operator used often?


Often is relative. But the ternary operator is certainly used.


how come in the sample question they use char canDrive = (fuelLevel > 0) ? ‘Y’ : ‘N’;

I’m confused as to why it’s a char variable and not a boolean variable, or does this matter?


'y' and 'n' are characters, not boolean values. Which is why you use char

1 Like

Often is relative. But the ternary operator is certainly used.

char canDrive = (fuelLevel > 0) ? 'Y' : 'N';

never met “?”, “:” those symbols together,
why instead of : we can’t use || (or) operator?


A tenary is akin to an if…else statement.

if (x > ) {
    y = 'Y'
} else {
    y = 'N'

This is not a logical expression, as such, and neither is a ternary.

variable = condition ? response_when_true : default_response;

The nice thing about a ternary expression is it can be a single conditional assignment.