Control Flow Structures

The way control of the CPU is transferred from one instruction to the next one.

Remember — The question is, “Who’s controlling the CPU?”

Control Flow Graph

TODO Control flow graph explanation

TODO Example of control flow graph with code parallel.

Professor’s Opinion: A function written by a good programmer only has one exit point.

Unconditional Jumps

Transfers control from one instruction to another unconditionally.

Example: Unconditional Jump
...
goto LABEL_A
LABEL_A:
...

Issues — The ability to jump anywhere leads to the spaghettication of code.

Restricted Unconditional Jumps

Example: Restricted Unconditional Jumps

Two-Way Selection Statements

Fancy way of saying if-then-else.

Example: General Syntax of Two-Way Selection Statements
if B then S_1 else S_2 end

Design Issues:

Multiple Selection Statements

You know what these are (TODO)

Example: Multiple Selection Statements
select option in:
  case v_1 do s_1
      ...
  case v_2 do s_2
      ...
  case v_3 do s_3
      ...
  default
      ...

On Default Case — Needed if you make non-exhaustive cases.

Design Issues:

Iterative Statements

Counter Controlled Loops

A way to introduce primitive recursion. Basically, any iteration that does a finite number of predetermined iterations.

Example: General Syntax of Counter Controlled Loops
for i \leftarrow init to n do S end

Design Issues:

Generalized Loops

Basically a pretty while loop. A more general version of the above.

Example: General Syntax of Generalized Loops
for i \leftarrow init until B(i) with update(i) do S end

update(i) doesn’t even need to use i in C.

Logically-Controlled Loops

Example: General Syntax of Logically-Controlled Loops
while B do S end

There are some variants like do while.