Exit scope

General FreeBASIC programming questions.
Post Reply
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Exit scope

Post by Gonzo »

is it possible to add the possibility for early exiting a scope?
it would be very useful to me...

right now i have a macro that i dont want to turn into a function,
and it needs to be run several times... so theres no option for a goto
its the kind of macro that putting a do loop inside would make no sense
vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

Scope
...
End Scope

???

-Vince
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

what?
i have a huge macro that i want to encapsulate in a scope
at some points inside that macro i want to Exit Scope
just like a Return in a function...
its code that would be a nightmare to turn into a function, and would reduce overall speed of code.. its not happening
its also the biggest bottleneck, so whatever i have to do, i will do it
exit scope would have been very useful, but i disabled some features and made it run by using a few extra if tests.. if i enable these features i NEED to be able to jump in the code, inside a macro..

come to think of it, it would be possible to:
#macro stuff(jumpto)

goto jumpto
#endmacro
right? thats one way i guess =)
pestery
Posts: 493
Joined: Jun 16, 2007 2:00
Location: Australia

Post by pestery »

You could use a Do ... Loop instead of Scope ... End Scope. Then you can use the Exit Do statement.

Code: Select all

Do
   ' Some code
   If condition Then Exit Do
   ' Some more code
   Exit Do
Loop
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Yeah, a 'Do..Loop While 0' block is effectively equivalent to a Scope.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Post by fxm »

counting_pine wrote:Yeah, a 'Do..Loop While 0' block is effectively equivalent to a Scope.
Yes, and it is faster than 'Do ... Exit Do : Loop'!
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

yea that works too, thanks :)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Exit scope

Post by dodicat »

Gonzo wrote:is it possible to add the possibility for early exiting a scope?
it would be very useful to me...

right now i have a macro that i dont want to turn into a function,
and it needs to be run several times... so theres no option for a goto
its the kind of macro that putting a do loop inside would make no sense
You can have goto alright, if you give the macro a different label each call

Code: Select all


#macro getmeout(label)
scope
    k=k+1
    dim as string dummy="Just passing through   @ " & str(k+25)
for z as integer=1 to 50
    if z=25+k then
        print dummy
goto label
end if
next z
label
end scope
#endmacro


dim as integer k
getmeout(oot:)
getmeout(finish:)
getmeout(free:)
getmeout(thankyou:)
sleep
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

can confirm that using goto labels for macro parameters made it work as expected =)

i would have preferred exit scope! but its ok
it was a multiple goto-thing.. it would have been ugly code with lots of always-ending loops =)
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

You could stop it looking like a loop by hiding the blocks in macros:

Code: Select all

#define _scope do
#define end_scope loop while 0

_scope

    '...

    exit _scope

    '...

end_scope
Either way it's nicer than any Goto-based solution, and it avoids having to invent multiple Goto labels.
Gonzo
Posts: 722
Joined: Dec 11, 2005 22:46

Post by Gonzo »

yea, thats a neat trick
i think ill do that, since its a little bit prettier again :D

thanks!
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Re: Exit scope

Post by agamemnus »

bump
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Exit scope

Post by D.J.Peters »

agamemnus wrote:bump
Any questions ?

I wonder me what dodicat posted at Sat Nov 19, 2011

Code: Select all

if z=25+k then
Looks as C/C++ code for me.

I did not that FreeBASIC evaluates an assignment in a if expression ?

Joshy
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Exit scope

Post by counting_pine »

D.J.Peters wrote:

Code: Select all

if z=25+k then
Looks as C/C++ code for me.

I did not that FreeBASIC evaluates an assignment in a if expression ?
It's an equality test. FB doesn't have '=='.
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Re: Exit scope

Post by agamemnus »

Any chance of this happening, or 0.
Post Reply