On 2017-08-05 08:45, Bernd Oppolzer wrote:
.... some of the indentation in the first example,
which was already awful, has been further damaged
by my eMail program :-)

That's why PL/I has the "SELECT" statement:

do_something: proc;
  do_initialization;

  select;
    when (not condition 1);
    when (not condition 2);
    when (not condition 3);
    when (not condition 4);
    other
      do_processing
  end;

  do_housekeeping
end do_something;

Robert
Am 05.08.2017 um 10:44 schrieb Bernd Oppolzer:
A similar problem occurs, when I am coding a subroutine (or function)
in C, PL/1, Pascal, whatever, and I am checking some conditions at the
start and processing should stop if the conditions aren't true, but immediate
return is no option, because some housekeeping has to be done at the end
of the function.

Example:

procedure do_something (parameters ...);

begin
  do_initialization;
  if not condition1 then
     /* don't continue */
else
     if not condition2 then
/* don't continue */
else
        if not condition3 then
/* don't continue */
else
           if not condition4 then
/* don't continue */
           else
              begin
                 do_processing
              end;
  do_housekeeping
end;

This looks awful because of the indentation.

For example, if implementing this logic in C, I could use goto,
of course. If I don't want to use goto, there is another solution,
involving a "dummy loop" and break:

void do_something (...)

{
  do_initialization ();
  do
  {
     /* one time dummy loop */

     if (! condition1)
        break;
     /* don't continue */

     if (! condition2)
        break;
     /* don't continue */

     if (! condition3)
        break;
     /* don't continue */

     if (! condition4)
        break;
     /* don't continue */

     do_processing ();
  }
  while (0);

  do_housekeeping ();
end;

This break, combined with the dummy loop,
works like a "goto forward", and IMO looks OK
and is maintainable.

In PL/1, you can use LEAVE for simple DO groups directly,
although you will get a warning, if you use it.

The warning is driven, AFAIK, by some improper use
(which occurs often in existing software), like:

DO I = 1 TO TABLE_COUNT;
   IF TAB(I) = 0 THEN DO;
      LEAVE;
   END;
   /* PROCESSING TAB (I) */
END;


often undected (LEAVE has no effect). Some companies
force the use of DO and END even on simple statements,
which leads to such errors. But that is (one or two)
another topics.


--
Robert AH Prins
robert(a)prino(d)org

----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to lists...@listserv.ua.edu with the message: INFO IBM-MAIN

Reply via email to