The ternary conditional operator

Slides:



Advertisements
Similar presentations
Recursive binary search
Advertisements

For loops.
Templates.
Introduction to classes
Static variables.
Default values of parameters
Pointers.
Dynamically allocating arrays
Anatomy of a program.
Switch statement.
Binary search.
Command-line arguments
Throwing exceptions.
Pointer arithmetic.
Console input.
Dangling pointers.
Sorted arrays.
Dynamically allocating arrays within structures
Break statements.
Linked Lists.
Wild pointers.
Logical operators.
The comma as a separator and as an operator
Selection sort.
Bucket sort.
Dynamically allocating structures
Bit-wise and bit-shift operators
Sorting algorithms.
Command-line arguments
Passing pointers as parameters to and from functions
Repetitious operations
Dynamically allocating arrays
Insertion sort.
Problems with pointers
A list-size member variable
Protecting pointers.
Dynamically allocating arrays
Throwing exceptions.
Anatomy of a program.
Selection sort.
Insertion sort.
Pointers as arguments and return values
Reference variables, pass-by-reference and return-by-reference
Addresses and pointers
Default values of parameters
Pointer arithmetic.
Class variables and class functions
Operator overloading.
The std::string class.
Dynamic allocation of arrays
Templates.
Insertion sort.
Sorted arrays.
Sorting algorithms.
Issues with classes.
Dangling pointers.
Dynamic allocation of classes
Encapsulation.
Destructors.
Counting sort.
Selection sort.
Searching and sorting arrays
Protecting pointers.
Arithmetic operators.
Data structures: class
An array class: constructor and destructor
Constructors.
Recursive binary search
Algorithms and templates
Presentation transcript:

The ternary conditional operator

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

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”

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

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 : 1000000/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

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; }

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; }

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; }

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;

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*' ^

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

References [1] No references?

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 https://www.rbg.ca/ for more information.

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.