Please enable JavaScript.
Coggle requires JavaScript to display documents.
Control Flow - Coggle Diagram
Control Flow
- Control flow (flow of control) refers to the order in which statements of
an imperative or declarative program are executed or evaluated
1.)Flow of control within expressions.
2.)Flow of control among program units.
3.)Flow of control among statements
- Control structures: A control statement and a collection of statements whose
execution it controls.
Compound Statements
- One of the language features that helps make control statement design easier
is the ability to define compound statements.
- Compound statements allow collection of statements to be abstracted into
a single statement.
- No compound statements in early versions of FORTRAN. Introduced by ALGOL60
- Adding data declarations to a compound statement makes it a block.
Common forms of statement-level sequence control
- Composition : Execution order of the statements is the textual order of
the statements in the program segment
-Alternation : A collection of statement sequences may form alternatives.
- Iteration : A sequence of statements may be executed repeatedly, zero
or more times.
Evolution of Control Statements
- At machine level, execution flow is usually controlled by changing the value of the
program counter (instruction counter).
- For CPUs the only control flow instructions available are conditional or
unconditional jumps (Gotos).
Design Issues
- Multiple Entry Points
- Multiple Exits
Labels
- A label is an explicit name or number (variable) assigned to a
fixed position within the source code.
- The label may be referenced by control flow statements
appearing elsewhere in the source code.
- Other than marking a position within the source code a label
has no effect.
Break Statement
- Causes control to move forward in the program to an explicit point at the
end of a given control structure such as while,
Continue Statement
- Causes control to move forward to the end of the current loop body.
Selection statements
Single way selectiono
- FORTRAN, BASIC
IF (.NOT.Condition)GO TO 30
30 CONTINUE-In ALGOL 60 this can be coded as
No need for a CONTINUE key-word
Two-way selection statements
Semantics
Only one of the clauses are executed
Under no circumstances both clauses are executed
ALGOL 60 was the first to introduce this form
-
Nesting Selectors
Can be interpreted in two different ways depending on whether the else
clause is matched with the first then clause or the second
Nested selector issue solved by,
Providing a rule (static semantics)
ex:-The else clause is always paired with the most recent unpaired then clause (java)
Providing a syntactic form
ex:-In Pascal placing if-then in a compound statement
-
Semantics
- The expression is evaluated and the value is compared with the
constants in the constant lists
- If a match is found control transfers to the statement attached to
the matched constant_list
- When statement execution is completed, control transfers to the
first statement following the case structure.
Implementation
commonly implemented using a jump table.
Jump table: Vector stored sequentially in memory,each of whose components is an unconditional jump instruction
- The expression forming the condition of the case statement is evaluated
- Result is transfomr=ed into an interger representing the offset into the jump table form its base address
Iteration Statements
Implementation
- Causes a statement or a collection of statements to be
executed zero or more times
- The basic structure of an iteration statement consists of a
head and a body
head - controls the number of times that the body will be executed
Body - usually a statement that provides the action of the statement
Iterative Statements
1)Simple repetition
2)Counter-controlled loops
3)Logically Controlled Loops
4)User-Located Loop Control Mechanism
5)Iteration based on data structures
6)User-Defined Iteration Control
1)Simple repetition
- Statement head specifies that the body is to be executed some
fixed number of times.
2)Counter-controlled loops
- Has a variable : loop variable
-some means of specifiing the initial and terminal values of the loop variables,and the difference between sequential loop
3. Logically Controlled loops
- Repetition control is based on a boolean expression
- Every counter controlled loop can be built with a logically
controlled loop, but not the converse.
4. User-located loop controls
- Several languages have loop statements that have no iteration control mechanism they are infinite loops unless controls are addded by the programmer
5. Iteration based on data structures
Rather than a counter or boolean expression controlling the iteration, a data structure is used to control the iteration . Number of iterations is controlled by the number of elements in the data structure.
6. User-Defined Iteration Control
A more general databased iteration statement users a userdefined data structure and a user-defined function, called an iterator to go through the structure's elements.