Document

advertisement
The Flow of Control Among Statements

Selection Statements

Iterative Statements

Unconditional Branching

Guarded Commands

Control statements provide two very
important means of making programs flexible
and powerful.
Some way of allowing different control flow paths.
 A way of allowing for the repeated execution of
statements or groups of statements.


A control structure is defined as a control
statement and the collection of statements
whose execution it controls.


Bohm and Jacopini proved that all algorithms
that can be diagrammed in a flowchart can be
reproduced in a programming language with
only the two previously mentioned control
statements.
So why have more than these?

The one design issue which is common to all
control and iteration structures is whether to
allow multiple points of entry to the statement.


These multiple entry points are only possible in
languages that allow for GOTO or statement labels.
Multiple exits from a control statement exist in most
programming languages and they are not seen as a
detriment either to readability or understanding.

Selection statements are grouped into two
different categories.

Two-way control statements (if then)

n-way control statements


What type of expression controls the if
statement
How are then and else clauses specified.
By curly braces.
 With the keyword Then.


How should the meaning of nested selectors be
specified.



Indention
Placed in a compound (Braces)
Using a reserved word to end the statement.

Counter-Controlled Loops

Logically Controlled Loops

User-Located Loop Control Mechanisms

Iteration Based on Data Structures

An unconditional branch statement transfers
execution control to a specified location in the
program.
Can be one of the most powerful statements in any
language.
 Leads to problems in readability since programs are
not restricted to a traditional top to bottom flow of
execution.
 Can lead to higher maintenance costs and
unreliability.


GOTO

Switch

User-Controlled Loop Control Statements

A new and different form of selection and loop
statement recommended by Dijkstra

Supports correctness in development.

Constructs can be considered independently

Increased clarity in reasoning

Allows for nondeterminism

Enables program verification


Guarded commands consist of a boolean
expression , the guard, and the statement or
statements to be executed when it is found
true.
Although it bears some resemblance to a multiselect statement, it differs in that any statement
whose guard evaluates to true can be executed.


Although Bohm and Jacopini proved that only
selection and pretest logical loops were
absolutely necessary to follow an flow chart
sequence, no modern language has taken the
step to remove other control structures.
Additional control structures which have been
suggested in the past may have not been
included because doing so would increase the
size and complexity of languages.



Control structures should be simple.
They should have one entrance although
having more than one exit from a control
structure is standard and is not seen as a
hindrance to readability.
GOTO is regarded as evil , however many
modern languages still allow it, including C#.

There are several different categories of control
structures.
Selection
 Multiple selection
 Iterative
 Unconditional branching


Looping structures vary among languages



Do
For
Foreach
Download