home .. forth .. colorforth mail list archive ..

Re: [colorforth] if then else - but why is there no ‘else’ in colorForth?


Hi Nick
Nick here:  Sorry for the typo, of course it can't
work!
 Here (I hope) is a correct version: this one should
act
to yield 1 if
n is 1 or else yield 2 if n is 2, then for any other
value of n it should yield n+3.

    ( n)  : word1 dup ( nn) -1 + drop ( n) 0if ( n,f)
drop -2 ( no ";" ) then ( n/1) dup ( nn/11) -2 + drop
0if -1 (still no ";") then 3 + ;
    But is this what is meant by "if-else-then" ?
Not quite what I mean. This is so hard to explain and I have spent literally hours over this. I have never given ‘if’s any thought in other languages. Who says colorForth is simple?

in this example
  : w1 if w2 then w3 ;
the if the ZF is not set then w2 will execute and then w3 will too. If the ZF is set then only w3 goes.

in this one:
  : w1 if w2 ; then w3 ;
the if the ZF is not set then only w2 will execute. If the ZF is set then only w3. This is what I think of as an if-then-else (or if-else-then in Forths) but you cannot include any more after this as anything you put between the ‘w3’ and the ‘;’ will only execute if the ZF is set.

However, if you want to execute w4 directly after as part of w1 then you have to do this:
  : w1 if w2 w4 ; then w3 w4 ;
which duplicates some code.

or this:
  : w5 if w2 ; then w3 ;
  : w1 w5 w4 ;
Which takes two words.

So I thought of this:
  : w1 if w2 else w3 then w4 ;
the if the ZF is not set then only w2 will execute. If the ZF is set then only w3. w4 will execute afterwards regardless. Pygmy has an ‘else’ that, I think, works like this.

Now I am beginning to see that colorForth encourages all sorts of ‘factoring’ and also that you are not restricted by rules. Yours is an example of this in that what happens if the first ‘if’ executes (ie. if ZF=1) changes what the second ‘if’ does to achieve the effect you want. I'm looking for something that makes the two parts (what goes when ZF set and what when it isn’t) independent and then allows more to follow within the same word definition. My colorForth understanding is so limited that I have no examples and I am unable to contrive something satisfactory either. I'm ignoring recursion at the moment too.

What I wonder now though is if I will find, with more experience, that there is no need for an if-else-then construct; I suspect there might be no ‘else’ because Mr Moore has no need for it. Also you have not needed it yet.
    Thank you for the explanation of how "if" works.  I
shall try to digest it then post an example with 2
words as you suggest - this time with a " ; " betwee. "if" and "then" (if;else;then ?).
Ah, but what I mean is an if-else-then that doesn’t require ‘;’ to prevent execution of the following items.
  : w1 if w2 else w3 then w4 ;
reduces the pair of definitions required if there is no ‘else’
  : w5 if w2 ; then w3 ;
  : w1 w5 w4 ;
     Also, re 'then' has to match an 'if', there are 2
'then' matched with only 1 'if' in the definition of '
+! ' (on block 26 of CF05).  I have noticed such multi
concatenations elsewhere in CF.  Perhaps the structure
of 'if' in CF is different from previous Forth,
allowing one to build multi choice equivalents  of
'if;else;then' or 'case' ?
That example actually has two ‘if’s which are nested. It is easy to overlook the first one. I think that the way ‘if’ works precludes having multiple ‘then’s: the action is to jump to the ‘then’ if the ZF is set, so having two ‘then’s means you can only jump to the first, the second being ignored. I haven't looked at how the compiler works yet, but I would expect that this would cause a stack underflow during compilation, yet a simple example I tried compiled fine.


Still perplexed,
Jason



---------------------------------------------------------------------
To unsubscribe, e-mail: colorforth-unsubscribe@xxxxxxxxxxxxxxxxxx
For additional commands, e-mail: colorforth-help@xxxxxxxxxxxxxxxxxx
Main web page - http://www.colorforth.com