[Implemented] Premature Exits

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

[Implemented] Premature Exits

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

There are 2 commands I would like to see in PureBasic

1) A command that can be placed anywhere within a procedure
that causes immediate return from the procedure.

ie like RETURN in C, or EXPROC in some Basics

This is useful for error exits from a procedure and can
simplify or remove nested IF--ENDIF structures and extra
condition variables in subsequent code lines.

2) A command that can be placed anywhere within a FOR--NEXT
loop or WHILE--WEND loop that jumps to the end of the loop.

ie like BREAK in C, EXIT and EXIT_IF in some Basics

This is useful to simplify or eliminate IF--ENDIF
structures within a loop.

Who else misses these 2 useful commands?

Geoff
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Pupil.

You can use 'ProcedureReturn' to jump out of a procedure, just be carefull if you try to jump out of a Select:Case structure -it doesn't work properly yet.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by vanleth.

Had to admit I would use those features / commands if they existed.
But they would also make my code more messy for me in the long run.
Without them I'm kinda forced to write cleaner code and not just do stragihtforward Hack and Slash programming.

I would use them , but am better without them :)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.

Thanks Pupil.
From the help file, I thought that ProcedureReturn just set the return
result. It doesn't say that it also causes immediate return.

OK one down.

Any one any ideas on the BREAK loop exit?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by geoff.


I don't like the "Hack and Slash" comment Vanleth.
BREAK is not nasty like GOTO.

Isn't the first of these 3 routines clearest?
Maybe it's a personal thing?

Code: Select all

;find 1st string with weekend day


for i=1 to 100
  if FindString(a$(i),"Saturday",1)
    BREAK; found
  elseif FindString(a$(i),"Sunday",1)
    BREAK; found
  endif
next i



found=0
For i=1 to 100
  if FindString(a$(i),"Saturday",1)
    found=1
  else if FindString(a$(i),"Sunday",1)
    found=1
  endif
  if found
    i=100;force loop exit
  endif
next i



found=0
i=0
while not found
 i=i+1
 if FindString(a$(i),"Saturday",1)
   found=1
 else if FindString(a$(i),"Sunday",1)
   found=1
 endif
wend
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by vanleth.

Code: Select all

repeat
  if ...
    Goto breakit
  endif
until ...

breakit:
...
But thats even more worse coding style
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by vanleth.

To Geoff:
Well I admit the first one is most clear. Think what I tried to say was that I try to avoid using dirty loop breaks for the app's main loops. But when it comes to small routine looping, I can see your point.
Then clearly the first one is the most easy to read.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Yes, it's planned (Break/Continue C like operands).

Fred - AlphaSND
Post Reply