0% found this document useful (0 votes)
7 views5 pages

Control Structures in Programming

The document discusses control structures, specifically focusing on selection statements that allow choosing between different execution paths. It outlines two-way and multiple-way selection statements, highlighting design issues and examples from programming languages like FORTRAN, ALGOL 60, and C. The document also addresses nesting selectors and the use of else-if clauses in languages such as Ada.

Uploaded by

imxcrazylol
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Control Structures in Programming

The document discusses control structures, specifically focusing on selection statements that allow choosing between different execution paths. It outlines two-way and multiple-way selection statements, highlighting design issues and examples from programming languages like FORTRAN, ALGOL 60, and C. The document also addresses nesting selectors and the use of else-if clauses in languages such as Ada.

Uploaded by

imxcrazylol
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Statement-Level

Control Structures
Control Structure
• A control structure is a control statement and the statements whose
execution it controls
• Design question
– Should a control structure have multiple entries?
Selection Statements
• A selection statement provides the means of choosing between two
or more paths of execution
• Two general categories:
– Two-way selectors.
– Multiple-way selectors.
Two-Way Selection Statements
• General form:
if control_expression
then clause
else clause
• Design Issues:
– What is the form and type of the control expression?
– How are the then and else clauses specified?
– How should the meaning of nested selectors be specified?
Example1:-
FORTRAN: IF (boolean_expr) statement
• Problem: can select only a single statement; to select more, a
GOTO must be used, as in the following example
IF (.NOT. condition) GOTO 20
...
20 CONTINUE
1
Example2:-
ALGOL 60:
if (boolean_expr)
then statement (then clause)
else statement (else clause)
• The statements could be single or compound
Nesting Selectors
• Java example
if (sum == 0)
if (count == 0)
result = 0;
else result = 1;
• Which if gets the else?
• Java's static semantics rule: else matches with the nearest if
Multiple-Way Selection Statements
• Allow the selection of one of any number of statements or
statement groups
Example:-
Early multiple selectors:
– FORTRAN arithmetic IF (a three-way selector)
IF (arithmetic expression) N1, N2, N3
– Segments require GOTOs
– Not encapsulated (selectable segments could be anywhere)
– Modern multiple selectors
– C’s switch statement
switch (expression) {
case const_expr_1: stmt_1;

2
case const_expr_n: stmt_n;
[default: stmt_n+1]
Design choices for C’s switch statement
1. Control expression can be only an integer type
2. Selectable segments can be statement sequences, blocks, or
compound statements
3. Any number of segments can be executed in one execution
of the construct (there is no implicit branch at the end of
selectable segments)
4. default clause is for unrepresented values (if there is no
default, the whole statement does nothing)
Multiple Selectors can appear as direct extensions to two-way selectors,
using else-if clauses, for example in Ada:
if ...
then ...
elsif ...
then ...
elsif ...
then ...
else ...
end if

3
Selection Guarded Command: Illustrated

4
Loop Guarded Command: Illustrated

You might also like