Chapter roadmap
Make programs choose among paths using comparisons, logical operators, if/else chains, switch statements, and input validation. Work through the concepts in order, type every example yourself, and keep a short debugging log that records predictions, errors, fixes, and what each fix taught you.
Learning objectives
- Build boolean expressions
- Use if, else if, and else
- Choose between if chains and switch
- Validate input before processing
02.1
Comparisons and boolean expressions
A condition evaluates to true or false. Comparison operators include ==, !=, <, <=, >, and >=. Do not confuse assignment with equality: = changes a value, while == compares two values. Store a complicated condition in a clearly named bool when that improves readability.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampleint age = 17;
bool hasPermit = true;
bool mayPractice = age >= 16 && hasPermit;
std::cout << std::boolalpha << mayPractice;
Resulttrue
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
02.2
if, else if, and else
An if chain checks conditions from top to bottom and runs only the first matching branch. Arrange narrow or exceptional cases before broad cases. Braces should be used even for one-line branches because they prevent mistakes during later edits.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Exampleint score = 86;
if (score >= 90) {
std::cout << "A";
} else if (score >= 80) {
std::cout << "B";
} else if (score >= 70) {
std::cout << "C";
} else {
std::cout << "Needs revision";
}ResultB
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
02.3
Logical operators and nesting
The && operator requires both sides to be true, || requires at least one side, and ! reverses a boolean. Parentheses make compound logic easier to audit. Nested decisions are useful when a second question only makes sense after the first condition succeeds, but excessive nesting should be simplified.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplebool member = true;
double total = 72.0;
if (member && total >= 50.0) {
total *= 0.90;
}
std::cout << total;Result64.8
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
02.4
switch and validation
A switch is useful when one expression is compared with several discrete constant choices. break prevents fall-through into the next case. Validation rejects or re-prompts for values outside an allowed range before the program performs calculations.
Before running the example, trace each expression and write down the expected state or result. Then run it, compare the actual result, and change one input or rule to confirm that you understand which part controls the behavior.
Examplechar choice = 'b';
switch (choice) {
case 'a': std::cout << "Add"; break;
case 'b': std::cout << "Browse"; break;
case 'q': std::cout << "Quit"; break;
default: std::cout << "Invalid choice";
}ResultBrowse
Check your understandingExplain the example in plain language, identify one boundary or failure case, and revise it so the output changes in a predictable way.
Chapter assignments
Complete these in order. Later assignments assume that earlier skills are working. Do not only test the sample values; design tests that challenge boundaries, missing values, incorrect types, empty collections, and other likely failures.
- A2.1
Classify an integer as positive, negative, or zero and also report whether it is even or odd.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A2.2
Create a grade calculator that validates a score from 0 through 100 and prints both a letter grade and feedback.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A2.3
Build a shipping-cost decision tree based on package weight, destination zone, and express delivery.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
- A2.4
Create a four-operation calculator using switch and protect division from a zero divisor.
Submit readable source code, a brief design note, and evidence from at least three tests including one boundary or invalid case.
Chapter project
Eligibility Advisor
Eligibility Advisor โ Ask a user a sequence of questions for a fictional program, scholarship, or event. Use compound and nested conditions to produce a clear decision with reasons. Include at least eight test cases covering boundaries, invalid values, and every outcome.
Required process
- Write a short specification listing inputs, outputs, rules, and failure cases.
- Break the work into small functions, queries, modules, or classes appropriate to the language.
- Build the smallest working version before adding optional features.
- Test normal, boundary, empty, and invalid cases and record the results.
- Refactor names and duplication, then write a concise user guide.
Key terms
- condition
- comparison
- boolean
- branch
- compound expression
- short-circuit
- switch
- validation