Recent

Author Topic: Here's an old friend...GOTO  (Read 4612 times)

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Here's an old friend...GOTO
« on: July 30, 2014, 09:03:11 pm »
There is still a use for GOTO, surely?  ::)

I read somewhere that the fpc compiler loves GOTO statements, as they optimise to machine code easily.

It's surprising that the current version of Laz/FPC still supports the LABEL and GOTO commands because they have been unfashionable for decades.  I did remember the syntax though..

Quick 'n dirty string input routine using LABEL and GOTO that won't let go until szPW is assigned a non-blank string:
Code: [Select]
var
  iRetries:Integer;
  sz,szPw:String;
LABEL STARTAGAIN,RETRYPW;
begin
iRetries:=3;
RETRYPW:
sz:='';
If iRetries=0 then GOTO STARTAGAIN;
  If InputQuery(
  Format('Input password.  Tries left %d',[iRetries]),
  'Please type in a non-blank passowrd',TRUE, sz) then
  szPW:=sz
  else // user closed query
   begin
    Dec(iRetries);
    GOTO RETRYPW;
   end;

  STARTAGAIN: // user cancelled 3 times
  iRetries:=3;
  If (sz = '') then GOTO RETRYPW; // user entered nothing
ShowMessage(szPW);   
« Last Edit: July 30, 2014, 09:06:24 pm by minesadorada »
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11451
  • FPC developer.
Re: Here's an old friend...GOTO
« Reply #1 on: July 30, 2014, 09:16:54 pm »
goto is mostly used in lowlevel code. Remember that Free Pascal implements its own RTL in itself. Having goto eliminates some uses for assembler.

Also, jumping with goto out multiple nested IFs is usually still faster than without. (afaik both FPC and Delphi have this trait)

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: Here's an old friend...GOTO
« Reply #2 on: July 30, 2014, 10:13:44 pm »
I think you must add COMEFROM to the syntax, too.

That can be really nice to write

minesadorada

  • Sr. Member
  • ****
  • Posts: 452
  • Retired
Re: Here's an old friend...GOTO
« Reply #3 on: July 30, 2014, 11:06:35 pm »
I think you must add COMEFROM to the syntax, too.

That can be really nice to write
Oh I like that.

If there was a statement DidntMeanToGOTO or OOPS it could replace EXCEPT, and be more truthful...

Try
 ...code..
OOPS
..apology here
End;
GPL Apps: Health MonitorRetro Ski Run
OnlinePackageManager Components: LazAutoUpdate, LongTimer, PoweredBy, ScrollText, PlaySound, CryptINI

Edson

  • Hero Member
  • *****
  • Posts: 1302
Re: Here's an old friend...GOTO
« Reply #4 on: July 31, 2014, 04:51:18 am »
I think you must add COMEFROM to the syntax, too.

That can be really nice to write
Oh I like that.

If there was a statement DidntMeanToGOTO or OOPS it could replace EXCEPT, and be more truthful...

Try
 ...code..
OOPS
..apology here
End;

Casually, I was looking for new instructions for a language that I'm writing. I will include these :D.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Here's an old friend...GOTO
« Reply #5 on: July 31, 2014, 05:14:40 am »
Casually, I was looking for new instructions for a language that I'm writing. I will include these :D.
Lolcode?

@minesadorada, you've made a perfect example of how, where and why NOT to use goto.

 

TinyPortal © 2005-2018