If's And Loops

Jeremy Dunn jeremydunn@ibm.net
Sat, 17 Jul 1999 12:43:24 -0700


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(:=A->B)

and let us write

If A Then B Else C   as   If(:=A->B->C)

The := indicates that the expression which follows it is the test
expression. 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(:=A->{a,b,c}->{x,y,x})

where a,b,c,x,y,z are expressions to be evaluated. Naturally we might
rearrange this vertically for better
appearance as in

If(:=A
   ->{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(:=A->B->:=C->D->E), or one could indent vertically to show structure

If(:=A
   ->B
   ->:=C
     ->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. We
can also indicate a loop structure using this format. The two basic loop
formats would be shown as

If(B<-=:A) and
If(=:A->B)

In this case we reverse the := to =: to indicate that the test condition
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(=: (x = 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. If we want to
increment by something other than 1 then inside B we need to have a
statement

x+= N where N is the amount we wish to increment.

I would suggest modifying C++'s operators in the following way:

X+=	means X = X+1
X-=	means X = X-1
X*=	means X = X*2
X/=	means X = X/2
X^=	means X = X^2

So to decrement a counter we can just write X-= .

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?