Structured Control Flow (Brain Dump)
2021-02-02
Note: This is a “brain dump” article, and subject to be cleaned up.
Categories of Structured Control Flow
- Procedure call
foo(123, true)
- Terminating
return
- Conditional
if
,if-else
,switch
- Looping
for
- loop with initial statement, condition, post statement, and bodyfor-in
- loop with a value to be iterated overwhile
- loop with condition then bodydo-while
- loop with body then condition
- Branching
break
- go to end outside of the control statementcontinue
- skip to the end of a loopfallthrough
- merge two switch case bodies, to have multiple entry points to the merged body- Labels on other control flow statements
- Deferred
defer
/scope(exit)
Pseudo/Partial Categories
- Structured Exception Handling (not specifically Microsoft’s SEH
try
,catch
- Default (named) return values
foo :: proc(x: int) -> (val: int, err: Error) { ... }
async
/await
yield
/resume
Examples of Extensions
Examples in Odin
foo :: proc(x: int) -> (val: int, err: Error) {
defer if err != nil {
// handle clean up
}
if x > 0 {
err = .Bad_Error;
} else {
val = -x*2 + 1;
}
// returns all the named return values without having the manual specify them
return;
}
the_cases: switch x {
case 1:
loop: for y > 0 {
do_thing(&y);
if y == 3 {
break the_cases;
} else if y == 2 {
continue loop;
}
do_other_thing(&y);
}
case 2, 3:
do_thing(&y);
fallthrough;
case 4:
do_other_thing(&y);
}
Categories of Unstructured Control Flow
- Procedure call
- Terminating
- Comparison
- Jump
- Indirect Jump (goto by value)
- Stack Unwinding (exceptions)