PeaBox Lab › PeaLang › Learn › Chapter 3 › Lesson 1

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:

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.

SecurityImpactUsersOutcome
trueanyanycritical
false8–10anyhigh
false0–7100 or morehigh
false0–7fewer than 100low

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.

ConditionBoundary included?Meaning at 8
impact > 8No8 does not match
impact >= 8Yes8 matches
impact < 8No8 does not match
impact <= 8Yes8 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:

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

Do not evaluate skipped conditions while tracing. First-match semantics are both a correctness rule and a performance rule.

1.14 Readable policy design

1.15 Common mistakes

1.16 Guided practice

1.17 Condition-design workflow

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:

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:

CaseKey conditionsExpected reason
ASecurity hold true; all other checks validSECURITY_HOLD
BInactive account; no security holdINACTIVE
CActive but identity unverifiedUNVERIFIED
DVerified user requests sensitive action without admin roleROLE_REQUIRED
EVerified admin requests sensitive actionALLOWED
FVerified non-admin requests ordinary actionALLOWED

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

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.

← Previous Next →