When the Next Step Depends on a Condition

Suppose an algorithm receives a temperature \(T\) and must display a warning when the temperature is below freezing.

The rule might be:

  1. Input \(T\).
  2. If \(T<0\), display “freezing.”
  3. Otherwise, display “not freezing.”

The condition

\[ T<0 \]

has a Boolean value:

\[ \text{true} \quad\text{or}\quad \text{false}. \]

That truth value determines which branch is followed.

\[ \boxed{ \text{condition} \longrightarrow \text{branch choice} \longrightarrow \text{next action} } \]

Conditions and Boolean Values

A condition is a statement that can be evaluated as true or false.

Examples include:

\[ x>5, \] \[ n=0, \] \[ a\leq b, \] \[ x\in A. \]

Conditions may also use logical connectives:

\[ P\land Q, \qquad P\lor Q, \qquad \neg P. \]

A branch does not need to understand why a condition is true. It only needs the resulting truth value.

The If Rule

The simplest decision rule performs an action only when a condition is true.

In pseudocode:

IF condition
    perform action
END IF

Example

IF balance < 0
    display "Overdrawn"
END IF

If the condition is false, the guarded action is skipped.

If-Else Branching

An if-else structure chooses between two alternatives.

IF condition
    perform action A
ELSE
    perform action B
END IF

Example: Even or Odd

For integer \(n\):

IF n mod 2 = 0
    output "even"
ELSE
    output "odd"
END IF

Exactly one branch is taken.

A Two-Way Decision

Two-way branching flowchart A flowchart receives an integer n, asks whether n modulo 2 equals zero, then outputs even on the yes branch and odd on the no branch. Input integer \(n\) Is \(n\bmod2=0\)? Output “odd” Output “even” No Yes
The condition partitions valid inputs into two cases. Each input follows the branch corresponding to the truth value of the test.

Multiple Branches

Some problems require more than two cases.

Example: Sign of a Number

IF x > 0
    output "positive"
ELSE IF x < 0
    output "negative"
ELSE
    output "zero"
END IF

This divides the real numbers into three regions:

\[ x>0, \qquad x<0, \qquad x=0. \]

Together, the cases cover every real input.

Mutually Exclusive Cases

Two conditions are mutually exclusive when they cannot both be true for the same input.

For example,

\[ x<0 \]

and

\[ x>0 \]

are mutually exclusive.

Mutually exclusive cases simplify branching because an input can belong to at most one branch.

Overlapping Conditions

Conditions can overlap.

Consider:

\[ x>0 \]

and

\[ x>10. \]

Every value greater than \(10\) satisfies both conditions.

That is not automatically a mistake, but the algorithm must define what should happen when several conditions are true.

Possible policies include:

Why Branch Order Matters

In a first-match structure, broad conditions can accidentally hide more specific ones.

Poor Order

IF score >= 60
    grade = "Pass"
ELSE IF score >= 90
    grade = "Excellent"
END IF

A score of \(95\) satisfies the first condition, so the second branch is never reached.

Better Order

IF score >= 90
    grade = "Excellent"
ELSE IF score >= 60
    grade = "Pass"
ELSE
    grade = "Fail"
END IF

When conditions are nested by strength, test the most restrictive condition first.

Nested Decisions

A decision can contain another decision.

Example

IF age >= 18
    IF has_id = true
        output "admit"
    ELSE
        output "ID required"
    END IF
ELSE
    output "not eligible"
END IF

The second condition is tested only inside the first branch.

Nested rules can model layered requirements, but too much nesting can make logic difficult to read.

Compound Conditions

Logical connectives combine conditions.

AND

\[ P\land Q \]

is true only when both \(P\) and \(Q\) are true.

OR

\[ P\lor Q \]

is true when at least one is true.

NOT

\[ \neg P \]

reverses the truth value of \(P\).

Example

A value is accepted when

\[ x\geq0 \]

and

\[ x\leq100. \]

The compound condition is

\[ \boxed{ x\geq0\land x\leq100 }. \]

Decision Tables

A decision table organizes combinations of conditions and the actions associated with them.

Suppose a discount depends on membership and order size.

Member? Order at least \$100? Discount
NoNo0%
NoYes5%
YesNo5%
YesYes10%

Decision tables are useful when several Boolean conditions interact.

Piecewise Functions as Branching

A piecewise function is a mathematical form of branching.

For example,

\[ f(x)= \begin{cases} x^2,&x\geq0,\\ -x,&x<0. \end{cases} \]

Algorithmically:

IF x >= 0
    return x^2
ELSE
    return -x
END IF

The cases in the function are the branches in the algorithm.

\[ \boxed{ \text{piecewise definition} \longleftrightarrow \text{decision structure} } \]

