IonicWind Software

IWBasic => The Roundtable => Topic started by: sapero on April 24, 2009, 12:48:29 PM

Title: Tipp: creating break-enabled loops
Post by: sapero on April 24, 2009, 12:48:29 PM
Current version of Emergence Basic does not have BREAK keyword, with one exception: BREAKFOR, designed for for-next loops.
Lets say you have a subroutine with a huge tree with IF:
sub init()
if (success1)
do_something1()
if (success2)
do_something2()
if (success3)
do_something3()
if (success4)
I have such trees and hate it when I need to scroll horizontally to see what it does, what the IF tries to check, or when I need to add one or more child IF nodes.

Now you may think what to do, to keep all the IF's visible? Simple, create a while (true) loop and a goto to jump outside the loop on error. Bad idea, you'll need different goto labels for each loop. But hey, we have breakfor:sub init()
pointer memory1 = failure
pointer memory2 = failure

int temp
for temp = 0 to 0 ' one time loop

memory1 = allocate1()
if (memory1 = failure) then breakfor

memory2 = allocate2()
if (memory2 = failure) then breakfor

do_something2()
next temp

if (memory1 <> failure) then dealloc1(memory1)
if (memory2 <> failure) then dealloc2(memory2)
endsub

Do you see the difference? If you need to add more nodes, just add it anywhere you wish. Easier to code and read.
Title: Re: Tipp: creating break-enabled loops
Post by: Steven Picard on April 24, 2009, 01:22:58 PM
That is really a great tip.  Thanks for sharing.