Decisions and debugging
Express rules as conditional logic, then find and fix what goes wrong
Operational rules rarely consist of one uninterrupted calculation. A program must classify cases, respect boundaries, and choose between actions. Conditions turn such rules into explicit decisions that a program can evaluate.
This part builds on the values, expressions, and functions of Module 3. You first express decision rules with conditional statements. You then locate and correct defects when a program stops with an error or runs but produces a wrong result.
Learning objectives
By the end of this part, you will be able to:
- translate a decision rule or decision tree into
if,if-else,if-elif-else, andmatchstatements; - write conditions with comparison and logical operators, and predict how truthy and falsy values and short-circuit evaluation affect them;
- distinguish mutually exclusive cases and explain the effect of rule order;
- use conditional expressions where a single value depends on a condition;
- distinguish syntax errors, runtime errors, and logic errors;
- locate a defect with error messages, print statements, assertions, and the VS Code debugger; and
- handle a predictable runtime error with a
try-exceptstatement.