Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 10:19 25 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MMbasic 3.1 ’For...Next’ bug?

Author Message
Raros

Regular Member

Joined: 06/02/2012
Location: Italy
Posts: 55
Posted: 11:48pm 16 Feb 2012
Copy link to clipboard 
Print this post

Hi.

In a loop FOR....NEXT I will return an error:

10 For t = 1 to 5
Error: FOR without matching NEXT


error:

10 For t = 1 to 5
20 For t1 = 1 to 5
.........
50 Next t1,t

OK:

10 For t = 1 to 5
20 For t1 = 1 to 5
.........
50 Next t,t1

OK:

10 For tt = 1 to 5
20 For t = 1 to 5
.........
50 Next tt,t (or Next t,tt)

OK:

10 For t = 1 to 5
20 For x1 = 1 to 5
.........
50 Next x1,t (or Next t,x1)

OK:

10 For t = 1 to 8
20 For p = 1 to 3
.........
50 Next t,p (or Next p,t)

error:

10 For p = 1 to 7
20 For pp = 1 to 2
50 Next pp,p

OK:

10 For p = 1 to 7
20 For pp = 1 to 2
50 Next p,pp

etc.

Some explanation?

Thanks, Raros
 
BobDevries

Senior Member

Joined: 08/06/2011
Location: Australia
Posts: 266
Posted: 01:56am 17 Feb 2012
Copy link to clipboard 
Print this post

I seem to remember this bug appearing some time ago. I thought it had been squashed, but it seems to have re-appeared.

Regards,
Bob Devries
Dalby, QLD, Australia
 
djuqa

Guru

Joined: 23/11/2011
Location: Australia
Posts: 447
Posted: 02:35am 17 Feb 2012
Copy link to clipboard 
Print this post

Best solution is always use seperate matching next statements, as it is much easier to debug/trace program FLOW.


[code]
For Iterations =1 to 10 ' We need to do this 10 times
For prodCount = 1 to Number_Products ' How many Products we have
'
'
'
'
Next ProdCount
Next Iterations
[/code]
The above is better software engineering practice also.

Some of your above examples have invalid Syntax and should/would not be allowable by MMBasic.

e.g.
[quote]10 For tt = 1 to 5
20 For t = 1 to 5
.........
50 Next tt,t
[/quote]

Edited by djuqa 2012-02-18
VK4MU MicroController Units

 
Raros

Regular Member

Joined: 06/02/2012
Location: Italy
Posts: 55
Posted: 03:23am 17 Feb 2012
Copy link to clipboard 
Print this post

Thanks Djuga.

I already knew, i always use seperate matching next statements.
I just wanted to know why when a variable repeats in a cycle (a single and then double time 't' and 'tt' for example),
with the same letter by mistake and with different letters is ok (it is difficult to translate from Italian to English ).

Italian:
Questo lo sapevo già, uso sempre il comando NEXT separato. Volevo solo sapere come mai, quando si ripete una
variabile in un ciclo (una volta singola e poi doppia 't' e 'tt' ad esempio),
con la stessa lettera da errore e con lettere diverse è ok (è difficile da tradurre da italiano ad inglese, sono on po' imbranato con le lingue).

Raros.
Edited by Raros 2012-02-18
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6098
Posted: 04:58am 17 Feb 2012
Copy link to clipboard 
Print this post

Raros,
There is nothing wrong with your English. It's a lot better than my Italian!

I hadn't seen the problem before because I would usually put the NEXT code on separate lines.
This might be a better description of the bug:

This code works:
for nxy = 1 to 10
for nx = 11 to 21
print nxy,nx
next nx,nxy
end


This code also works - the order of the variables in the NEXT statement have been reversed without any problems.

for nxy = 1 to 10
for nx = 11 to 21
print nxy,nx
next nxy,nx
end


This code fails because the outer loop variable (n) is a substring of the inner loop variable (nxy) and it is placed after the inner loop variable.

for n = 1 to 10
for nx = 11 to 21
print n,nx
next nx,n
end


This code works - the outer loop variable is first in the NEXT statement.

for n = 1 to 10
for nx = 11 to 21
print n,nx
next n,nx
end



I haven't found anything in the help to indicate if multiple NEXT variables have to be in a defined order.

Jim

VK7JH
MMedit   MMBasic Help
 
Raros

Regular Member

Joined: 06/02/2012
Location: Italy
Posts: 55
Posted: 07:29pm 17 Feb 2012
Copy link to clipboard 
Print this post

Thanks very TassyJim.

Ok. I understood perfectly.

Actually I had guessed what you say:

"the outer loop variable (n) is a substring of the inner loop variable (nxy) and it is placed after the inner loop variable."

But I had many doubts.

And it is also true that the manual does not indicate a definite order in NEXT with several variables.

Thank you for your availability.

I do not give anything for granted, I like to fully understand things.

Greetings to all, Raros.
Edited by Raros 2012-02-19
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024