Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mmintl.UUCP Path: utzoo!linus!philabs!pwa-b!mmintl!franka From: franka@mmintl.UUCP (Frank Adams) Newsgroups: net.lang,net.lang.c Subject: Re: Varieties of conditional statement Message-ID: <719@mmintl.UUCP> Date: Mon, 14-Oct-85 04:07:16 EDT Article-I.D.: mmintl.719 Posted: Mon Oct 14 04:07:16 1985 Date-Received: Thu, 17-Oct-85 07:46:14 EDT References: <418@phri.UUCP> <2600012@ccvaxa> <1901@reed.UUCP> <1703@peora.UUCP> Reply-To: franka@mmintl.UUCP (Frank Adams) Followup-To: net.lang Organization: Multimate International, E. Hartford, CT Lines: 68 Xref: linus net.lang:1637 net.lang.c:6207 Summary: The c if statement is no simpler than Ada's In article <1703@peora.UUCP> jer@peora.UUCP (J. Eric Roskos) writes: >Some languages, such as Pascal and C, define conditional statements (this >includes ifs, whiles, etc) like this: > > > >e.g., > if (x) > >You then have 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: > > {} > >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 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 ... 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