FutureBasic Logo

<<    Index    >> FutureBasic

break   statement



Syntax
break

Description
The break statement works only within a loop (such as for/next, do/until, while/wend) and is somewhat like the continue statement. Instead of skipping a loop iteration like continue, break terminates the current loop and resumes execution at the first statement following the loop body.

break is recommend over the older exit xxxx statements (like exit for/exit next) because the exit family of statements override any smart branching decisions the LLVM clang compiler may make. For example, exit for, when there are two or more nested loops (say a while inside a for loop), can be used to exit not only the inner while loop but also the outer loop. This can potentially bypass compiler optimizations and memory managment and is therefore risky.

The following examples show break's mechanics:

Example1

while ( x )
if ( x == 25 ) then break // exit the while loop
x++ // this statement...
fn CallSomeThingElse // and this statement are skipped when a break occurs
wend

fn DoSomeGoodWork // the break above branches here directly and skips the remaining statements within the while loop

Example2
Note: break used inside a nested loop exits only the innermost loop and not the outer loop.

for i = 1 to 50
while ( x )
if ( x == 25 ) then break // exit the while loop
x++ // this statement...
fn CallSomeThingElse // and this statement are skipped when a break occurs
wend
fn DoSomeGoodWork // the break above branches here directly and skips the remaining statements within the while loop
next

See also
continue; for; while; do; select case; exit [structure]