Varieties of conditional statement

Frank Adams franka at mmintl.UUCP
Mon Oct 14 18:07:16 AEST 1985


In article <1703 at peora.UUCP> jer at peora.UUCP (J. Eric Roskos) writes:
>Some languages, such as Pascal and C, define conditional statements (this
>includes ifs, whiles, etc) like this:
>
>	<conditional> <statement>
>
>e.g.,
>	if (x) <statement>
>
>You then have <statement> defined recursively as either of a simple
>statement, or as a sequence of statements delimited by a begin/end pair.
>
>However, this style has recently fallen into disfavor.  Thus we have the
>sort of definition that is in Ada, in which all conditional statements
>can contain one or more statements, like this:
>
>	<conditional-begin-type-1> {<statement>} <conditional-end-type-1>
>
>where the conditional itself is imbedded in the conditional begin.
>
>Now, I would argue that the latter is a "weaker" construct than the former.
>The reason is (among other things) that the <statement> which can be a
>compound statement can also have private data (and thus is a "block"),
>whereas the latter approach requires a separate "block" construct to be
>defined if you want to have a local scope of reference (and note that
>syntactically, defining a local scope of reference within each type of
>conditional is harder since the declarations have to be duplicated
>in each conditional type).

With the if ... endif type of syntax, one doesn't really need the concept
of a "statement".  The syntax is if <condition> <block of code> ... endif.
One never has a context where a single statement only is required.

One no more has to duplicate the declarations to permit a local scope of
reference in each conditional type than one has to duplicate the declaration
for an expression to permit expressions in, say, conditionals of if clauses
as well as in assignments.  Personally, I would favor doing so univerally;
every block of code should be permitted to have local declarations.

(The big disadvantage of having if's control single statements is that
"if ... then if ... then ... else ..." is ambiguous; which if does the else
go with?  This is well known, of course; I just though I'd repeat it in case
anyone has forgotten.)

Dealing specifically with c, I think you are right that the
   if (condition) {
      ...
   }
syntax is based on the if ... endif idea.  But the appropriate indentation
for the if statement and compound expression interpretation (which is the
language definition) is
   if (condition)
      {
         ...
      };
which almost nobody likes.  From this point of view, we are trading off
the number of constructs in the language definition against the number of
constructs required in a particular piece of code.  No proposes that a
language should maximize the number of constructs in its definition; but
if you want to minimize them, write in assembler (or FORTRAN, if you want
portability).

I have sent this article to both net.lang and net.lang.c, but directed
followups to net.lang only.  Change this if your response fits better in
net.lang.c.

Frank Adams                           ihpn4!philabs!pwa-b!mmintl!franka
Multimate International    52 Oakland Ave North    E. Hartford, CT 06108



More information about the Comp.lang.c mailing list