Menu
JAQForum Ver 19.10.27

Forum Index : Windmills : MM Basic labels

Posted: 03:58am
27 Jun 2023
Copy link to clipboard
Kalevi
Newbie

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
 
Posted: 05:39am
27 Jun 2023
Copy link to clipboard
Grogster
Admin Group


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.
 
Posted: 11:36pm
27 Jun 2023
Copy link to clipboard
Kalevi
Newbie

Thanks! GOTO worked just fine.
So much for the advice to avoid using GOTO and rely on IF and THEN.
 
Posted: 12:15am
28 Jun 2023
Copy link to clipboard
Grogster
Admin Group


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
 
Posted: 07:02am
11 Jul 2023
Copy link to clipboard
Kalevi
Newbie

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


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

© JAQ Software 2024