The conditional operator (?:) is C++’s only ternary operator; that is, it is the only opera-tor to take three terms.
The conditional operator takes three expressions and returns a value:
(expression1) ? (expression2) : (expression3)
This line is read as “If expression1is true, return the value of expression2; otherwise, return the value of expression3.” Typically, this value is assigned to a variable. Listing 4.9 shows an ifstatement rewritten using the conditional operator.
LISTING4.9 A Demonstration of the Conditional Operator
1: // Listing 4.9 - demonstrates the conditional operator 2: //
3: #include <iostream>
4: int main() 5: {
6: using namespace std;
7:
8: int x, y, z;
9: cout << “Enter two numbers.\n”;
10: cout << “First: “;
11: cin >> x;
12: cout << “\nSecond: “;
13: cin >> y;
14: cout << “\n”;
Creating Expressions and Statements 95
4
15:
16: if (x > y) 17: z = x;
18: else 19: z = y;
20:
21: cout << “After if test, z: “ << z;
22: cout << “\n”;
23:
24: z = (x > y) ? x : y;
25:
26: cout << “After conditional test, z: “ << z;
27: cout << “\n”;
28: return 0;
29: }
Enter two numbers.
First: 5 Second: 8
After if test, z: 8
After conditional test, z: 8
Three integer variables are created:x,y, and z. The first two are given values by the user. The ifstatement on line 16 tests to see which is larger and assigns the larger value to z. This value is printed on line 21.
The conditional operator on line 24 makes the same test and assigns zthe larger value. It is read like this: “If xis greater than y, return the value of x; otherwise, return the value of y.” The value returned is assigned to z. That value is printed on line 26. As you can see, the conditional statement is a shorter equivalent to the if...elsestatement.
Summary
In today’s lesson, you have learned what C++ statements and expressions are, what C++
operators do, and how C++ ifstatements work.
You have seen that a block of statements enclosed by a pair of braces can be used any-where a single statement can be used.
You have learned that every expression evaluates to a value, and that value can be tested in an ifstatement or by using the conditional operator. You’ve also seen how to evaluate multiple statements using the logical operator, how to compare values using the rela-tional operators, and how to assign values using the assignment operator.
O
UTPUTLISTING4.9 continued
ANALYSIS
You have explored operator precedence. And you have seen how parentheses can be used to change the precedence and to make precedence explicit, and thus easier to manage.
Q&A
Q Why use unnecessary parentheses when precedence will determine which operators are acted on first?
A It is true that the compiler will know the precedence and that a programmer can look up the precedence order. Using parentheses, however, makes your code easier to understand, and therefore easier to maintain.
Q If the relational operators always return true or false, why is any nonzero value considered true?
A This convention was inherited from the C language, which was frequently used for writing low-level software, such as operating systems and real-time control soft-ware. It is likely that this usage evolved as a shortcut for testing if all of the bits in a mask or variable are 0.
The relational operators return true or false, but every expression returns a value, and those values can also be evaluated in an ifstatement. Here’s an example:
if ( (x = a + b) == 35 )
This is a perfectly legal C++ statement. It evaluates to a value even if the sum of a
and bis not equal to 35. Also note that xis assigned the value that is the sum of a
and bin any case.
Q What effect do tabs, spaces, and new lines have on the program?
A Tabs, spaces, and new lines (known as whitespace) have no effect on the program, although judicious use of whitespace can make the program easier to read.
Q Are negative numbers true or false?
A All nonzero numbers, positive and negative, are true.
Workshop
The Workshop provides quiz questions to help you solidify your understanding of the material covered and exercises to provide you with experience in using what you’ve learned. Try to answer the quiz and exercise questions before checking the answers in Appendix D, and be certain that you understand the answers before continuing to tomor-row’s lesson on functions.
Creating Expressions and Statements 97
4
Quiz
1. What is an expression?
2. Is x = 5 + 7 an expression? What is its value?
3. What is the value of 201 / 4?
4. What is the value of 201 % 4?
5. If myAge,a, and bare all intvariables, what are their values after
myAge = 39;
a = myAge++;
b = ++myAge;
6. What is the value of 8+2*3?
7. What is the difference between if(x = 3)and if(x == 3)? 8. Do the following values evaluate true or false?
a. 0
b. 1
c. –1
d. x = 0
e. x == 0 // assume that x has the value of 0
Exercises
1. Write a single ifstatement that examines two integer variables and changes the larger to the smaller, using only one elseclause.
2. Examine the following program. Imagine entering three numbers, and write what output you expect.
1: #include <iostream>
2: using namespace std;
3: int main() 4: {
5: int a, b, c;
6: cout << “Please enter three numbers\n”;
7: cout << “a: “;
8: cin >> a;
9: cout << “\nb: “;
10: cin >> b;
11: cout << “\nc: “;
12: cin >> c;
13:
14: if (c = (a-b))
15: cout << “a: “ << a << “ minus b: “ << b <<
16: _” equals c: “ << c;
17: else
18: cout << “a-b does not equal c: “;
19: return 0;
20: }
3. Enter the program from Exercise 2; compile, link, and run it. Enter the numbers 20, 10, and 50. Did you get the output you expected? Why not?
4. Examine this program and anticipate the output:
1: #include <iostream>
2: using namespace std;
3: int main() 4: {
5: int a = 2, b = 2, c;
6: if (c = (a-b))
7: cout << “The value of c is: “ << c;
8: return 0;
9: }
Enter, compile, link, and run the program from Exercise 4. What was the output? Why?