Page 1 of 3

I have used a 'goto'

Posted: Sun Sep 01, 2013 6:12 pm
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

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 6:20 pm
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".

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 6:21 pm
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:

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 6:31 pm
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:

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 6:58 pm
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

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 7:12 pm
by sec
I mean:
While #True

or While 1

:lol:

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 7:46 pm
by IdeasVacuum
It's my first time.
:mrgreen: A goto virgin! That was rare in the 70s.......

Re: I have used a 'goto'

Posted: Sun Sep 01, 2013 8:49 pm
by Pot Noodle
Does this make the good old Amiga, Amos Basic a prostitute :wink:

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 5:41 am
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.

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 8:03 am
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*

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 9:26 am
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:

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 12:25 pm
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:

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 10:36 pm
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

Re: I have used a 'goto'

Posted: Mon Sep 02, 2013 11:37 pm
by BasicallyPure
Thanks Idle for a direct answer to the question that I asked. :)

BP

Re: I have used a 'goto'

Posted: Tue Sep 03, 2013 1:06 am
by idle
I was tempted to answer, yes, bad very bad! :lol:
but then thought it warranted an explanation before the thread went feral!