Click here to navigate the course.

Drag the edges to resize the window.

In Projects, you can keep track of your progress as you go throught the tasks. Check each item as you complete it!

Code Editor
Learn

if/else statements can become lengthy even when you simply want to return a value depending on a Boolean expression. Fortunately, Java provides a shortcut that allows you to write if/else statements in a single line of code. It is called the ternary conditional statement.

The term ternary comes from a Latin word that means "composed of three parts".

These three parts are:

  1. A Boolean expression
  2. A single statement that gets executed if the Boolean expression is true
  3. A single statement that gets executed if the Boolean expression is false

Here is an example of a ternary conditional statement:

int pointsScored = 21; char gameResult = (pointsScored > 20) ? 'W' : 'L'; System.out.println(gameResult);

In the example above, the int variable called pointsScored is equal to 21.

The Boolean expression is (pointsScored > 20), which evaluates to true. This will return the value of 'W', which is assigned to the variable gameResult. The value 'W' is printed to the console.

Report a Bug
If you see a bug or any other issue with this page, please report it here.