Professional C__ - Marc Gregoire [8]
switch (menuItem) {
case kOpenMenuItem:
// Code to open a file
break;
case kSaveMenuItem:
// Code to save a file
break;
default:
// Code to give an error message
break;
}
If you omit the break statement, the code for the subsequent case will be executed whether or not it matches. This is sometimes useful, but more frequently a source of bugs.
The Ternary Operator
C++ has one operator that takes three arguments, known as the ternary operator. It is used as a shorthand conditional expression of the form “if [something] then [perform action], otherwise [perform some other action].” The ternary operator is represented by a ? and a :. The following code will output “yes” if the variable i is greater than 2, and “no” otherwise.
std::cout << ((i > 2) ? "yes" : "no");
The advantage of the ternary operator is that it can occur within almost any context. In the preceding example, the ternary operator is used within code that performs output. A convenient way to remember how the syntax is used is to treat the question mark as though the statement that comes before it really is a question. For example, “Is i greater than 2? If so, the result is ‘yes’: if not, the result is ‘no.’”
Unlike an if statement or a switch statement, the ternary operator doesn’t execute code blocks based on the result. Instead, it is used within code, as shown in the preceding example. In this way, it really is an operator (like + and -) as opposed to a true conditional, such as if and switch.
Conditional Operators
You have already seen a conditional operator without a formal definition. The > operator compares two values. The result is “true” if the value on the left is greater than the value on the right. All conditional operators follow this pattern — they all result in a true or false.
The following table shows common conditional operators (op).
OP DESCRIPTION USAGE
<
<=
>
>= Determines if the left-hand side is
less than, less than or equal to, greater than, or greater than or equal to the right-hand side. if (i < 0) {
std::cout << "i is negative";
}
== Determines if the left-hand side equals the right-hand side. Don’t confuse this with the = (assignment) operator! if (i == 3) {
std::cout << "i is 3";
}
!= Not equals. The result of the statement is true if the left-hand side does not equal the right-hand side. if (i != 3) {
std::cout << "i is not 3";
}
! Logical NOT. Complements the true/false status of a Boolean expression. This is a unary operator. if (!someBoolean) {
std::cout << "someBoolean is false";
}
&& Logical AND. The result is true if both parts of the expression are true. if (someBoolean && someOtherBoolean) {
std::cout << "both are true";
}
|| Logical OR. The result is true if either part of the expression is true. if (someBoolean || someOtherBoolean) {
std::cout << "at least one is true";
}
C++ uses short-circuit logic when evaluating an expression. That means that once the final result is certain, the rest of the expression won’t be evaluated. For example, if you are performing a logical OR operation of several Boolean expressions as shown below, the result is known to be true as soon as one of them is found to be true. The rest won’t even be checked.
bool result = bool1 || bool2 || (i > 7) || (27 / 13 % i + 1) < 2;
In this example, if bool1 is found to be true, the entire expression must be true, so the other parts aren’t evaluated. In this way, the language saves your code from doing unnecessary work. It can, however, be a source of hard-to-find bugs if the later expressions in some way influence the state of the program (for example, by calling a separate function). The following code shows a statement using && that will short-circuit after the second term because 0 always evaluates to false.
bool result = bool1 && 0 && (i > 7) && !done;
Loops
Computers are great for doing the same thing over and over. C++ provides three types of looping structures