On this page
  1. Control Flow & Composition
  2. Contents
  3. Structured Control Primitives
  4. Guard Clauses & Short-Circuit Evaluation

Control Flow & Composition

Prev: State, Variables & Scope | Up: foundations | Next: Functions & Recursion
Rel: Python Control Flow, Imperative & Procedural Programming
Ref: Wikipedia — Control Flow
Tags: #public #prog #programming #foundations


Contents


Structured Control Primitives

By the Structured Program Theorem, any computable algorithm can be expressed with just three primitives:

  1. Sequence: execute statements in order.
  2. Selection: branch on conditions (if/else, switch/match).
  3. Iteration: repeat while a condition holds (for, while).

Guard Clauses & Short-Circuit Evaluation

Prefer flat guard clauses (early returns on invalid cases) over deeply nested if pyramids:

function process(order):
  if order is null: return error("missing order")
  if order.items is empty: return error("empty order")
  return charge(order)

Boolean AND (&&) and OR (||) short-circuit: the right-hand operand is evaluated only if the left-hand operand does not already determine the result.

Linked from

Related notes