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!
Nested FOR loop
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Nested FOR loop
You could just write
and then it's much clearer and easier to read the code and understand what it's doing.
Code: Select all
NEXT: NEXT
Re: Nested FOR loop
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!p_m21987 wrote: ↑Sat 11 Apr 2020, 19:12You could just writeand then it's much clearer and easier to read the code and understand what it's doing.Code: Select all
NEXT: NEXT
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
Code: Select all
NEXT H
NEXT B
-
- Posts: 3
- Joined: Wed 11 Mar 2020, 17:44
Re: Nested FOR loop
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.
-
- Posts: 3
- Joined: Wed 11 Mar 2020, 17:44
Re: Nested FOR loop
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?
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?
-
- Posts: 3
- Joined: Wed 11 Mar 2020, 17:44
Re: Nested FOR loop
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.
- hellomike
- Posts: 192
- Joined: Sat 09 Jun 2018, 09:47
- Location: Amsterdam
Re: Nested FOR loop
The theory for this is in the help or here Order of evaluation
Re: Nested FOR loop
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.