If's And Loops

Hans-Dieter.Dreier@materna.de Hans-Dieter.Dreier@materna.de
Mon, 19 Jul 1999 11:22:27 +0200


--HU8y26ZEydBtljVTPQQOphhlyB84mFem
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

Jeremy Dunn wrote ...


>Previous discussion on the portrayal of looping constructs and the many
>forms they take in various languages has made me think more about what a
>loop is, and especially how its form is not that much different from
>If/Then structures (which also have a multiplicity of forms). So let us
>try another way of representing things to see if there is any conceptual
>advantage. Let us write
>
>If A Then B  as  If(:=3DA->B)
>
>and let us write
>
>If A Then B Else C   as   If(:=3DA->B->C)

Everyone used to Pascal would mistake :=3D for an assignment operator. =


>The :=3D indicates that the expression which follows it is the test
>expression.

What other than a test can possibly follow an "if"?

>The -> symbol indicates that the next expression (or group
>of statements) is evaluated if "A" is true else the next consequent C
>will be evaluated. We could group multiple expressions to be executed
>between curly brackets {} as in
>
>If(:=3DA->{a,b,c}->{x,y,x})

C programmers would mistake -> to be a pointer access. The token "->" also =
has a different meaning in functional languages.

If you use -> to denote both "then" and "else", the token will not be self-=
explanatory, you'd have to look at the context instead to determine what it=
 means, which is a possible source of errors.

>where a,b,c,x,y,z are expressions to be evaluated. Naturally we might
>rearrange this vertically for better
>appearance as in
>
>If(:=3DA
>   ->{a,b,c}
>   ->{x,y,z}
>  )
>
>The expression is evaluated left to right and every test condition may
>have no more than two consequents, a true consequent and a false one. A
>statement like If A then B ElseIf C Then D Else E would be in the form
>
>If(:=3DA->B->:=3DC->D->E), or one could indent vertically to show structur=
e
>
>If(:=3DA
>   ->B
>   ->:=3DC
>     ->D
>     ->E
>)
>
>Each ElseIf statement is merely the False consequent of the previous
>statement, the final Else statement is merely the False consequent of
>the last ElseIf statement. This format I believe would obviate the need
>for SWITCH, CHOOSE, IIF and all the other multiple IF-type commands.

Switch statements can always be replaced by if .. then ..elseif ... cascade=
s. =

Switches were introduced to save typing, especially to avoid redundant typi=
ng of the condition. Also, I guess, to make optimisation easier.

To be frankly, I can't see any significant improvements of the above notati=
on, compared to the standard.

> We
>can also indicate a loop structure using this format. The two basic loop
>formats would be shown as
>
>If(B<-=3D:A) and
>If(=3D:A->B)
>
>In this case we reverse the :=3D to =3D: to indicate that the test conditi=
on
>will be repeatedly evaluated until it is True. The arrow shows the
>direction to the False consequent B that is evaluated. In the first case
>the statement is processed left to right and the body is evaluated first
>and then the test statement. The second statment's test condition is
>evaluated first then the body. Using interior parentheses we can
>construct any nested structure of If's, Loops that we desire. To run a
>loop 5 times we could write
>
>If(=3D: (x =3D 5) ->B)
>
>x is initiialized to 0, it is not equal to 5 so it performs B,
>increments by 1 (by default) and goes back to test again.

So this is a for-loop? How do you do a while-loop that runs until a variabl=
e x is 5?

> If we want to
>increment by something other than 1 then inside B we need to have a
>statement
>
>x+=3D N where N is the amount we wish to increment.
>
>I would suggest modifying C++'s operators in the following way:
>
>X+=3D	means X =3D X+1
>X-=3D	means X =3D X-1
>X*=3D	means X =3D X*2
>X/=3D	means X =3D X/2
>X^=3D	means X =3D X^2
>
>So to decrement a counter we can just write X-=3D .
>
>It is a little unusual to see IF beginning a loop statement but I think
>you can see the close relationship in abstract structure between the
>two, the loop is only an if statement that repeats its test condition,
>or at least it appears to me that that is one way of looking at it.
>
>Any ideas, comments, problems?
>

Comment: Rather unusual notation. As long as there is no real *functional* =
improvement (hardly possible for control flow since this an old issue for w=
hich time-proven solutions exist), IMHO the disadvantage of being not self-=
explanatory to mainstream programmers is so big that I would rather stick t=
o the standards.

Possible problem: Where ever you have operators composed of special (ie non=
-alnum) characters, you may get a lexer problem. (like < and << in C++).


--

Regards,

Hans-Dieter Dreier
(Hans-Dieter.Dreier@materna.de)=

--HU8y26ZEydBtljVTPQQOphhlyB84mFem
Content-type: text/plain; charset="ISO-8859-1"
Content-transfer-encoding: quoted-printable

IDENTIFIKATIONSANGABEN:
a14546a.txt IA5 DX-MAIL X.400 User Agent=

--HU8y26ZEydBtljVTPQQOphhlyB84mFem--