BBC BASIC for Windows
Programming >> BBC BASIC language >> FOR NEXT loop http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1372138836 FOR NEXT loop
Post by Matt on Jun 25th, 2013, 05:40am
Hi,
When using a FOR NEXT loop, I've always assumed that the variable after the TO is checked each time the loop is run to ascertain whether the looping has reached it's end, and if a calculation is required, e.g. t%-1, then is also done each loop. After some testing, this does not seem to be the case. Am I correct. Is this end figure - the result of t%-1, variable, or figure - set at the start and stored somewhere, e.g. on the stack, at the start of the looping?
Matt
Re: FOR NEXT loop
Post by admin on Jun 25th, 2013, 9:54pm
What follows the TO keyword is a numeric expression (not a variable), which like every expression is evaluated when it is encountered. There are no circumstances whatever in BBC BASIC when the evaluation of an expression is somehow 'deferred' until later (apart from when you do it in your own code using EVAL).
But I'm sure you knew that, so I wonder if you have failed to appreciate that the FOR statement is not part of the loop: it is executed only once at the start. Only the 'body' of the loop and the NEXT statement are executed multiple times. You can easily ascertain that using Trace and single-stepping.
This is totally standard in all dialects of BASIC, so far as I'm aware.
Richard. Re: FOR NEXT loop
Post by Matt on Jun 26th, 2013, 04:44am
I wonder if you have failed to appreciate that the FOR statement is not part of the loop: it is executed only once at the start. Only the 'body' of the loop and the NEXT statement are executed multiple times.
You're right. I had failed to appreciate that. I think it's one of those things that I misunderstood years ago, and then carried that misunderstanding through - until now.
As I was quite young when I first started programming with the BBC Micro and was not as interested in learning as I was in 'getting the job done', I guess there are quite a lot of misunderstandings in my reasoning when it comes to programming.
Thanks for explanation.
Matt
Re: FOR NEXT loop
Post by DDRM on Jun 26th, 2013, 08:19am
Thanks, that IS interesting. I'd also always assumed it went back to the FOR... statement, and had wondered, but never tested, whether the end condition was changeable.
I note that changing the control variable WILL alter the looping, though.
Best wishes,
D
Re: FOR NEXT loop
Post by admin on Jun 26th, 2013, 08:56am
I note that changing the control variable WILL alter the looping, though.
Since BBC BASIC is interpreted there's no way it can tell that you've changed the value of the control variable 'behind its back'. Before the advent of EXIT FOR the standard way of prematurely exiting a FOR...NEXT loop was to set the loop variable to (or past) the limit value. Setting it past the limit value provided a convenient way of being able to test, on exit, whether it was terminated prematurely or not.