Forum Navigation
You need to log in to create posts and topics.

unhappy with loops

Posted by: Waverly <Waverly@...>

>The FOR loop unhappiness
>------------------------
>ZBasic and FB made a poor choice in implementing FOR loops, such that the
>loop body is _always_ executed at least once, regardless of the start, end
>and increment values. To allow the body to be skipped we must bracket the
>loop as shown:-
>
>LONG IF last%>=first% ' else we want to skip the loop
> FOR j% = first% TO last%
> .. do stuff
> NEXT j%
>END IF
>
>FOR loops in BASIC are officially supposed to be implemented such that the
>body can be executed zero times if appropriate. Some variants of BASIC got
>unhappy loops in the days of tiny interpreters, which were too stupid to
>follow the language definition. The only language with an officially
>unhappy loop was FORTRAN. The late lamented MS QuickBasic did it right. How
>about FBIII?
>
>The LONG IF nonsense
>--------------------
>Ever felt embarrassed showing your source code to another programmer? The
>LONG IF, inherited from ZBasic, curls my toes by its ineptness:-
>
>LONG IF flagSet
> .. do stuff
>XELSE
> .. do other stuff
>END IF
>
>Those other programmers snicker at the silly syntax and ask "Why LONG? Why
>no THEN after LONG IF? Why XELSE? Why no ELSEIF?..."
>
>In MS QuickBasic you could write, with no perceptible strain or discomfort:-
>IF flagSet THEN
> .. do stuff
>ELSEIF tr%=2 THEN
> .. do other
>ELSE
> .. yet different
>END IF
>
>How about FBIII? Is it going to cast off the ugly and inflexible LONG IF?
>

I find nothing wrong with FB's implementation of the for loop. If this were
"C" it would be a different story. A for loop is based on the
implementation on a loop based on an initiating expression, an ending
expression and the conditional use of a STEP clause. By definition the
initiating expression exists causing the loop to executed atleast once.

EX:

WINDOW 1

FOR loop = 12 TO 11
PRINT "this is a test"
PRINT loop
NEXT

DO
HANDLEEVENTS
UNTIL 0

With this example, even though the step value is not negative one, loop
initially equalled 12 and is executed one time. I dont know the ANSI
definition for a FOR loop but I would bet that is not only within standard
but is the correct implementation. If we were looking at a FOR loop in C
then the loop would not execute unless the {condition} were true.

I also see nothing wrong with LONG IF. Every version of BASIC I've seen has
it own dialectical spin. In all honesty, it took me a bit of getting used
to but that was just a bump in the learning curve. If you want to test
multiple conditions your could always use the SELECT statement. It's not as
pretty but just as effective.

If you want an easier conversion of other languages such as C to FB(III)
then its reasonable and desirable. It just sounds more like you have a
gripe than a suggestion.

W.