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.
- If it has multiple exit points, it breaks modular reasoning.
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.
- “Goto considered harmful”
Restricted Unconditional Jumps
Example: Restricted Unconditional Jumps
break: Jump out of loop
return: Jump out of function
continue: Jump to next iteration
labeled break/continue: jump out/to of a nested loop
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
- If: Statement initiator
- B: Boolean condition (control expression)
- S_1: Statement executed if B is true.
- S_2: Statement executed if B is false.
- End: Statement terminator.
Design Issues:
- Type and Form: Can you do anything other than booleans?
- Nesting Semantics: Are nested conditionals allowed?
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:
- Form and type of option expression: Does it have to be an enumerated type?
- e.g., C only lets you do integers, which are technically enumerated types
- Specification of case values: Can we do non-exhaustive cases? Do you have to provide a default case?
- Restrictions on executing multiple cases: Can you execute multiple cases? Can you fall-through breaks (cascading semantics)?
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:
- Type and scope of counter variable: Is it an int? Is the scope the body of the loop?
- Read-only: Is the counter variable read-only in the body?
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
There are some variants like do while.