Control 2: Sequencing Control Between Statements



     sequencing simple expressions (post-, pre-, in- fix, precedence, problems with infix, short circuits)

     sequencing complex expressions (pattern matching, term rewriting, unification, substitution)






              Pratt &Zelkowitz 8.3, 8.4

Sequence Control Between Statements

Forms of Statement-Level Control

     composition (collecting statements into a structure)



Explicit Sequence Control

Most languages provide unconditional "GOTO label", and conditional "if A=0 then GOTO label", GOTO statements. They equate to a machine code instruction that transfers control to another memory location

Early languages mostly provided direct analogues of these. However it was soon recognised that they are a prime cause of poorly structured code. One of the key ideas behind structured programming is that each section of code should have one entry point and one exit point, GOTO's break this. Most modern languages provide many control alternatives so GOTO statements are not needed, and some do not provide GOTOs at all.

Many languages provide a special command (Ada - exit, C - break) that will jump from the inside of a loop to just past its end. C also provides the continue command that jumps to the end of the loop (but still inside it). These structures are still considered structured as they have only one exit.

Structured Sequence Control

Most structured imperative languages have some sort of compound statement that collects terms together: C family {}, Algol family begin .. end, Smalltalk [].

Conditional Statements

The common if statements.

       if test then statements endif


       if test then statements

       else statements2 endif

are implemented by simple machine code statements that check a value, then dependent on whether it is true or false, jump to the appropriate location

CASE statements are more complicated as often multiple tests must be performed.

       case check is

        when 1 => ..

        when 2 => ...

        when 3 => ..

        when others =>

       end case;

In some languages the following approach is used

        GET value of check

        jump to location L0+check

L0      Jump to L1

        Jump to L2

        jump to L3

L1      code for when check = 0



L2      code for when check = 1



L3      code for when check = 2

Iteration Statements

Simple Repetition:

The simplest loop is of the form

     perform body K times

but even it leads to questions

               What if K is 0 or negative?

               What if the value of K changes whilst the loop is running?

To answer these we must know:

               When is the test done (at the start or end) ?

               Are the values used to control the loop reevaluated each time the loop is executed?

Repetition while condition holds:

       while test do body

      (pretest loop)

       repeat body until test

      (posttest loop)

       loop body

      (infinite loop - hopefully contains an exit statement!)

Fairly consistent across languages

Repetion while increasing a counter:

     for I:=1 step 2 until 30 do body

or more generally

     For  (K=3, L=2 ;

          (K<7 && L<11) ;

          K++ , L+=2)

Raises similar questions about when exactly the test is checked, and if values can be reevaluated.

Implemented as a simple test that checks whether or not to continue the loop, it may also be necessary to allocate memory for the loop counter.

Problems for Structured Sequence Control

The following cases are not easily satisfied by the pure structured commands. They can be solved either by complicated combinations of the above commands, some sort of alternate structure, the use of exit/breaks or with GOTOs.

Multiple exit loops:

If an algorithm has multiple tests that may indicate a loop must finish, eg


         X = read_character(file);

         Exit when EOF(file);

         Exit when EOLN(file);

         Exit when X = ' ';

       end loop;


It is sometimes desirable to do the loop test in the middle of the loop.


         read (X)

         if X is acceptable then goto labela

         give error message  

    end loop


This 'n and a half loop' construct can also be built with an exit or break statement.

Exception conditions:

               When an exception occurs, i.e about to divide by 0, encountered end of file, it is sometimes desirable to transfer control to a separate section of code that deals with exceptional circumstances. More on exceptions later




     conditional and case statements

     repetition (when checked)

     problems with structures (multiple exit, do_while_do, exception conditions)