I was reading over a thread on BASIC speeds on various home computers and noticed an interesting point that was not explained.

Apparently, in MS BASICs, a loop using FOR I= ... NEXT will run faster than FOR I= ... NEXT I.

Does anyone know why? I imagine that not having to read the I would help a tiny bit, but the time difference seemed far too great to be that alone.

  • 4
    I think it is also because the interpreter doesn't have to match the 'I' up with the variable of the previous FOR.
    – Glen Yates
    May 11, 2018 at 13:32

2 Answers 2


The two statement forms are slightly different in meaning.

The meaning of NEXT is, roughly, "increment the loop variable for the most nested loop and go to the next iteration of the loop".

The meaning of NEXT I is, roughly, "while the mentioned variable is not the loop variable for the most nested loop, abandon execution of that loop, then perform as NEXT". The latter operation is more expensive.

For example,

10 FOR I=1 TO 5
20 FOR J=1 TO 5

will print 0, 1, 2, 3, 4.

  • Very interesting. What is the purpose of the second form though? Can you use it in an IF statement as a EXIT FOR proxy? Otherwise it seems like this would be an obvious source of hard to find errors! May 11, 2018 at 14:26
  • 3
    @MauryMarkowitz It works like a combination of the C language's break for the inner loop and continue for the outer loop. I cannot think of a concrete example now, but I remember wishing for that in C a few times.
    – Leo B.
    May 11, 2018 at 15:03
  • 3
    Been using BASIC since 1980 and I learned something new today. May 11, 2018 at 19:39
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – wizzwizz4
    May 14, 2018 at 15:37

Does anyone know why? I imagine that not having to read the I would help a tiny bit, but the time difference seemed far too great to be that alone.

Short Answer:

It's quite simple. Looking for a NEXT stack entry with a specific variable takes more time then just looking up the last NEXT- and beingpart of a loop makes it even more costly.

Detailed Answer:

Each FOR/NEXT loop creates a frame on the (BASIC) stack - like GOSUB, ON ERR or alike does. When a NEXT without a variable name occures, the last next-frame is searched and handled.

Now, if one (or more) variables are present, also the first NEXT entry is searched, like before, but further the variable needs to be parsed from the command, then searched in the variable table and finally compared to the one used in the stack entry found (fine details see below). If not equel, it gets repeated. This difference might sound small, but it adds up with every iteration.

Deeper Details:

(MS-) BASIC stores variable names within the tokenized program as character strings. To look up a variable from the name the string need to be searched in the variable tab, resulting in a pointer to the entry (and thus the value).

A for-stackframe stores, beside things like start value, sign or step value, the address of the variables value (within the variable table). So to compare if a variable given at NEXT is the same, it has first to be searched and then the addresses to be compared.

Bottom line: Finding a variable takes time

All of this is the reason why not only NEXT without a variable is faster, but also why speed of NEXT handling varies with the variable used. With an early declared variable, it will be faster than with a later one. Therfore a good old 1 LET I=0 at the beginning of a program using I as loop variable will speed up tings - the same way as the general habit of declaring often used variables in (MS-)BASIC will do.

  • Why should a next have to search through all variables, rather than checking the source-code name against the name of the variable associated with the for loop and, if they don't match, popping the for loop and trying again.
    – supercat
    May 12, 2018 at 20:36
  • @supercat Simply because neither the FOR statement nor the variable name is part of the stack frame. Before making up weired theories, it might be a good idea if you take a look into the MS-BASIC source code. And stay with the facts. Don't you think so? The question is about MS did it, not how it could have been done.
    – Raffzahn
    May 12, 2018 at 21:06
  • @Raffzahn - "It's quite simple" - I'm not so sure. Ignoring the oddity noted by Leo above, for now, the only legal loop format is that NEXT has to pop the last FOR. So then why read the variable at all? Actually reading the variable requires them to write MORE code, which ultimately does nothing. I'm still not clear on whether this was by design, or simply because that's the way BASIC-PLUS did it. May 13, 2018 at 16:53
  • @MauryMarkowitz No, it is't. Genuine Dartmouth BASIC did require that NEXT is always a companied by a variable name. It also included an error message for a mismach between NEXT and FOR. Thus checking the name is something inherited from the very origin of BASIC. Alowing NEXT without FOR is a later relaxation. And as your question is about the MS' BASIC implementation, code rules - and the code just works the way I described.
    – Raffzahn
    May 14, 2018 at 20:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .