Branching and Decisions
-
Reading If and Else Branches
A conditional runs work for the input cases described by its test and can supply a separate action for the other cases.
-
Inclusive and Exclusive Comparison Boundaries
Whether a comparison includes an endpoint can decide which quantities are admitted and whether two categories overlap.
-
If Chains and Independent Conditions
An if-elif chain chooses the first matching branch. Two independent checks can both run for one input.
-
Guard Clauses and Early Returns
A guard can handle inputs for which the remaining computation has no valid result or no work to perform.
-
Short-Circuit Boolean Evaluation
Some Boolean operations skip a later operand once the first result determines the answer.
-
When And and Or Return Operands
Python's Boolean operators and and or return a selected operand, so the result need not have Boolean type.
-
De Morgan's Laws in Conditions
Distributing a negation across a compound test exchanges conjunction and disjunction while negating each operand.
-
Truth Tables for Program Decisions
A truth table assigns an expected outcome to each combination of independent Boolean inputs.
-
Conditional Expressions That Choose a Value
A conditional expression chooses one value for use inside a larger expression or as the result of a function.
-
Detecting Overlap in Condition Ranges
A category rule should state whether its ranges overlap and which outcome to choose if several conditions apply.
-
Covering Every Case in a Decision
A decision is exhaustive when each allowed input reaches a defined outcome.
-
Chained Comparisons in Python
Python permits a sequence of comparisons that shares the middle operand and can stop at the first false comparison.
-
Writing Predicates as Functions
A predicate gives a name to a question that evaluates to true or false for a documented class of inputs.
-
Decision Tables for Small Business Rules
A decision table lists the relevant input cases and their expected outcomes before mapping them to implementation branches.
-
Testing Membership in a Collection
Membership asks whether an accepted value exists in a collection under that collection's comparison or lookup rule.
-
Replacing Repeated Choices with a Lookup
For a fixed key and one result per key, a data table can express a decision compactly.
-
Branching on a Failed Conversion
A failed conversion provides an observable result that can guide how a program reports invalid data.
-
A State Transition Table for a Small Workflow
Model a workflow by writing the next state for each allowed combination of current state and input event.
-
Branching on an End Marker
An end marker represents completion of an input sequence and needs a representation distinguishable from any valid record.
-
Naming Boolean Flags
A Boolean name should allow a reader to interpret the true case as a complete statement about the domain.
-
Reading Nested Branches and Guards
Nested conditions are useful when a later decision assumes that an earlier requirement was met.
-
Classification with Ordered Thresholds
An ordered series of thresholds can classify a numeric value into nonoverlapping bands when its endpoint rules are explicit.
-
Writing Conditions on Dates and Times
A condition about a calendar deadline depends on the date, time zone, and interpretation of the endpoint.
-
Pattern Matching on Record Shapes
Structural matching tests the shape of a value and can bind the fields of an accepted case where the language supports it.
-
Designing a Default Branch
A default branch can handle the remaining valid case or report a value outside a decision's supported domain.
-
Case-Insensitive Decisions on Text
A case-insensitive text comparison needs the same normalization on both operands and a rule suited to the kind of identifier.
-
When a State Transition Can Be Repeated
An idempotent transition reaches the same intended state when the same operation is applied again.
-
Intentional Fall-Through in Switch Statements
Some languages can continue from a matching case into following case bodies unless control leaves the switch.
-
Recognizing Monotone Predicates
On an ordered domain, a monotone Boolean predicate changes truth value at most once in the relevant direction.
-
Keeping Conditions Free of Surprising Effects
A condition is easier to move and combine when its evaluation reads stable input and has no surprising state changes.