Control Flow Flashcards
Basic Paradigms for Control Flow
(T/F)
Details in syntax and semantics do not differ between languages.
False
Seven Categories for Control Flow
- Sequencing
- Selection
- Iteration
- Procedural Abstraction
- Recursion
- Non-Determinacy
- Concurrency
Describe Sequencing.
Statements are to be executed in a particular order, usually in the order appearing in the program text.
Define Selection.
- A choice is made between two or more alternatives (statements or expressions) depending on a condition determined at runtime.
- If, case (switch) statements.
Describe Iteration.
- A given code fragment is executed repeatedly, either a given number of times or until a runtime condition is satisfied.
- While, do, repeat loops.
Describe Procedural Abstraction.
A potentially complex collection of control constructs is encapsulated so that it can be treated as a single unit, often subject to parameterization.
Describe Recursion.
- An expression is defined in terms of simpler versions of itself.
- The computational model requires a stack on which to save information about partially evaluated instances of the expression.
- Usually defined using self-referential subroutines.
Describe Non-Determinacy.
- The ordering or choice among statements or expressions is deliberately left unspecified.
- Any alternative should lead to a correct result.
- Sometimes “fairness” is required.
Define Concurrency.
- Two or more program fragments are to be executed “at the same time”, either in parallel on separate processors or interleaved in a single processor.
Sequencing
Imperative VS Functional Languages
- Central to imperative languages.
- Minor importance in functional languages, which emphasize expression evaluation.
Recursion
Imperative VS Functional Languages
- Crucial in functional languages
- Available in imperative languages
Iteration
Imperative VS Functional Languages
- Emphasized in imperative languages.
Precedence Associativity

Assignments

Construct Side Effects

Value Model of Variables

Reference Model

Boxing in Java

Multiway Assignment

What are some advantages to Initialization?
- Initial value of statically allocated variables can be set by compiler.
- Helps avoid common programming error of using variable before it has a value (or at least making behavior repeatable).
Ordering Within Expressions
(a + b) * (c + d)
f (a+b, c+d)
Which will be evaluated first? (a + b) or (c + d)
Not determined by precedence or associativity rules

Order Within Expressions

C, C++ define “sequence points” that constrain order of evaluation. What happens between sequence points?

Evaluation Between Sequence Points in C:
Assume x has value 1 before the expression
x[i] = i++ + 1;
What happens?
Undefined—both outcomes below are allowed
x[i] = i++ + 1;
x[1] = 2, i==2
x[2] = 2, i==2
Instead use
x[i] = i+1;
i++;
Evaluation Between Sequence Points in C:
f( i++, j++, i+j)
What value is passed to f in the 3rd parameter?
f( i++, j++, i+j)
Could be 3,4,5
Instead use
i++;
j++;
f(i,j,i+j)
Structured VS Unstructured
Unstructured Control Flow
- Use goto statement
- Fortran example
- if condition goto 10
Structured
- Use only sequencing, selection, and iteration.
- Use boundaries of lexically nested constructs as targets of branches instead of labels.
(T/F)
Goto statement are not considered harmful.
False
Case/Switch Statement
Rather than testing each case sequentially, the case statement may compute the address of the jump target.
What are some of the implementation techniques?

Iteration
Describe the following:
- Enumeration Controlled Loop
- Logically Controlled Loop

Logically Controlled Loops

Enumeration Controlled Loop

Parallel Loops


Recursion VS Iteration
Which is more efficient?
- A naive implementation of iteration usually more efficient than naive implementation of recursion since recursion makes actual subroutine calls.
- However, high quality compilers (especially those for a functional language) can generate efficient recursive code.