What does a C++ selection condition evaluate to?
A condition is an expression contextually converted to bool. true selects the true branch; false selects the false branch.
Study 3 Selection and Control Structures with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.
What does a C++ selection condition evaluate to?
A condition is an expression contextually converted to bool. true selects the true branch; false selects the false branch.
Which operator tests equality in C++?
The equality comparison operator is ==. The single = operator assigns a value instead of testing equality.
How are numeric expressions converted to bool?
Zero converts to false, while any nonzero numeric value converts to true.
Which operator means logical AND, and when is its right operand evaluated?
The logical AND operator is &&. With A && B, B is evaluated only when A is true.
Why can direct floating-point equality be unreliable?
Use a tolerance such as std::abs(a - b) < 0.000001, because many decimal fractions cannot be represented exactly in binary floating-point.
When does an if block execute?
An if statement executes its statement or block only when its condition is true.
What happens in an if-else statement?
Only one branch executes: the if branch when its condition is true, or the else branch when it is false.
How does an else-if chain choose a branch?
An else-if chain selects the first true condition, so the order of conditions matters.
How can braces prevent a dangling else?
Use braces to make each intended block explicit and prevent a dangling else from attaching to the wrong if.
When is switch preferable to a long else-if chain?
Use switch when one integral or enumeration expression is compared with several fixed constant values.
What does break do in a switch statement?
A break exits the switch. Without it, execution continues into the following case, causing fallthrough.
What happens when no switch case matches?
A switch can use default when no case matches. If default is absent, execution continues after the switch.