Re: OT: The REAL RIddle of DO LOOPS Solved




Reef Fish wrote:
While I expected my question to alt.lang.basic to take awhile
before getting an answer back for my question (since the
traffic there is less than 1 post per day <G>), I was pleasantly
surprised to have gotten a response from Tom Lake that
turned out to be MOST INFORMATIVE about the programming
language conventions and its existing mess. :-)

This was the paragraph that explained it all:

TL> The problem is that the other language you tried will always
execute a
TL> FOR-NEXT loop at least once (Older MS BASICs such as Applesoft
TL> and TRS-80 Level II BASIC do this) whereas the ANSI standard and
TL> newer MS BASICs will skip the loop altogether if the initial
condition
TL> is not met. It runs fine in QBasic when n>0 and x=0



Here's a follow-up conversation that sheds further light into the
subject:

Stephen Rush wrote:
On Wed, 24 May 2006 20:58:24 -0700, Reef Fish wrote:

After posing my question, someone gave me a qbasic program to
try, and I discovered that "quirk" of skipping the loop altogether.
I didn't realize that's an ANSI standard and see at least two merits
in the OLD standard of executing the FOR - NEXT loop at least
once because,
(a) there may be some CONDITION within the loop for k = 0.
to check the value of X, and for X =0, the program may
wish to branch OUT of the loop to do something else; OR
(b) want to actually execute the LOOP in a negative increment
of -1.

I have not used BASIC or FORTRAN for years. And I am glad
that I don't use any program that abides by the new ANSI
convention. :-)


QuickBasic and friends let you use negative increments in a FOR loop, and
let you use a floating-point loopcounter, so

FOR Q = 2.5 TO -2.5 STEP -0.05

That's good. But if the explicit statement is allowed, the the IMPLIED

step should be obvious ( minus 1 as default) if the second argument
is less than the first. That is,

FOR Q = 0 TO -1

should mean the same as

FOR Q = 0 TO -1 STEP -1

That is indeed the case in the very powerful object oriented language
i use, similar to R, S, or APL. For that language, it allows the FOR
- NEXT construction (though very rarely used or needed) and that's
exactly how it treats an IMPLIED step.

is valid, if Q is a SINGLE or DOUBLE. You can also say
IF [condition]THEN EXIT FOR
anywhere in the loop.

Precisely. That gives the programmer perfect control of what he wants
to do, rather let some ASSUMED standard that is inconsistent with
the logical usage of FOR - NEXT loops, by skipping a loop entirely
without even executing the first step (or initial condition) of a loop.


QuickBasic has more looping options than most
languages. You can actually use DO WHILE, IF [condition] THEN
EXIT DO and LOOP UNTIL in the same loop, although having too many exit
conditions from a loop is a good way to shoot yourself in the foot.

Is QuickBasic different from QBASIC? I was under the impression
that they are the same.

But from your explanation, it appears that they may be different
dialects of BASIC. Is that true?
========== end post from alt.lang.basic

-- Bob.

.



Relevant Pages

  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... >> considered to be a convenience by those who use the C language. ... I make no claims to being a VB.NET programmer. ... Ritchie SCREWED UP in designing the for loop because ... I do not believe that you are capable of writing a conforming C compiler. ...
    (comp.programming)
  • Re: Letter to US Sen. Byron Dorgan re unpaid overtime
    ... > Hey, idiot, read my lips - the CONDITION is evaluated before each loop ... expressed my hope that you actually know the sequence of evaluations ... inappropriately with commercial promotion of the C language when I ... Programming Style: code what you mean. ...
    (comp.programming)
  • Re: Program compression
    ... RH> which is a one-liner in pretty well any language. ... PJB> Not in languages that don't provide syntactic abstraction. ... ;Return list of such SUBSEQ forms. ... loop at all, just inline machine instructions to call SUBSEQ just ...
    (comp.programming)
  • Re: Why does returning from a block cause a LocalJumpError
    ... If I understand correctly, even though b is defined inside the loop, a ... and b are both stored in the same stack frame when this method is ... I see your logic -- you're trying to look at how the language runtime ... Maybe I'm perseverating on this point, but I'm curious about how Ruby ...
    (comp.lang.ruby)
  • Re: Learning Lisp in Linux?
    ... To adapt the language to the task at hand? ... unless (member key excluded-keys) ... (loop (cddr property-list) ... correct property lists as results. ...
    (comp.lang.lisp)