Author Topic: Goto and Labels  (Read 5305 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Goto and Labels
« on: July 24, 2018, 10:41:49 am »
Is there a way of coding a GOTO in a Subroutine to LABEL outside of the subroutine?

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Goto and Labels
« Reply #1 on: July 24, 2018, 11:10:30 am »
This is usually a bad idea. What is the reason you need this?

FellippeHeitor

  • Guest
Re: Goto and Labels
« Reply #2 on: July 24, 2018, 11:38:02 am »
Can't do that.

The only occurrence of a GOTO in a SUB that actually redirects execution back to the main level is ON ERROR GOTO lineLabel.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Goto and Labels
« Reply #3 on: July 24, 2018, 12:36:44 pm »
What you'd want is to basically:

example 'call the sub
IF global_exit_code = whatever THEN GOTO label

SUB example
    global_exit_code = 0 'reset the exit code
    DO
       'Stuff
    LOOP
    IF exit_condition THEN global_exit_code = whatever : EXIT SUB
END SUB



Without a valid exit from a SUB/FUNCTION, you'll generate a STACK OVERFLOW ERROR and crash the program eventually.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Goto and Labels
« Reply #4 on: July 24, 2018, 03:50:28 pm »

Hi Fellippe, Hi Steve
fine to read you


Hi Dimster

why do you like use GOTO...LABEL  and not another control of flow of program?

IMHO to get your goal : the program executes the block marked by the label  out of the subroutine

1.
 you can use another Subroutine  to call for get this performance
   in pseudocode

   label1:  ---> SUB nameSubBlock
     instruction 1
     instruction 2
    instruction 3
.....
   instruction n
return  --->     END SUB

in this way in pseudocode you solve with:

SUB subtoexitForLabel1

IF n = m THEN nameSubBlock
END SUB

2. if you need that the block to execute must be in the main
    2.1  you can use a SHARED Flag to set the flow of program and in the Subroutine you set this flag and then EXIT SUB

     in pseudocode
 
MAIN
DIM SHARED FLAG1 AS INTEGER
   label1:
     instruction 1
     instruction 2
    instruction 3
.....
   instruction n

SubToExitForLabel1
if Flag1 = 2 GOTO Label1
END MAIN

SUB SubToExitForLabel1
     instruction 1
     instruction 2
    instruction 3
.....
IF n=m THEN
    Flag1 = 2
    EXIT SUB
END IF
     instruction 4
     instruction 5
    instruction 6
.....

END SUB

    2.2  moreover you can convert the SUBroutine in FUNCTION to get back the value to manage the flow
in pseudocode


MAIN
 
   label1:
     instruction 1
     instruction 2
    instruction 3
.....
   instruction n

IF SubToExitForLabel1% = 2  GOTO Label1
END MAIN

FUNCTION SubToExitForLabel1%

SubToExitForLabel1% = 0 ' Default result of FUNCTION
     instruction 1
     instruction 2
    instruction 3
.....
IF n=m THEN
    SubToExitForLabel1%
    EXIT FUNCTION
END IF
     instruction 4
     instruction 5
    instruction 6
.....

END FUNCTION


Programming isn't difficult, only it's  consuming time and coffee

FellippeHeitor

  • Guest
Re: Goto and Labels
« Reply #5 on: July 24, 2018, 04:28:55 pm »
Hi Fellippe, Hi Steve
fine to read you

Hello, TempodiBasic! What are you working on these days?

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Goto and Labels
« Reply #6 on: July 24, 2018, 04:29:21 pm »
My apologies, I was called away and just now reading your answers. What I'm trying to do is a "Restart" option of the entire program. I had hoped to begin the main part of the program as a label "Restart:", then simply have a GOTO Restart: in the Subroutine. As you are pointing out I have been having problems with that approach. At the moment was wondering if I can copy the entire beginning of my program to & including the first subroutine Call and hold this copy in a Subroutine of it's own called Restart. The User of the program then would have an option to either END or Restart. Having tried this yet, no doubt it will have this computer smoking.

FellippeHeitor

  • Guest
Re: Goto and Labels
« Reply #7 on: July 24, 2018, 04:32:57 pm »
To restart your program:

Code: QB64: [Select]

Without parameters, RUN will run restart execution of the current program. It still is a bad idea to use it from a SUB though:

http://qb64.org/wiki/RUN
« Last Edit: July 24, 2018, 04:42:08 pm by FellippeHeitor »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Goto and Labels
« Reply #8 on: July 24, 2018, 07:38:52 pm »
Hi Fellippe

 is this your? 
https://play.mob.org/game/make_7_hexa_puzzle.html
it let me think like 2048 but in hexagon cell and with a simpler playgame going to max 7 in sequence
and this?
http://www.coolmath-games.com/0-2048
or this one?
https://apkpure.com/br/2248/com.vector.game.puzzle.numberlink

Quote
Hello, TempodiBasic! What are you working on these days?
----
my goals are
-->to enlarge cryptography of Encrypt/Decrypt but I'm choosing what aspects to talk about now with examples...
-->to finish recipe Book (I'm thinking to use an internal list of file to open/delete/modify and a subfolder structure for storing image and text files)
--> continue to study and experiment with _MEM functions and instructions... the spin has been given from _PRESERVE option of REDIM
--> to finish ASCII_SPRITE Editor
--> to finish Terry Ritchie Library's tutorial for dummies like me :-)
--> to finish Unseen Library's tutorial for dummies like me :-)
--> to finish ASCII Trex runs of Google  Chrome
--> to finish Chess library to use to build up a chess program
--> to finish Chess program for analysis using Bob's Chess Interface
--> to colorize my ASCII T.U.I. library
---> to finish to follow Bob's QBasic tutorial
--> to finish to follow 3D engine Raycaster tutorial
and there are many other treasures that are in the waters of lake [abandoned, outdated and now likely malicious qb64 dot net website - don’t go there]....


 just to talk about which that my mind brings up to my attention in this moment...

I need a day of 48h and an year of  730 days :-)

Good Coding


Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Goto and Labels
« Reply #9 on: July 24, 2018, 07:47:16 pm »
Hi Dimster,

Steve had a similar problem with his Math trainer program. He used same solution Fellippe just mentioned. Retstart the program by rerunning it. Except, some people systems were having problems with the rerun.

Petr and I worked out an alternate method but I forgot how that went.

I would now set a dim shared flag, call it restartFlag, set it to true in the subroutine when conditions called for it and then the next line of code in main calling the subroutine where flag might be set, check if it was.

in main program:
mysub x, y, z
If restartFlag then goto restart
'else continue on with main code...

sub mysub
...
if condition = x then restartFlag = true : exit sub
...
end sub

So the restartFlag carries a signal from the subroutine to the main code where a goto can be used.
« Last Edit: July 24, 2018, 07:49:19 pm by bplus »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Goto and Labels
« Reply #10 on: July 25, 2018, 10:05:05 am »
Thanks bplus, will give it a shot. I do appreciate all the advice and comments on this topic, my thanks to you all

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Goto and Labels
« Reply #11 on: July 25, 2018, 10:55:06 am »
oh, PLUS don't forget to reset the restartFlag back to false, right after the restart label, otherwise the flag will short circuit the flow in main.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Goto and Labels
« Reply #12 on: July 25, 2018, 11:26:27 am »
Will do