What do you think will print this program:
Code: Select all
10 FOR i=1 TO 1
20 PRINT i
30 NEXT i
40 PRINT i
50 NEXT i
60 PRINT i
70 NEXT i
80 PRINT i
Well, it will give no error and print:
Code: Select all
1
2
3
4
Key idea is absence of structured programming blocks in this interpreter and FOR/NEXT block-like behaviour in fact are imitated by nature of 'control for-loop variable':
FOR initializes it's variable as a special kind of variable (like strings or arrays are different kinds of variables too) and it keeps all parameters including line/statement for looping too!
Then NEXT is executed it strictly needs variable name to update it's value by Step and check condition of Limit. If Limit is not reached - it just GOTOs to looping line/statement!
That is FOR and NEXT are not have to be paired in any way, but just imitate structured blocks in this hidden manner. Special case is then FOR detects reaching of Limit in first interation - then it skips chain of operators for the next NEXT with the same looping variable. But if it is not the case - all looping job is done by NEXT i.
Interesting...
(I also tried to replicate this behaviour on C64 emulator, but it's BASIC seems to be smarter than that and behaves like destroing looping variable after Limit is reached. However details are unknown for me.)