Control flow

Control flow is the order in which a program evaluates instructions. Without control flow, a program would only execute a fixed sequence from beginning to end.

The help desk functions from Functions and single responsibility now need an explicit path for valid and invalid events.

The basic forms

  • Sequence: execute instructions in order.
  • Selection: choose a path with a condition such as if and else.
  • Iteration: repeat work with a loop.
  • Function calls: transfer responsibility to a named operation.
  • Recursion: solve a problem by calling the same operation on a smaller input.

For example:

receive event
if event is invalid:
    return validation failure
else:
    validate event
    accept ticket
    return accepted

Good control flow makes the program’s decisions visible. Deeply nested conditions, duplicated branches, and unclear exit conditions make behavior harder to reason about.