Guard Clauses

A guard clause checks an exceptional or invalid condition early and exits before the main process continues.

Example

IF b = 0
    return "division undefined"
END IF

return a / b

The guard protects the later division from invalid input.

This can be clearer than deeply nesting the valid case inside another branch.

Complete and Exhaustive Rules

A branching system should account for every valid input that the problem allows.

For example:

IF x > 0
    output "positive"
ELSE IF x < 0
    output "negative"
END IF

What happens when

\[ x=0? \]

The rule is incomplete unless zero is intentionally excluded from the input domain.

A complete rule either:

Practical Applications

Software

Branching controls login rules, permissions, validation, menus, and error handling.

Business

Decision rules classify customers, apply discounts, calculate fees, and route transactions.

Engineering

Control systems choose actions based on sensor thresholds and operating conditions.

Mathematics

Piecewise functions, case proofs, sign analysis, and domain restrictions all use branching logic.

Data Analysis

Rules classify observations into categories based on numerical or logical conditions.

Worked Examples

Example 1: Absolute Value

IF x >= 0
    return x
ELSE
    return -x
END IF

This computes

\[ \boxed{|x|}. \]

Example 2: Grade Categories

IF score >= 90
    grade = "A"
ELSE IF score >= 80
    grade = "B"
ELSE IF score >= 70
    grade = "C"
ELSE IF score >= 60
    grade = "D"
ELSE
    grade = "F"
END IF

The conditions are tested from most restrictive to least restrictive.

Example 3: Interval Membership

Test whether \(x\) lies in

\[ [2,8]. \]
IF x >= 2 AND x <= 8
    output true
ELSE
    output false
END IF

Example 4: Guard against Division by Zero

IF b = 0
    return "undefined"
END IF

return a / b

The invalid case is handled before division occurs.

Example 5: Nested Eligibility Rule

A user qualifies if age is at least \(18\), and then only if a required form is complete.

IF age >= 18
    IF form_complete
        output "qualified"
    ELSE
        output "complete form"
    END IF
ELSE
    output "age requirement not met"
END IF

Example 6: Overlap Problem

Consider:

IF x > 0
    output "positive"
ELSE IF x > 10
    output "large positive"
END IF

The second branch can never run because every \(x>10\) already satisfies \(x>0\).

A corrected order is:

IF x > 10
    output "large positive"
ELSE IF x > 0
    output "positive"
END IF

Common Errors

1. Forgetting a Boundary Case

Rules for \(x<0\) and \(x>0\) do not cover \(x=0\).

2. Testing Broad Conditions before Narrow Ones

A general branch can prevent a more specific branch from ever running.

3. Assuming Overlapping Conditions Are Mutually Exclusive

Check whether more than one condition can be true at once.

4. Confusing AND with OR

\[ P\land Q \]

requires both conditions, while

\[ P\lor Q \]

requires at least one.

5. Nesting More Than Necessary

Guard clauses or compound conditions can sometimes make the logic clearer.

6. Failing to Define What Happens on Invalid Input

A decision system should state how exceptional cases are handled.

Practice

Two-Way Branch

Write the condition needed to output “adult” when age is at least \(18\).

Check Answer \[ \boxed{\text{age}\geq18}. \]

Boundary Case

Do the conditions \(x<5\) and \(x>5\) cover every real number?

Check Answer

No. The case

\[ \boxed{x=5} \]

is missing.

Compound Condition

Write a condition for \(x\) being between \(0\) and \(10\), inclusive.

Check Answer \[ \boxed{x\geq0\land x\leq10}. \]

Branch Order

Which test should come first in a first-match structure:

\[ x>0 \]

or

\[ x>100? \]
Check Answer \[ \boxed{x>100} \]

because it is the more restrictive condition.

Piecewise Translation

Translate

\[ f(x)= \begin{cases} 1,&x\geq0,\\ -1,&x<0 \end{cases} \]

into an if-else rule.

Check Answer
IF x >= 0
    return 1
ELSE
    return -1
END IF

The Deeper Idea

Branching is logic turned into process.

\[ \boxed{ \text{proposition} \longrightarrow \text{truth value} \longrightarrow \text{action} } \]

That connects algorithms directly to earlier logical structures:

A branching algorithm is therefore a dynamic version of mathematical case analysis.

The important design questions are:

When those questions are answered clearly, branching becomes reliable rather than ad hoc.

Summary

Decision rules give algorithms the ability to respond to conditions instead of following one rigid path. The quality of the algorithm depends on the quality of those conditions: they must be precise, correctly ordered, and complete enough to handle the problem's full domain.

Algorithms and Computational Thinking