Home
JAQForum Ver 20.06
Log In or Join  
Active Topics
Local Time 17:07 27 Apr 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 : Windmills : MM Basic labels

Author Message
Kalevi
Newbie

Joined: 28/09/2021
Location: Australia
Posts: 9
Posted: 03:58am 27 Jun 2023
Copy link to clipboard 
Print this post

In 1988 I was testing something that required checking all the permutations to be checked. I wrote a program to compute all the permutations of N items in an orderly manner (eg. numbers 1 2 3 4 to 4 3 2 1 in numerical order). I wrote the program using MSX basic. Recently I rewrote the same program using my CMM2 in MM basic. It run without any changes. Now I dedicided to write it again without line numbers. Error message: Error in line 18: Unknown command (line 18 has the IF command).

Section of the original program:

170 IF S>Q THEN 180 ELSE 210
180 C(I)=1
190 A(I)=0
200 GOTO 230
210 C(I)=0
220 A(I)=A(I)+C(I+1)
230 C(N)=1
240 NEXT I

Section of the program without the line numbers

IF S>Q THEN CCC1 ELSE CCC2
CCC1: C(I)=1
A(I)=0
GOTO CCC3
CCC2: (I)=0
A(I)=A(I)+C(I+1)
CCC3: C(N)=1
NEXT I

Why the error?
NB -Now I notice I could have used: IF <=Q THEN 210 (simpler)
Kalevi
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 05:39am 27 Jun 2023
Copy link to clipboard 
Print this post

Hello.

Try IF S>Q THEN GOTO CCC1 ELSE GOTO CCC2

When you employ labels, you have to GOTO them specifically, if the code does not simply run over the top of the label during the normal passage through the loop.

In other words, NOT using GOTO, the interpreter is thinking CCC1 and CCC2 are commands or subroutine names.  It can't find either, so stops with the error.
Smoke makes things work. When the smoke gets out, it stops!
 
Kalevi
Newbie

Joined: 28/09/2021
Location: Australia
Posts: 9
Posted: 11:36pm 27 Jun 2023
Copy link to clipboard 
Print this post

Thanks! GOTO worked just fine.
So much for the advice to avoid using GOTO and rely on IF and THEN.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9063
Posted: 12:15am 28 Jun 2023
Copy link to clipboard 
Print this post

Well, it does depend on what you are doing to some extent!  

As a previous BIG user of GOTO, where I would GOTO all over the place, I can say that I agree that is a bad idea, and more structured programming with a main loop, IF/THEN/ELSE's etc, are the way to go.  It makes debugging code MUCH more easy, if you have some kind of "Flow" to the code, rather then jumping all over the place with GOTO, which can be a debugging nightmare with a large code.

But it can be hard to drop old habits.  

In your case, the code is not huge, so you get away with it fine.

You could "Modernize" your code by putting it all inside one IF/THEN/ELSE like this:


FOR I=?????? (we don't have this line of code, but put it here)
 IF S>Q THEN 'Do the CCC1 code...
   C(I)=1
   A(I)=0
 ELSE 'Do the CCC2 code...
   (I)=0
   A(I)=A(I)+C(I+1)
 ENDIF 'Now do the CCC3 code no matter what happens above...
 C(N)=1
NEXT I


Untested, but should do the same thing, and does away with hopping around with GOTO's.  
Edited 2023-06-28 10:18 by Grogster
Smoke makes things work. When the smoke gets out, it stops!
 
Kalevi
Newbie

Joined: 28/09/2021
Location: Australia
Posts: 9
Posted: 07:02am 11 Jul 2023
Copy link to clipboard 
Print this post

Thank you. That worked out well. I revisited a few of my programs.
Goodby GOTO (almost).
 
Print this page


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

© JAQ Software 2024