Main Page

Previous Next

The Conditional Operator

The conditional operator is sometimes called a ternary operator because it involves three operands. It is best understood by looking at an example. Suppose we have two variables of type int, yourAge and myAge, and we want to assign the greater of the values stored in yourAge and myAge to a third variable also of type int, older. We can do this with the statement:

older = yourAge > myAge ? yourAge : myAge;

The conditional operator has a logical expression as its first argument, in this case yourAge>myAge. If this expression is true, the operand which follows the ? symbol – in this case yourAge – is selected as the value resulting from the operation. If the expression yourAge>myAge is false, the operand which comes after the colon – in this case myAge – is selected as the value. Thus, the result of this conditional expression is yourAge, if yourAge is greater than myAge, and myAge otherwise. This value is then stored in the variable, older. The use of the conditional operator in this assignment statement is equivalent to the if statement:

if(yourAge > myAge) {
  older = yourAge;

} else {
  older = myAge;

Remember, though, the conditional operator is an operator and not a statement, so it can be used in a more complex expression involving other operators.

The conditional operator can be written generally as:

logical_expression ? expression1 : expression2

If the logical_expression evaluates as true, the result of the operation is the value of expression1, and if logical_expression evaluates to false, the result is the value of expression2. Note that if expression1 is evaluated because logical_expression is true, then expression2 will not be, and vice versa.

There are lots of circumstances where the conditional operator can be used, and one common application of it is to control output, depending on the result of an expression or the value of a variable. You can vary a message by selecting one text string or another depending on the condition specified.

Try It Out – Conditional Plurals

Type in the following code which will add the correct ending to 'hat' depending on how many hats you have:

public class ConditionalOp {
  public static void main(String[] args) {
    int nHats = 1;     // Number of hats
    System.out.println("I have " + nHats + " hat" + (nHats == 1 ? "." : "s."));

    nHats++;           // Increment number of hats
    System.out.println("I have " + nHats + " hat" + (nHats == 1 ? "." : "s."));

The output from this program will be:

I have 1 hat.
I have 2 hats.

How It Works

The result of the conditional operator is a string containing just a period when the value of nHats is 1, and a string containing an s followed by a period in all other cases. The effect of this is to cause the output statement to automatically adjust the output between singular and plural. You can use the same technique in other situations such as where you need to choose "he" or "she" for example, as long as you are able to specify a logical expression to differentiate the situation where you should use one rather than the other.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor