Control flow is achieved using low-level constructs such as
conditional branches and gotos
Common in assembly languages and some other early languages
if (A .lt. B) goto10...10 ...
Structured Flow
Gotos begin to be considered evil in the 60s
We seek better tools to solve control flow problems
Examples include if, then,
else and for
Multi-level Returns
One “advantage” of goto is that it can jump anywhere in a program,
not simply return to the caller
Sometimes we want to jump outside of our immediate context, and
multilevel returns can allow this
Exceptions
Exception handling is one example of a non-local return
Execution moves from the point where the exception was raised to the
point where the exception is handled
def assert_positive(num):if num <=0:raiseValueErrordef assert_all_positive(nums):for n in nums: assert_positive(n)try: assert_all_positive([1,2,3,-1])exceptValueError:print('All numbers are not positive')
Sequencing
Core to imperative programming
Determines order of side effects such as assignment
Compound Statement
An ordered list of statements that can be used where statements are
expected
Sometimes called blocks
Selection
Provide a way to select one set of statements based on a
condition
e.g. if or switch
Short-circuit Evaluation
Most modern languages will skip evaluating unneeded arguments in
boolean expressions
This creates more efficient programs
This can be used for control flow
functioncheck_value(obj) {if (obj && obj.val) {// Confirms that obj is definedconsole.log('Value is set') } else {console.log('Value is unset') }}check_value()check_value({ val:1 })
Switch Statements
More efficient in hardware than nested conditionals
Creates a jump table that transfers execution in a single
instruction