Part I — Foundations · Chapter 3 · Lesson 1 of 5 · 9 min read
Lesson 1: Boolean Logic and Conditional Branches
Lesson level: Foundation
Purpose: Design correct, readable decisions by combining boolean logic with ordered conditional branches.
Learning objectives
By the end of this lesson, you should be able to:
- identify boolean values and distinguish them from truthy or false-like values;
- combine conditions with and, or, not, and !;
- write if, else if, and else branches;
- predict branch selection from top-to-bottom evaluation;
- translate a condition table into mutually exclusive branches;
- choose comparison operators that handle boundaries correctly;
- compare chained and nested conditionals; and
- order policy rules by explicit precedence.
1.1 Decisions begin with conditions
A conditional statement chooses whether a block of code runs. Its condition is evaluated for truthiness. A true-like result selects the associated block; a false-like result skips it.
if service_impact >= 8 {
print "High-impact case"
}
The comparison produces a boolean value. When service_impact is 8 or greater, the message is printed. Otherwise, execution continues after the block.
1.2 Boolean values and truthiness
The literal values true and false are booleans. Comparisons and equality tests also produce booleans.
let is_high_impact = service_impact >= 8
let is_gold = customer_tier == "gold"
Conditional statements accept any value and interpret it by truthiness. False, null, numeric zero, empty strings, and empty collections are false-like; other ordinary values are true-like. For policy code, boolean expressions are usually clearer than relying on implicit truthiness.
1.3 Combining boolean conditions
Use and when every required condition must be true:
customer_tier == "gold" and service_impact >= 5
Use or when any listed condition is sufficient:
security_relevant or affected_users >= 100
Use not or ! to reverse truthiness:
not archived
Because not binds more tightly than comparisons, and binds more tightly than or, and logical operators short-circuit, add parentheses whenever they make the intended policy grouping easier to verify.
1.4 if and else
An else block supplies the alternative when an if condition is false-like:
if stock > 0 {
print "Available"
} else {
print "Out of stock"
}
Exactly one branch runs. The else block has no condition because it represents every case not selected by the preceding if.
1.5 else-if chains
Use an ordered chain when one decision must be selected from several alternatives:
if score >= 90 {
grade = "A"
} else if score >= 80 {
grade = "B"
} else if score >= 70 {
grade = "C"
} else {
grade = "Needs improvement"
}
Conditions are tested from top to bottom. Evaluation stops at the first true-like condition, and later conditions are skipped.
1.6 First-match semantics and policy precedence
Branch order is part of the program’s meaning. Consider a case that is security-relevant, has impact 9, and affects 200 users. If the security rule appears first, it wins even though later conditions are also true.
if security_relevant {
priority = "critical"
} else if service_impact >= 8 {
priority = "high"
} else if affected_users >= 100 {
priority = "high"
} else {
priority = "low"
}
Order rules from highest precedence to lowest. Document why a higher rule overrides lower ones, and include overlap tests that make several conditions true at once.
1.7 Condition tables
A condition table separates policy analysis from syntax. Write one row for each meaningful input region, then map each row to an outcome.
| Security | Impact | Users | Outcome |
|---|---|---|---|
| true | any | any | critical |
| false | 8–10 | any | high |
| false | 0–7 | 100 or more | high |
| false | 0–7 | fewer than 100 | low |
The word any means that a higher-priority condition has already decided the outcome. The table reveals coverage gaps, overlaps, and boundary values before code is written.
1.8 Mutually exclusive branches
An else-if chain produces one outcome even when its raw conditions overlap. To make reasoning easier, describe each branch as the remaining input region after earlier conditions have failed.
For example, the user-count branch above effectively means: not security-relevant, impact below 8, and affected users at least 100. The code need not repeat those failed conditions because reaching the branch already proves them.
Do not replace one ordered decision with several independent if statements unless multiple outcomes are intended. Independent conditions can execute more than one block and overwrite related outputs.
1.9 Boundary conditions
Threshold rules divide an input range. The comparison operator determines which side owns the boundary.
| Condition | Boundary included? | Meaning at 8 |
|---|---|---|
| impact > 8 | No | 8 does not match |
| impact >= 8 | Yes | 8 matches |
| impact < 8 | No | 8 does not match |
| impact <= 8 | Yes | 8 matches |
For every threshold, test one value below it, the exact value, and one value above it. This three-point method exposes most off-by-one errors.
1.10 Chained versus nested conditionals
Use an else-if chain when alternatives belong to one decision and only one outcome should win. Use nesting when a second decision is meaningful only after a broader condition has been selected.
if account_active {
if balance >= purchase_total {
decision = "approve"
} else {
decision = "insufficient funds"
}
} else {
decision = "inactive account"
}
The balance question matters only for active accounts, so nesting reflects the dependency. Avoid deep nesting when named boolean expressions or an ordered chain can express the same policy more clearly.
1.11 Complete and consistent branch outputs
When one decision produces several related outputs, update them together in every branch. A priority, response target, and reason code form one result and should never disagree.
Initialize a safe default before the chain or assign every output in every branch. Then verify the invariant after the decision: each priority has its correct response target and reason code.
1.12 Worked example: Shipping service level
A warehouse selects one service level using package weight, destination region, and fragile status. The policy is:
- Fragile packages receive special handling.
- Otherwise, packages above 20 kilograms receive freight.
- Otherwise, international packages receive international parcel.
- All remaining packages receive standard parcel.
let package_weight = 12
let destination_region = "international"
let fragile = false
let service_level = "standard parcel"
let reason_code = "STANDARD"
if fragile {
service_level = "special handling"
reason_code = "FRAGILE"
} else if package_weight > 20 {
service_level = "freight"
reason_code = "WEIGHT"
} else if destination_region == "international" {
service_level = "international parcel"
reason_code = "INTERNATIONAL"
} else {
service_level = "standard parcel"
reason_code = "STANDARD"
}
print "Service level: {service_level}"
print "Reason: {reason_code}"
With the supplied inputs, fragile is false and weight is not above 20. The international condition is true, so the third branch runs. The expected service level is international parcel with reason INTERNATIONAL.
1.13 Trace a branch chain
- Evaluate the first condition.
- If it is true-like, run its block and skip the rest of the chain.
- If it is false-like, evaluate the next condition.
- Continue until one condition succeeds.
- If none succeeds, run the final else block when present.
- Record the outputs changed by the selected block.
Do not evaluate skipped conditions while tracing. First-match semantics are both a correctness rule and a performance rule.
1.14 Readable policy design
- Give important conditions names when they combine several comparisons.
- Place exceptional or highest-priority rules first.
- Keep branch bodies focused on one decision.
- Assign related outputs together.
- Use a final else when every valid input needs an outcome.
- Document intentional overlaps and precedence.
- Test boundaries, overlaps, defaults, and invalid inputs separately.
1.15 Common mistakes
- Using assignment in place of equality: use == to compare values.
- Ordering a broad rule before a specific one: the broad rule may make the specific branch unreachable.
- Using separate if statements for one exclusive decision: multiple blocks may run and overwrite outputs.
- Forgetting the exact threshold: test below, at, and above every boundary.
- Repeating failed conditions: reaching a later else-if branch already proves earlier conditions false.
- Relying on implicit truthiness where a domain comparison is clearer: write the intended threshold explicitly.
- Nesting too deeply: use named booleans or an ordered chain when the dependency is not truly hierarchical.
- Updating only part of a decision: keep related outputs internally consistent.
- Omitting a default unintentionally: decide what happens when no condition matches.
1.16 Guided practice
- Write a condition that is true only when a customer is gold-tier and the order total is at least 100.
- Parenthesize security_relevant or customer_tier == "gold" and impact >= 5 to make default precedence explicit.
- Explain which branch owns score 80 in the grade example.
- Reorder the shipping rules so fragile handling still overrides freight and international service.
- Convert three independent if statements for low, medium, and high risk into one exclusive chain.
- Describe the effective condition of the final else branch in the support-priority policy.
- Choose chained or nested conditionals for checking account status and then available balance.
- Create below, exact, and above test values for a threshold of 100 affected users.
1.17 Condition-design workflow
- State the possible outcomes.
- List the inputs that influence the decision.
- Write a condition table covering valid input regions.
- Identify overlaps and define precedence.
- Choose exact boundary operators.
- Translate rows into an ordered branch chain.
- Keep related outputs together in each branch.
- Test boundaries, overlaps, the default path, and invalid data.
1.18 Hands-on lab: Build an access decision engine
Create access_decision.pea for a controlled internal system. Use source-defined sample values for account_active, identity_verified, role, security_hold, and requested_sensitive_action.
The policy is:
- A security hold always denies access with reason SECURITY_HOLD.
- An inactive account denies access with reason INACTIVE.
- An unverified identity denies access with reason UNVERIFIED.
- A sensitive action requires the admin role; otherwise deny it with reason ROLE_REQUIRED.
- All other valid requests are allowed with reason ALLOWED.
Return a complete decision through three variables: allowed, decision, and reason_code. Print every input and output with labels, but do not include secrets or personal data.
Required test cases:
| Case | Key conditions | Expected reason |
|---|---|---|
| A | Security hold true; all other checks valid | SECURITY_HOLD |
| B | Inactive account; no security hold | INACTIVE |
| C | Active but identity unverified | UNVERIFIED |
| D | Verified user requests sensitive action without admin role | ROLE_REQUIRED |
| E | Verified admin requests sensitive action | ALLOWED |
| F | Verified non-admin requests ordinary action | ALLOWED |
Add at least two overlap tests in which several denial conditions are true. Record which rule wins and explain why. Format, check, run, and compare the result with a prediction table.
Deliverable: The source file, condition table, ordered-policy explanation, prediction-and-results table, and a short note explaining why independent if statements would be unsafe for this decision.
1.19 Review questions
- What value does an if condition use to choose a branch?
- Why are explicit boolean comparisons often clearer than implicit truthiness?
- When does an else block run?
- How does an else-if chain choose among several true conditions?
- Why is branch order part of policy meaning?
- What does a condition table reveal before coding begins?
- How do > and >= differ at a threshold?
- When is nesting preferable to a flat chain?
- Why can several independent if statements be wrong for one exclusive decision?
- Which tests demonstrate both boundaries and precedence?
1.20 Applied challenge
Refactor the Chapter 1 case-priority program by defining named boolean conditions for every rule. Create a condition table that includes overlaps, verify the existing cases A–F, and add one case for every exact boundary. Then reverse two adjacent rules deliberately, predict which results change, run the tests, and restore the approved precedence.
Lesson summary
PeaLang conditional branches evaluate conditions for truthiness and execute blocks according to source order. An if/else pair selects between two paths, while an else-if chain chooses the first matching alternative. Correct decision logic depends on explicit boolean expressions, deliberate precedence, exact boundary operators, complete branch outputs, and tests for thresholds, overlaps, defaults, and invalid data.
Lesson status: Approved.