BBC BASIC for Windows
« FOR NEXT loop »

Welcome Guest. Please Login or Register.
Apr 6th, 2018, 12:20am



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: FOR NEXT loop  (Read 665 times)
Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx FOR NEXT loop
« Thread started 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: FOR NEXT loop
« Reply #1 on: Jun 25th, 2013, 9:54pm »

on Jun 25th, 2013, 05:40am, Matt wrote:
Am I correct?

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.
User IP Logged

Matt
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 210
xx Re: FOR NEXT loop
« Reply #2 on: Jun 26th, 2013, 04:44am »

on Jun 25th, 2013, 9:54pm, Richard Russell wrote:
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
User IP Logged

DDRM
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 321
xx Re: FOR NEXT loop
« Reply #3 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
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: FOR NEXT loop
« Reply #4 on: Jun 26th, 2013, 08:56am »

on Jun 26th, 2013, 08:19am, DDRM wrote:
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.

Richard.
« Last Edit: Jun 26th, 2013, 08:56am by admin » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls