LB Booster
Programming >> Compatibility with LB4 >> for,next
http://lbb.conforums.com/index.cgi?board=compatibility&action=display&num=1490376123

for,next
Post by Alincon on Mar 24th, 2017, 5:22pm

The code below prints only one line, even though n>1.
Is the line "for n = 1 to n" not valid in LBB?

r.m.

Code:
 open fileName$ for input as #pjtTntNam
        n=0 : redim pjtTntNam$(99)
        while not (eof(#pjtTntNam))
            n = n+1
            input #pjtTntNam, pjtTntNam$(n)
        wend
    close #pjtTntNam
    sort pjtTntNam$(),1,n
    open fileName$ for output as #pjtTntNam
        for n = 1 to n
            print pjtTntNam$(n)
        next
    close #pjtTntNam

 

Re: for,next
Post by Richard Russell on Mar 24th, 2017, 6:18pm

on Mar 24th, 2017, 5:22pm, Alincon wrote:
Is the line "for n = 1 to n" not valid in LBB?

I wouldn't say it's "not valid" but it's certainly ambiguous. Nothing in the LB or LBB documentation tells you whether the initial 'n = 1' assignment happens before or after the evaluation of the 'to' value. In LBB it happens before, so it's equivalent to writing:

Code:
    for n = 1 to 1 

There are other ways of provoking different FOR..NEXT behaviors by writing ambiguous code. For example this:

Code:
    for n = 1 to 0
    next
    print n 

which prints 1 in LBB but 2 in LB 4.

The moral is: if you don't want uncertain results, don't write ambiguous code!

Richard.