Control Structures in Programming: Beyond Selection and Logical Loops, Exercises of Programming Languages

The debate around the necessary control statements for programming languages beyond selection and logical loops. It covers the evolution of control structures, the issues surrounding their design, and various solutions such as fortran, algol 60, pascal, and ada. The document also discusses the advantages and disadvantages of different control structures and their impact on readability and flexibility.

Typology: Exercises

2011/2012

Uploaded on 08/04/2012

dhanvin
dhanvin 🇮🇳

4.2

(14)

108 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Def: A control structure is a control statement and
the statements whose execution it controls
Levels of Control Flow
1. Within expressions
2. Among program units
3. Among program statements
Overall Design Question
What control statements should a language have,
beyond selection and pretest logical loops?
Evolution
- FORTRAN I control statements were based
directly on IBM 704 hardware
- Much research and argument in the1960s about the issue
- One important result: It was proven that all flowcharts
can be coded with only two-way selection and pretest
logical loops
Selection Statements
Design Issues
1. What is the form and type of the control
expression?
2. What is the selectable segment form (single
statement, statement sequence, compound
statement)?
3. How should the meaning of nested selectors
be specified?
Single-Way Selection Statement
FORTRAN IF
- IF (boolean_expr) statement
Problem
- Can select only a single statement;
- To select more, a goto must be used
FORTRAN example:
IF (.NOT. condition) GOTO 20
...
...
20 CONTINUE
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Control Structures in Programming: Beyond Selection and Logical Loops and more Exercises Programming Languages in PDF only on Docsity!

  • Def: A control structure is a control statement and the statements whose execution it controls
  • Levels of Control Flow
  1. Within expressions
  2. Among program units
  3. Among program statements
  • Overall Design Question What control statements should a language have, beyond selection and pretest logical loops?

Evolution

  • FORTRAN I control statements were based directly on IBM 704 hardware
  • Much research and argument in the1960s about the issue
    • One important result: It was proven that all flowcharts can be coded with only two-way selection and pretest logical loops

Selection Statements

Design Issues

  1. What is the form and type of the control expression?
  2. What is the selectable segment form (single statement, statement sequence, compound statement)?
  3. How should the meaning of nested selectors be specified?

Single-Way Selection Statement

FORTRAN IF

  • IF (boolean_expr) statement

Problem

- C an select only a single statement;

  • To select more, a goto must be used

FORTRAN example:

IF (.NOT. condition) GOTO 20 ... ... 20 CONTINUE

ALGOL 60 if:

if (boolean_expr) then begin ... end

Two-way Selector

ALGOL 60 if :

if (boolean_expr) then statement (the then clause) else statement (the else clause)

  • The statements could be single or compound

Nested Selectors

Example (Pascal)

if ... then if ... then ... else ...

  • Which then gets the else?
  • Pascal's rule: else goes with the nearest then

Nested Selectors

ALGOL 60's solution - disallow direct nesting

if ... then if ... then begin begin if ... if ... then ... then ... end else ... else ... end

FORTRAN 77, Ada, Modula-2 solution – closing special words

Example (Ada)

if ... then if ... then if ... then if ... then ... ... else end if ... else

execution of the construct

  1. In Wirth's Pascal, result of an un-represented control expression value is undefined (In 1984 ISO Standard, it is a runtime error)
    • Many dialects now have otherwise or else clause

The C and C++ switch

switch (expression) { constant_expression_1 : statement_1; ... constant_expression_n : statement_n; [default: statement_n+1] }

Any number of segments can be executed in one execution of the construct (there is no implicit branch at the end of selectable segments)

  • Trade-off between reliability and flexibility (convenience)
  • To avoid it, the programmer must supply a break statement for each segment

Ada's case is similar to Pascal's case, except:

  1. Constant lists can include:
    • Subranges e.g., 10..
    • Boolean OR operators e.g. 1..5 | 7 | 15..
  2. Lists of constants must be exhaustive
    • Often accomplished with others clause
    • This makes it more reliable
  • Multiple Selectors can appear as direct extensions to two-way selectors, using else-if clauses (ALGOL 68, FORTRAN 77, Modula-2, Ada)

Example (Ada) if ... then ... elsif ... then ... elsif ... then ... else ...

end if

  • Far more readable than deeply nested if's
  • Allows a boolean gate on every selectable group

Iterative Statements

  • The repeated execution of a statement or compound statement is accomplished either by iteration or recursion; here we look at iteration, because recursion is a unit-level control - General design Issues for iteration control statements are:
  1. How is iteration controlled?
  2. Where is the control mechanism in the loop?

Counter-Controlled Loops

Design Issues

  1. What is the type and scope of the loop variable?
  2. What is the value of the loop variable at loop termination?
  3. Should it be legal for the loop variable or loop parameters to be changed in the loop body, and if so, does the change affect loop control?
  4. Should the loop parameters be evaluated only once, or once for every iteration?

1. FORTRAN 77 and 90

  • S yntax: DO label var = start, finish [, stepsize]
    • Stepsize can be any value but zero
    • Parameters can be expressions - Design choices:
  1. Loop variables can be INTEGER, REAL, or DOUBLE
  2. Loop variable always has its last value
  3. The loop variable cannot be changed in the loop, but the parameters can; because they are evaluated only once, it does not affect loop control
  4. Loop parameters are evaluated only once

FORTRAN 90’s ‘Other DO’

control

4. Ada

  • Syntax: for var in [reverse] discrete_range loop ... end loop

Ada Design choices

  1. Type of the loop var is that of the discrete range; its scope is the loop body (it is implicitly declared)
  2. The loop var does not exist outside the loop
  3. The loop var cannot be changed in the loop, but the discrete range can; it does not affect loop control
  4. The discrete range is evaluated just once

5. C

  • Syntax: for ([expr_1] ; [expr_2] ; [expr_3]) statement
  • The expressions can be whole statements, or even statement sequences, with the statements separated by commas
  • The value of a multiple-statement expression is the value of the last statement in the expression e.g. for (i = 0, j = 10; j == i; i++) ...
  • If the second expression is absent, it is an infinite loop

-C Design Choices

  1. There is no explicit loop variable
  2. Irrelevant
  3. Everything can be changed in the loop
  4. Pretest
  5. The first expression is evaluated once, but the other two are evaluated with each iteration
  • This loop statement is the most flexible

6. C++

  • Differs from C in two ways:
    1. The control expression can also be Boolean
    2. The initial expression can include variable definitions (scope is from the definition to the end of the function in which it is defined)

7. Java

  • Differs from C++ in two ways:
    1. Control expression must be Boolean
      1. Scope of variables defined in the initial expression is only the loop body

Logically-Controlled Loops

- Design Issues 1. Pre-test or post-test? 2. Should this be a special case of the counting loop statement (or a separate statement)?

Examples

  1. Pascal has separate pretest and post-test logical loop statements (while-do and repeat-until)
  2. C and C++ also have both, but the control expression for the post-test version is treated just like in the pretest case (while - do and do - while)
  3. Java is like C, except the control expression must be Boolean (and the body can only be entered at the beginning- Java has no goto)
  4. Ada has a pretest version, but no post-test
  5. FORTRAN 77 and 90 have neither

User-Located Loop Control Mechanisms

- Design issues 1. Should the conditional be part of the exit? 2. Should the mechanism be allowed in an already controlled loop? 3. Should control be transferable out of more than one loop? 1. C , C++, and Java - break - Unconditional; for any loop or switch; one level only (Java’s can have a label) - There is also a continue statement for loops; it skips the remainder of this iteration, but does not exit the loop