Presentation is loading. Please wait.

Presentation is loading. Please wait.

The ternary conditional operator

Similar presentations


Presentation on theme: "The ternary conditional operator"— Presentation transcript:

1 The ternary conditional operator

2 Outline In this lesson, we will: Review unary and binary operators
Introduce the ternary conditional operator See how to use the conditional operator as An operand An argument Part of an assignment statement

3 Operators in C++ All other operators in C++ take either
One operand (a unary operator) Two operands (a binary operator) There is one operand that takes three operands: It could be called a conditional operator, but it is simply referred to as “the ternary operator”

4 Conditional operator The operands are
condition ? true-value : false-value The condition is any Boolean-valued statement: If the condition is true, the first true-value is evaluated Otherwise, the second false-value is evaluated

5 Conditional operator Remember that an integer division by a zero results in an exception that terminates the program int invert( int n ); int invert( int n ) { return (n == 0) ? 0 : /n; } If n is zero, the operator evaluates to the second operand 0 No attempt is made to evaluate the third operand Otherwise, the operator evaluates the third operand

6 Assignment The ternary operator can also be used to simplify assignment: Suppose our array index is expected to cycle through the entries of an array with capacity entries: std::size_t idx{0}; for ( int k{0}; k < 1000; ++k ) { // Do something at array index 'idx'... if ( idx == (capacity - 1) ) { idx = 0; } else { ++idx; }

7 Assignment The ternary operator can also be used to simplify assignment: Now, the update to the local variable idx is simplified to an easy-to-comprehend statement: std::size_t idx{0}; for ( int k{0}; k < 1000; ++k ) { // Do something at array index 'idx'... idx = (idx == (capacity - 1)) ? 0 : idx + 1; }

8 Assignment The ternary operator can work as either a minimum or maximum operation: template <typename T> T min( T x, T y, T z ); T min( T x, T y, T z ) { T min{(x < y) ? x : y}; return (z < min) ? z : min; }

9 As an operand or argument
Consider the following examples: std::cout << "The minimum is " << ((m < n) ? m : n) << std::endl; It can be worse… std::cout << ((m < n) ? "'m' is smaller" : ((m == n) ? "'m' equals 'n'" : "'n' is smaller")) << std::endl;

10 As an operand or argument
Note that the types must be the same: int m{}, n{}; // Assign and use 'm' and 'n' std::cout << ((m < n) ? m : "n") << std::endl; This produces an error: example.cpp:7:33: error: operands to ?: have different types 'int' and 'const char*' ^

11 Summary Following this lesson, you now
Understand the idea of a ternary operator Know how the conditional operator selects a result to evaluate based on a logical statement The operator can be used as either an argument or operand

12 References [1] No references?

13 Colophon These slides were prepared using the Georgia typeface. Mathematical equations use Times New Roman, and source code is presented using Consolas. The photographs of lilacs in bloom appearing on the title slide and accenting the top of each other slide were taken at the Royal Botanical Gardens on May 27, 2018 by Douglas Wilhelm Harder. Please see for more information.

14 Disclaimer These slides are provided for the ece 150 Fundamentals of Programming course taught at the University of Waterloo. The material in it reflects the authors’ best judgment in light of the information available to them at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. The authors accept no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.


Download ppt "The ternary conditional operator"

Similar presentations


Ads by Google