Nested FOR loop

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
dwburger
Posts: 3
Joined: Wed 11 Mar 2020, 17:44

Nested FOR loop

Post by dwburger »

I'm trying to convert some BBC programs to Python/Turtle. My BASIC is rusty and I don't remember how nested FOR loops work. I've got this program:

0 REM Parabolas - Polygon
10 INPUT'"How many sides? "V
20 INPUT'"And steps per side? "N
30 MODE 1
40 FOR B=0 TO 6.28 STEP 2*PI/V
50 G=B+2*PI/V
60 T=B+4*PI/V
70 FOR H=500/N TO 501 STEP 500/N
80 P=(500-H)*SIN B+H*SIN G+640
90 Q=(500-H)*COS B+H*COS G+500
100 X=(500-H)*SIN G+H*SIN T+640
110 Y=(500-H)*COS G+H*COS T+500
120 MOVE P,Q
130 DRAW X,Y
140 NEXT H,B

What happens on line 140? Does the program go through all H values (e.g. completes the H loop) before returning to the B loop in line 40? Thanks for any help provided!
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

Re: Nested FOR loop

Post by p_m21987 »

You could just write

Code: Select all

NEXT: NEXT
and then it's much clearer and easier to read the code and understand what it's doing.
RichardRussell

Re: Nested FOR loop

Post by RichardRussell »

p_m21987 wrote: Sat 11 Apr 2020, 19:12You could just write

Code: Select all

NEXT: NEXT 
and then it's much clearer and easier to read the code and understand what it's doing.
I entirely agree that NEXT H,B should be avoided; not only is it unclear what happens but it will screw up the indentation. It's one of those features of the original language which, whilst it still works, is best consigned to history. NEXT , (where the comma is part of the statement) is even worse!

Having said that, I wouldn't necessarily recommend omitting the variable names. It may make it run fractionally faster, but I would prefer:

Code: Select all

        NEXT H : NEXT B
or even:

Code: Select all

        NEXT H
      NEXT B
dwburger
Posts: 3
Joined: Wed 11 Mar 2020, 17:44

Re: Nested FOR loop

Post by dwburger »

Thanks for the replies and I get what has been said. This code is not mine...it's taken from a book and I'm trying to re-write the logic in Python using Turtle to do the drawing. I just want to make sure that I understand what's going to happen here. If it WAS written, Next H: Next B, that means the H loop will complete before the next iteration of the B loop will be started...correct?
Last edited by dwburger on Sat 11 Apr 2020, 20:17, edited 3 times in total.
dwburger
Posts: 3
Joined: Wed 11 Mar 2020, 17:44

Re: Nested FOR loop

Post by dwburger »

May I impose on this group a little more and ask about how this expression would be interpreted:

P=(500-H)*SIN B+H*SIN G+640

I'm pretty clear on (500-H), but for SIN B+H and SIN G+640, will the SIN of B and G be determined first and then the additions take place or are the sums made first and the SIN of the sums made?
dwburger
Posts: 3
Joined: Wed 11 Mar 2020, 17:44

Re: Nested FOR loop

Post by dwburger »

I worked on it a bit more and found the solution to my questions. It turned out I was using too many parentheses in my statements and when I simplified things, it all worked.
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Nested FOR loop

Post by hellomike »

The theory for this is in the help or here Order of evaluation
RichardRussell

Re: Nested FOR loop

Post by RichardRussell »

dwburger wrote: Sun 12 Apr 2020, 00:27

Code: Select all

P=(500-H)*SIN B+H*SIN G+640
BBC BASIC programs that you find listed in books are quite likely to have been written for the old BBC Microcomputer or Acorn Electron, in the 1980s, at a time when memory was in very short supply. Therefore they will often favour conciseness over clarity, which is why you will see things like NEXT H,B or SIN C rather than NEXT H : NEXT B or SIN(C) (speed is slightly boosted too).

What you need to know is that those BBC BASIC built-in functions which take a single parameter can mostly be written without the parentheses, so CHR$A instead of CHR$(A) and COSB instead of COS(B). Even today I'll still do that sometimes, through force of habit, although the memory saving will be irrelevant and the speed benefit probably insignificant in 2020!

But the question needs to be asked: why are you translating it to Python (which I am not alone in considering a dreadful language, because of its dependence on whitespace) when you could more easily run it in BBC BASIC? Keeping it in BBC BASIC would likely also make it more compatible with different platforms, and easier to build a standalone appplication.