03 Conditional Control Structures
Learn how C++ uses Boolean conditions, branching statements, and switch selections to control which parts of a program execute.
Build reliable Boolean conditions
A conditional control structure lets a C++ program choose which statements to execute. The choice is based on a condition, and the condition is interpreted as a Boolean value.
A evaluates to true or false. create these results:
==means equal to.!=means not equal to.<and>mean less than and greater than.<=and>=include equality.
Use == for comparison and = for assignment. For example, if (score == 75) tests a value, whereas score = 75 changes the value.
C++ does not interpret a chained comparison such as a < b < c as two comparisons joined by AND. Write the intended logic explicitly as a < b && b < c.
Takeaway: Build conditions with comparisons, and distinguish equality testing from assignment.
Combine conditions with
combine or invert conditions:
A && Bis true only when both conditions are true.A || Bis true when at least one condition is true.!Areverses the truth value ofA.
C++ uses . In A && B, the second condition is skipped when A is false. In A || B, it is skipped when A is true. This is useful for guarding an operation. For example, if (denominator != 0 && numerator / denominator > 2) evaluates the division only after the denominator has been established as nonzero.
Parentheses make mixed expressions easier to verify. For example, if ((score >= 60 && attendance >= 75) || has_special_permission) is true when both score and attendance requirements are met, or when special permission is present.
Takeaway: Combine conditions deliberately, and use short-circuiting to express safe dependencies between tests.
Choose among branches with if statements
An runs a statement or block only when its condition is true. For example, if (temperature > 30) { std::cout << "It is hot.\n"; } prints a message only when the temperature condition succeeds.
An supplies the alternative when the condition is false. For example, an even-or-odd decision can be written as if (number % 2 == 0) { std::cout << "Even\n"; } else { std::cout << "Odd\n"; }.
An handles several mutually exclusive alternatives. C++ tests the conditions from top to bottom and executes only the first true branch. Therefore, the order matters: a score of 95 reaches an earlier score >= 90 branch and does not continue to a later branch. A typical grading structure is if (score >= 90) { grade = 'A'; } else if (score >= 80) { grade = 'B'; } else { grade = 'F'; }.
Use braces consistently. They clarify branch boundaries and prevent a later statement from being unintentionally left outside the conditional.
Takeaway: Use if for a decision, else if for ordered alternatives, and else for the remaining cases.
Structure dependent decisions
places a conditional inside another conditional. It is useful when one decision should be made only after an earlier requirement succeeds. For example, a program can first test age >= 18 and then, inside that branch, test has_ticket before allowing entry.
When the rules are simple, a compound condition may communicate the same logic more directly. The nested requirement can become if (age >= 18 && has_ticket) { std::cout << "Enter the event.\n"; } else { std::cout << "Entry denied.\n"; }.
An else belongs to the nearest preceding unmatched if. Braces remove ambiguity and make the intended structure visible. Deeply nested code can be difficult to read, so flatten equivalent logic when doing so makes the rules clearer.
Takeaway: Nest decisions when the second decision depends on the first, but flatten simple logic when that improves clarity.
Select discrete values with switch
A compares one expression with several discrete constant values. It is a natural choice for commands, menu options, and values represented by an integer, character, or enumeration.
A typical command selection is switch (command) { case 'n': std::cout << "Create a new file\n"; break; case 'o': std::cout << "Open a file\n"; break; default: std::cout << "Unknown command\n"; break; }.
A identifies one constant alternative. If no case matches, default is selected when it is present. Each case value must be a converted constant expression, and duplicate case values are not allowed.
A normally ends a case. Without it, continues execution into the next case. Several case labels can intentionally share one block, as in case 6:, case 7:, and case 8: followed by one action for summer. In C++17 and later, [[]] can document intentional executable between case blocks.
Use braces around a case body when declaring local variables so that each declaration has a clear scope. A is a jump target, and an unbraced declaration can otherwise create initialization and scope problems.
Choose if when conditions involve ranges, multiple Boolean expressions, floating-point values, strings, or complex relationships. Choose switch when one expression is compared with several fixed alternatives.
Takeaway: Use switch for discrete choices, include break unless is deliberate, and use if for ranges or complex conditions.