Although the if/else structure should be your go to tool when you need to make a decision in code, there are some other ways to make decisions. This section covers two of those. Neither one is necessary - you can write any decision logic just using if/else. But there are special situations where these tricks can make code easier to read and write. You should recognize them, and should consider using them if they can make your code easier to read/write, but reaching for them should likely not be your first instinct.
If you need to make a series of decisions, chaining else if blocks can get long and redundant. For example, consider a program that converts integers like 1, 2, and 3 into words like "one", "two", and "three":
if (number == 1) {
word = "one";
} else if (number == 2) {
word = "two";
} else if (number == 3) {
word = "three";
} else {
word = "unknown";
}
This chain could go on and on, especially for banking programs that write numbers in long form (e.g., βone hundred twenty-three and 45/100 dollarsβ). An alternative way to evaluate many possible values of an expression is to use a switch statement:
switch (number) {
case 1:
word = "one";
break;
case 2:
word = "two";
break;
case 3:
word = "three";
break;
default:
word = "unknown";
break;
}
The body of a switch statement is organized into one or more case blocks. Each case ends with a break statement, which exits the switch body. The default block is optional and executed only if none of the cases apply.
If there is no break, execution will continue on, ignoring the caseβs it encounters. This is a feature that makes switches particularly useful when multiple cases can be grouped:
switch (food) {
case "apple":
case "banana":
case "cherry":
cout << "Fruit!\n";
break;
case "asparagus":
case "broccoli":
case "carrot":
cout << "Vegetable!\n";
break;
}
Any of the values 1-5 will match the appropriate case and then βfall throughβ the others until the cout is reached. Then the break exits the switch. A similar thing happens for 0 or 6 - they βfall throughβ to the line that prints that it is a weekend and then the break exits the switch.
There are some issues with switch statements that make them less useful than they might appear. First: switch statements in C++ can only be used to test integer values. You can not use a double or a string or any other type of data in the test. Second: there is no way to switch on a range. You canβt say case < 4: or case 5-10:. Switch statements are only appropriate if there are a fixed number of discrete values you want to test for.
Consider this somewhat silly example that reads in a number of monkeys and then prints out a message. It uses a conditional to pick the right form of monkey (singular or plural):
It is awkward to write basically the same code twice just so we can change one word. An alternative is to use the ternary conditional operator. This operator is a shorthand way to write a simple if/else statement that produces one of two values. It is called βternaryβ because it has three parts: a condition, a value to return if the condition is true, and a value to return if the condition is false. It takes the form (TEST ? VALUE1 : VALUE2) first is the test, followed by a question mark. If the test is true, the expression produces VALUE1. If it is false, it produces VALUE2. (The parentheses are not necessary but help to show what is part of ternary operator expression.) Here is the monkeys example rewritten to use it:
It is hard to claim that this is a massive improvement. But it is always nice to avoid repeating more code than is necessary. The ternary operator can sometimes be useful for doing so by making a simple choice between two values in the middle of some other expression. But when used with complex expressions it quickly becomes much less readable and much harder to debug. So if you choose to use it, make sure to avoid doing so in statements are already complex.