Varieties of conditional statement

J. Eric Roskos jer at peora.UUCP
Wed Oct 9 07:25:10 AEST 1985


>   The above difference of opinion has in the past been called a religious
> argument, with good reason.

Back in the old days, before religion was declared unconstitutional,
religious arguments were logical (albeit usually modal).  Presently
"religious argument" seems to imply "irrational", but actually reason
underlies most things, except where, through habit and ignorance,
irregularities prevail (and these are the things people argue over
endlessly).

The different brace styles actually have their basis in a much more
fundamental, and presently very important, matter having to do with the
design of languages.

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).

To expand on this, I would argue that a language construct "a" is
stronger than two language constructs "b" and "c" if "a" can express
everything that both "b" and "c" can express, but "b" cannot express some
things "c" can express, or vice versa.  I would further argue that a
language having two identical ways of expressing the same language construct
is weaker than one having only one unique way of expressing the same
construct.

Clearly, the compound statement can express both the body of a conditional,
and a "block" (where, for this discussion, let us define a "block" as a
sequence of statements having its own private scope of reference).  However,
the body of a conditional cannot be a block: because if it is, then it is
equivalent to a compound statement; and in this latter case, the language
is weaker than a language with only one form of compound statement,
because several variations of this equivalent compound statement exist
(corresponding to the numbered <conditional-begin-types> above).  Likewise,
a block cannot be the body of a compound statement, unless again there are
several synonymous ways of expressing the block, which once again makes
the language weaker.

Thus I would argue that a language with a unique compound statement, and
conditionals of the form "<conditional> <statement>", is a stronger
language than one with the Ada-style of conditionals.

Now, this brings us to C.  It is my feeling (which is open to interpretation,
since it is a sort of meta-semantic one) that writing

	if (bool) {
		stmts;
	}

is in a general semantic sense (i.e., looking at the semantics of the
coding style, as opposed to those of the language itself) equivalent to
writing

	if (bool)
		stmts;
	endif

or, alternately, that it is stylistically equivalent to writing

	c = a + (c
	    + b);

as opposed to writing

	c = a +
	    (c + b);

Because, in the latter interpretation, you are writing two distinct
syntactic components (the <conditional> and part of the <statement>) on the
same line, but then breaking the latter one (the <statement>) across lines;
and in the former, you are "hiding" the opening brace.  Neither way seems
as stylistically satisfactory as writing the conditional on one line, and
then writing the compound statement on the lines below it; it's just a
matter of breaking the statement at a point that is most reasonable.

As far as whether to indent the braces to the level of the statements that
make up the compound statement, or only to the level of the conditional,
now THAT is truly a matter of opinion.  (Though I think not indenting them
aids in finding them).

Now, we could go on to argue whether or not data declarations should be
indented, and how, but now that I have expressed the above opinion, I will
not comment any more; I try to stay out of these debates, due to the
tendency of people to write "flames" without reading the original posting
they are following up on thoroughly.  Besides, my office has meanwhile
filled up with 3 assembly-language programmers who are furiously arguing
about how they should allocate some registers, and thus it's hard enough to
proofread what I have written already. :-)

Beware: since the above was more language-theoretic than C-theoretic, I have
directed followups to net.lang; you should change this if you are going to
discuss only C braces.
-- 
Shyy-Anzr:  J. Eric Roskos
UUCP: Ofc:  ..!{decvax,ucbvax,ihnp4}!vax135!petsd!peora!jer
     Home:  ..!{decvax,ucbvax,ihnp4}!vax135!petsd!peora!jerpc!jer
  US Mail:  MS 795; Perkin-Elmer SDC;
	    2486 Sand Lake Road, Orlando, FL 32809-7642



More information about the Comp.lang.c mailing list