May 08, 2024, 11:11:13 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Tipp: creating break-enabled loops

Started by sapero, April 24, 2009, 12:48:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

sapero

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.

Steven Picard

That is really a great tip.  Thanks for sharing.