November 2001
Beginner
1128 pages
29h 12m
English
C++ has an operator that often can be used instead of the if else statement. This operator is called the conditional operator, written ?:, and, for you trivia buffs, it is the only C++ operator that requires three operands. The general form looks like this:
expression1 ? expression2 : expression3
If expression1 is true, then the value of the whole conditional expression is the value of expression2. Otherwise, the value of the whole expression is the value of expression3. Here are two examples showing how the operator works:
5 > 3 ? 10 : 12 // 5 > 3 is true, so expression value is 10 3 == 9? 25 : 18 // 3 == 9 is false, so expression value is 18
We can paraphrase the first example this way: If 5 is greater than 3, the expression ...
Read now
Unlock full access