CHAPTER 7
STATEMENT-LEVEL
CONTTROL STRUCTURE
- Two factors are
necessary to make the computations in programs flexible and powerful: some
means of selecting among alternative control flow paths (of statement
execution) and some means of causing the repeated execution of certain
collections of statements.
Statements that provide these kinds of capabilities are called control statements
- It was proven that all
algorithms that can be expressed by flowcharts can be coded in a
programming language with only two control statements: one of choosing
between two control flow paths and one for logically controlled
iterations.
- Writeability and
readability are very important.
All languages that have become very widely used contain more
control statements than the two that are minimally required, because writeability
is enhanced by a larger number of control statements.
- Control structure is a control statement
and the collection of statements whose execution it controls. Control structures should have single entries
and single exits.
COMPOUND STATEMENTS
- One feature that makes
control statement design easier is a method of forming statement
collections.
- Algol 60 introduced the
first statement collection structure, the compound statement:
begin
statement_1;
…
statement_n
end
- Compound statements
allow a collection of statements to be abstracted to a single statement –
which can be used to great advantage in control statement design. Data declarations can be added to the
beginning of a compound statement in several languages, making it a block.
- C, C++, and Java use
braces to delimit both compound statements and blocks.
SELECTION STATEMENTS
- Selection statements provides the means of
choosing between two or more execution paths in a program. They fall in two general categories,
two-way and n-way, or multiple selection.
Two-Way Selection Statements
- The simplest design
issue for two-way selectors is the type of expression that controls the
selector.
- All imperitative
languages include a single-way selector, in most cases as a sub-form of a
two-way selector
if(flag .ne. 1) go to 20
i = 1
j = 2
20
continue
if (Boolean expression) then
begin
statement _1;
…
statement-n
end
- Most of the languages
that followed Algol 60, including FORTRAN 77 and 90, provide a single-way
selectors that can select a compound statement or a sequence of
statements.
- Algol introduced the
first two-way selector:
if (Boolean expression)
then
statement
else
statement
Nesting
Selectors
if sum = 0 then
if count = 0 then
result := 0
else
result
:= 1
- The designers of Algol
60 chose to use syntax, rather than a rule, to connect else clauses. An if
statement is not allowed to be nested directly in a then clause. If an if must be nested in a then clause, it must be placed in
a compound statement.
if sum = 0 then
begin
if count
= 0 then
result := 0
end
else
result := 1
MULTIPLE SELECTION
CONSTRUCTS
- The multiple selection construct
allows one of any number of statements or statement groups, it is a
generalization of a group. In
fact, a single-way and a two-way selectors can be built with a multiple
selector.
- Multiple selectors can
be build from two-way selectors and gotos, but the resulting structure is
difficult to write and read.
- FORTRAN’S three-way
selector, which is called an arithmetic IF, is a special case of multiple
selection statements. Because only
three or fewer statement collections can be selected.
IF (arithmetic
expression) N1, N2, N3
- The general form of
Hoare’s multiple selector:
case
integer_expression of
begin
statement_1;
…
statement-n
end
-- Pascal’s case
case expression of
constant_list_1:
statement_1:
…
constant_list_n:
statement_n
end
-- The C multiple selector
construct, switch, which also
appears i C++ and Java:
switch (expression)
{
case expression_1: statement_1;
…
case expression_n: statement_n;
[default: statement_n + 1]
}
ITERATIVE STATEMENTS
An iterative statement is one that causes a
statement or collection of statements to be executed 0 or 1 or more times. Every programming language has included some
method of repeating the execution of segments of code. Iteration is the very essence of the power
of the computer. If iteration were not
possible, programmers would be required to state every action in sequence;
useful programs would be large and inflexible and take a huge amount of time to
write.
- The first iterative
constructs in programming languages were directly related to arrays.
- The body of a loop is the collection
of statements whose execution is controlled by the iteration statement.
- The term pretest means that the test for
loop completion occurs before the loop body is executed.
- The term posttest means that it occurs after
the loop body is executed.
- The iteration statement
and the associated loop body together form an iteration construct.
COUNTER-CONTROLLED
LOOPS
- A counting iterative
control statement has a variable, called the loop variable, in which the count value is maintained. This includes some means of specifying
the initial and terminal values of the loop
variable and the difference between sequential loop variable values, often
called the step-size.
THE
DO STATEMENT OF FORTRAN 77 &
FORTRAN 90
- The distinctive feature
of this statement was that it was a posttest, making it different from the
counting iterative statements of all other programming languages.
- The general form of
this statement is;
DO label variable = initial, terminal, [step size]
Where the label is the last statement in
the loop body and the step size, when absent, defaults to 1.
LOGICALLY
CONTROLLED LOOPS
- In many cases,
collections of statements mist be repeatedly executed, but the repetition
control is based on a Boolean expression rather than a counter. For these situation, a logically
controlled loop is convenient.
ITERATION
BASED ON DATA STRUCTURES
- Rather than have a
counter or Boolean expression control iterations, these loops are
controlled by the number of elements in a data structure.
- COMMON LISP and Perl
have such statements.
- In COMMON LISP the dolist function iterates on simple
lists, which are the most common data structures in LISP programs.
- Perl's for each
statement is similar, it iterates on the elements of lists or arrays.
UNCONDITIONAL BRANCHING
- An unconditional branch statement transfers execution
control to a specified place in the program.
- The unconditional
branch or goto is the most powerful statement for controlling the flow of
execution of a program's statements.
- Problems follow
directly from a goto's capability of forcing any program statement to
follow any other in execution sequence, regardless of whether that
statement precedes or follows the first in textual order.
- Modula-2 and Java are
designed without goto's
- Most currently used
languages include a goto statement.