New programming languages lack an old capability . . .

Grouping program statements

by Conrad Weisert
September 11, 2013
© 2013 Information Disciplines, Inc.

A distinction from long ago

Here are two program fragments in PL/I, a powerful and flexible programming language first specified in the 1960s and still in limited use:

    /*  Version A  */

IF TBL(J) > TBL(K) THEN DO;
   TEMPO  = TBL(J);
   TBL(J) = TBJ(K);
   TBL(K) = TEMPO;
END;
    /*  Version B   */

IF TBL(J) > TBL(K) THEN BEGIN;
   TEMPO  = TBL(J);
   TBL(J) = TBJ(K);
   TBL(K) = TEMPO;
END;

You don't need to be acquainted with PL/I to recognize a simple element swap, such as we'd find in an array sort routine. What's the difference between them and why should a modern programmer care?

As shown here, they do exactly the same thing, but, depending on the compiler's optimizing logic, version A may be slightly more efficient.

In PL/I a DO group just defines the scope of flow of execution, while a BEGIN block does that and more. A BEGIN block defines the scope of everything inside it. The above examples assume that TEMPO is declared outside the group or block, but the programmer might have declared it inside:

    /*  Version C   */

IF TBL(J) > TBL(K) THEN BEGIN;
  DECLARE    TEMPO  FLOAT(16);
    TEMPO  = TBL(J);
    TBL(J) = TBJ(K);
    TBL(K) = TEMPO;
END;

In version C:

The distinction may appear trivial for this tiny example, but in a large program with deeply nested logic, it can make a huge difference both in performance and in modular program structure.

Modern counterpart

What are the equivalents in modern programming languages, especially the C family (C, C++, Java, C#)? We aren't given the same choice. A block is defined by the delimiters { and }, and it works more or less like a PL/I BEGIN block.

Any variables declared inside the block are dynamically allocated upon entry and freed upon exit, and their names are local to the block. C# even insists that names inside the block not duplicate names in the containing block.

Here's the C-family equivalent of version 3:

 if (tbl[j] < tbl[k]) 
  {double  tempo;
    tempo  = tbl[j];
    tbl[j] = tbl[k];
    tbl[k] = tempo; 
  }

We may hope that a smart compiler will avoid some or all of the dynamic memory management. But is there no equivalent to the PL/I DO group?

Yes, there almost is in C and C++, but not in Java and C#. A sequence of consecutive assignments can be coded as a single "statement" with comma-separated subexpressions:

 if (tbl[j] < tbl[k]) 
    tempo  = tbl[j],
    tbl[j] = tbl[k],
    tbl[k] = tempo;   

But C experts frown upon the comma operator. It's confusing to read and it invites errors when changes are made. Some organizations tolerate it if the component expressions are written on one line with a comment:

 if (tbl[j] < tbl[k]) 
    tempo=tbl[j], tbl[j]=tbl[k], tbl[k]=tempo; // Swap
  

That's not a full equivalent of PL/I's DO group, since it can't be nested, and you can't insert flow-control statements. I confess to having used this construct occasionally in C++, but the orthodox solution in the C-family is to put brackets around the group of statements and hope that the compiler is smart enough to generate sensible code.


Return to Technical articles
IDI Home page

Last modified September 9, 2013