I have used a 'goto'

Just starting out? Need help? Post your questions and find answers here.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

I have used a 'goto'

Post by BasicallyPure »

Is this a safe way to use a goto?
It's my first time.

Code: Select all

Case #PB_Event_Menu
   Select EventMenu()
      Case #save
         trySaveImage: ;<--- this is where my goto goes to
         p$ = SaveFileRequester("",GetTemporaryDirectory()+d$,".jpg|*.jpg",0)
         If p$
            If LCase(GetExtensionPart(p$)) <> "jpg"
               p$ + ".jpg"
            EndIf
            
            If FileSize(p$) > -1 ; check if file exists
               If MessageRequester("File already exists","Do you wish to overwrite?",
                                   #PB_MessageRequester_YesNo) = #PB_MessageRequester_No
                  d$ = GetFilePart(p$)
                  Goto trySaveImage ;<-------- here it is!
               EndIf
            EndIf
            
            If SaveImage(#image, p$ , #PB_ImagePlugin_JPEG, 9) = 0
               MessageRequester("Error","unable to save image")
            Else
               d$ = GetFilePart(p$)
            EndIf
            
         EndIf
Now that I have used a goto, does this make me a bad person? :shock:

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: I have used a 'goto'

Post by Tenaja »

BasicallyPure wrote:Is this a safe way to use a goto?

Now that I have used a goto, does this make me a bad person? :shock:
For this situation, you are better off using a while or repeat loop.

Gotos are fine, in certain situations. For this one, a structured loop is more commonly accepted as "better".
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: I have used a 'goto'

Post by ts-soft »

BasicallyPure wrote:Now that I have used a goto, does this make me a bad person? :shock:

BP
Yes, very bad :twisted:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: I have used a 'goto'

Post by sec »

Code: Select all

;trySaveImage: ;<--- this is where my goto goes to
While True:
  p$ = SaveFileRequester("",GetTemporaryDirectory()+d$,".jpg|*.jpg",0)
  If p$
    If LCase(GetExtensionPart(p$)) <> "jpg"
      p$ + ".jpg"
    EndIf
    
    If FileSize(p$) > -1 ; check if file exists
      If MessageRequester("File already exists","Do you wish to overwrite?",
                          #PB_MessageRequester_YesNo) = #PB_MessageRequester_No
        d$ = GetFilePart(p$)
        ;Goto trySaveImage ;<-------- here it is!
        Continue
      EndIf
    EndIf
    
    If SaveImage(#image, p$ , #PB_ImagePlugin_JPEG, 9) = 0
      MessageRequester("Error","unable to save image")
    Else
      d$ = GetFilePart(p$)
    EndIf
    
  EndIf
  Break
Wend  
You can to re-write same as above code. Though, i don't know it is better goto or not :twisted:
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: I have used a 'goto'

Post by BasicallyPure »

ts-soft wrote:Yes, very bad
:mrgreen:

@sec
I never thought of using continue.
this confused me though:

Code: Select all

While True:
anyway I've got it working using a Repeat / Forever loop.
Thanks for the help.

Code: Select all

Case #PB_Event_Menu
   Select EventMenu()
      Case #save
         Repeat
            p$ = SaveFileRequester("",GetTemporaryDirectory()+d$,".jpg|*.jpg",0)
            If p$
               If LCase(GetExtensionPart(p$)) <> "jpg"
                  p$ + ".jpg"
               EndIf
               
               If FileSize(p$) > -1 ; check if file exists
                  If MessageRequester("File already exists","Do you wish to overwrite?",
                                      #PB_MessageRequester_YesNo) = #PB_MessageRequester_No
                     d$ = GetFilePart(p$)
                     Continue
                  EndIf
               EndIf
               
               If SaveImage(#image, p$ , #PB_ImagePlugin_JPEG, 9) = 0
                  MessageRequester("Error","unable to save image")
               Else
                  d$ = GetFilePart(p$)
               EndIf
            EndIf
            
            Break
         ForEver
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
sec
Enthusiast
Enthusiast
Posts: 792
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Re: I have used a 'goto'

Post by sec »

I mean:
While #True

or While 1

:lol:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: I have used a 'goto'

Post by IdeasVacuum »

It's my first time.
:mrgreen: A goto virgin! That was rare in the 70s.......
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Pot Noodle
Enthusiast
Enthusiast
Posts: 202
Joined: Sat Feb 18, 2012 10:21 pm
Location: Leicestershire

Re: I have used a 'goto'

Post by Pot Noodle »

Does this make the good old Amiga, Amos Basic a prostitute :wink:
P.N.
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: I have used a 'goto'

Post by electrochrisso »

If goto, gosub and the like are so bad, why does Fred keep it other than for backward compatibility. :?:
I mean anyone can program how they like, so long as they can create a workable application with the least amount of bugs.
PureBasic! Purely the best 8)
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: I have used a 'goto'

Post by Lord »

electrochrisso wrote:If goto, gosub and the like are so bad, why does Fred keep it other than for backward compatibility. :?:
...
Because GOTO is basic BASIC BASIC BASIC :!:

and
Because GOSUB is basic BASIC BASIC BASIC :!:

Same as jmp and jsr in ASM.
*medreamingofgoodold6502and68030days*
Image
User avatar
electrochrisso
Addict
Addict
Posts: 989
Joined: Mon May 14, 2007 2:13 am
Location: Darling River

Re: I have used a 'goto'

Post by electrochrisso »

Lord wrote:
electrochrisso wrote:If goto, gosub and the like are so bad, why does Fred keep it other than for backward compatibility. :?:
...
Because GOTO is basic BASIC BASIC BASIC :!:

and
Because GOSUB is basic BASIC BASIC BASIC :!:

Same as jmp and jsr in ASM.
*medreamingofgoodold6502and68030days*
So What :!: If is basic BASIC BASIC BASIC :!:

and
THEN is basic BASIC BASIC BASIC :!:

If you want to get silly about it. :lol:
PureBasic! Purely the best 8)
User avatar
Lord
Addict
Addict
Posts: 900
Joined: Tue May 26, 2009 2:11 pm

Re: I have used a 'goto'

Post by Lord »

Did I say "THEN isn't basic BASIC BASIC BASIC :!:" :?:

The issue here is
GOTO is basic BASIC BASIC BASIC :!:
and
GOSUB is basic BASIC BASIC BASIC :!:

That's it. :mrgreen:
Image
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: I have used a 'goto'

Post by idle »

BasicallyPure wrote:Is this a safe way to use a goto?
It's my first time.

Now that I have used a goto, does this make me a bad person? :shock:

BP
There is nothing wrong with goto BP

It's safe in that context.
A select statements pushes the stack so as long as you are using goto within the Select / EndSelect block
there is no risk of stack misalignment.

If you used goto to leave the Select / EndSelect block you would likely get an IMA on the procedure
return or an IMA calling another function and also end up accessing the wrong variables if any.

If you ever need stack safe goto gosub / return, you can use these macros for them
but they are only safe if you call them with the appropriate nesting depth.
http://www.purebasic.fr/english/viewtop ... 12&t=55242
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: I have used a 'goto'

Post by BasicallyPure »

Thanks Idle for a direct answer to the question that I asked. :)

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: I have used a 'goto'

Post by idle »

I was tempted to answer, yes, bad very bad! :lol:
but then thought it warranted an explanation before the thread went feral!
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply