Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!whuxl!whuxlm!akgua!gatech!seismo!cmcl2!philabs!pwa-b!mmintl!franka From: franka@mmintl.UUCP Newsgroups: net.lang Subject: and if you put this in your language ... Message-ID: <1187@mmintl.UUCP> Date: Fri, 7-Mar-86 22:34:12 EST Article-I.D.: mmintl.1187 Posted: Fri Mar 7 22:34:12 1986 Date-Received: Tue, 11-Mar-86 01:23:58 EST Reply-To: franka@mmintl.UUCP (Frank Adams) Organization: Multimate International, E. Hartford, CT Lines: 87 There is a fairly common, relatively simple programming structure which cannot be easily coded using standard control statements (not gotos) in any language I know of. In flow-chart form, the control structure is as follows: | A:/\ +---\/---+ +-+ | |X| | +-+ | | | B:/\ | +--\/-------+ +-+ +-+ |Y| |Z| +-+ +-+ +-----+-----+ | One often winds up writing this in the form: if (A) then X; if (A && B) then Y; else Z; However, this fails if executing X changes the value of A (or if A has side effects); and if evaluating A is expensive, the additional evaluation may be undesirable. In languages where the order of execution is not specified for the 'and' operator, the evaluation of (A && B) may fail if evaluation of B is not valid unless A is true and/or X has been executed (which is fairly common). In C, one often sees something like: if (A && (X, B)) then Y; else Z; However, this only works if X can be written in-line. A solution in full generality requires defining a boolean variable, say L, and writing: if (A) then {X; L = B;} else L = FALSE; if (L) then Y; else Z; I am proposing that a new clause be added to the 'if' statement, which will represent this function directly, and in a way I think is more readable than any of these options. This would be the 'and if' clause (or 'andif', depending on the language). It would be used as follows: if (A) then X; and if (B) then Y; else Z; Naturally, one would be able to use multiple and ifs in a single if statement. Having added an and if, it seems natural to add an or if, as well. This would be syntactically identical to the and if, and would represent the following control structure: | A:/\ +---\/---+ +-+ B:/\ |X| \/-+ +-+ | | +---+----+ | +-+ +-+ |Y| |Z| +-+ +-+ +---+---+ | This seems to me considerably less useful than the and if; I don't particularly remember ever needing this structure. There is also the problem of mixing and ifs and or ifs; the effect of doing this is likely to being confusing. However, I don't see any real disadvantages to including an or if clause in the language, at least if mixed ands and ors are not allowed; the meaning is quite intuitive. For languages which don't have and and or operators which specify the order of evaluation, providing both and if and or if clauses deals with most cases where such is needed. In particular, if the statement labelled 'X' in each example is null, the and if is just an &&, and the or if is an ||. Has anyone seen constructs like these implemented anywhere? Does anyone else think they are a good idea? Arguments both pro and con are welcomed. Frank Adams ihnp4!philabs!pwa-b!mmintl!franka Multimate International 52 Oakland Ave North E. Hartford, CT 06108