Conditional Operators

FIELDS OF STUDY

Coding Techniques; Software Development; Computer Science

ABSTRACT

Conditional operators perform a similar function in code to other conditional expressions such as the if-then-else construct. Conditional operators can be used to reduce the number of lines of code needed for branching logic. Such refactoring makes code easier to understand, debug, and maintain.

PRINCIPAL TERMS

WHAT IS A CONDITIONAL OPERATOR?

In computer programming, an operand represents the data on which a specific mathematical, relational, or logical operation is to be performed. A conditional operator is an operator that takes three operands. The first operand is a conditional statement, the second operand is the result to return if the condition is true, and the third operand is the result to return if the condition is false. In many programming languages, the conditional operator is the only ternary operator that is commonly used. Because of this, the terms “conditional operator” and “ternary operator” are often used interchangeably.

The conditional operator is a type of conditional expression. Conditional expressions result in different actions being taken depending on whether a Boolean condition evaluates to true or false. A conditional operator executes different blocks of code based on the bool value returned by a Boolean condition. The condition can be a simple Boolean condition, such as x = 5. Alternatively, it can be a complex Boolean expression constructed with Boolean operators, such as x = 5 AND y = 6 AND z = 7.




Along with a comparison operator, conditional operators can be used to combine more than one condition into a single test. True and false examples of integers are provided for the following conditional statements: (x < 3) AND (y > 6),





Along with a comparison operator, conditional operators can be used to combine more than one condition into a single test. True and false examples of integers are provided for the following conditional statements: (x < 3) AND (y > 6), (x < 1) OR (y <= 10), and the statement NOT (x = 6).

In programming code, conditional operators perform a similar function to that of other conditional expressions, such as the if-then-else construct. They can control program flow and implement conditional logic as needed by the application.

In many programming languages, the conditional operator uses the following syntax:




Conditional Operators

Conditional operators can be nested in parentheses in order to test multiple conditions, much as “if” statements can be nested:




Conditional Operators

Here, if condition 1 evaluates to false, the program will simply return result C. If condition 1 evaluates to true, however, the program will then evaluate condition 2. If condition 2 evaluates to true, the program will return result A; if it evaluates to false, the program will return result B.

CONSIDERATIONS WITH CONDITIONAL OPERATORS

Conditional operators can be used to reduce the number of lines of code needed to create branching logic. Such refactoring makes code easier to understand, debug, and maintain. If many conditions need to be tested, however, an if-then-else construct might be easier to understand.

It is crucial that developers be aware of how conditional operators are implemented in a specific programming language. For example, the conditional operator is right associative in many languages. However, it is left associative in PHP. Associativity refers to the direction in which the operation is performed. To avoid errors of this type, parentheses should be used to specify operator precedence.

CONDITIONAL OPERATORS IN PRACTICE




Conditional Operators

This code sets Discount to 0.1, or 10 percent, if a customer has spent more than $1,000 during the year and 0.05, or 5 percent, if the customer has spent $1,000 or less. A more efficient way to write this instruction would be to use a conditional operator, as below:




Conditional Operators

Note how the Boolean expression from the if-then-else statement is the first operand of the conditional operator, the code from the “if” block is the second operand, and the code from the “else” block is the third operand.

Suppose the retailer wanted a more refined discount system in which customers who spent more than $1,000 during the year received a discount of 10 percent, those who spent more than $5,000 received 15 percent, and all others received 5 percent. A nested if-then-else statement could be used to accomplish the task, as follows:




Conditional Operators

However, using a conditional operator could reduce the lines of code required from nine to one:




Conditional Operators

The Boolean expression used in the outermost “if” statement becomes the first operand of the outer conditional operator. Likewise, the Boolean expression used in the innermost “if” statement becomes the first operand of the inner conditional operator.

Conditional operators provide an alternative to the if-then-else construct that can reduce the number of lines of code needed to create branching logic, particularly in complicated nesting scenarios. This reduces the time needed to write, debug, and maintain code and makes the code easier to understand. The time saved can be significant, as conditional expressions are among the most common constructs used in computer programming. Branching logic is at the core of many algorithms, making it a core capability required of any computer language. Thus, conditional operators will continue to be relevant as new computer languages are developed and existing languages are improved.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2015.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Schneider, David I. An Introduction to Programming Using Visual Basic. 10th ed., Pearson, 2017.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Elsevier, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.