Re: [eclipse-users] Conditional jump out of loops

From: Kish Shen <kish.shen_at_...2...>
Date: Wed, 24 Jan 2007 20:02:20 +0000
On Tuesday 23 January 2007 23:47, David Tian wrote:
> Hi,
> 
> I want to find out how to jump out of a loop in ECLiPSe and go to the upper
> level of the program. In java, there is 'break'; is there a predicate does the
> same thing in ECLiPse?
> 
> Many thanks,
> David
> 
> _______________________________________________
> ECLiPSe-Users mailing list
> ECLiPSe-Users_at_...2...
> http://www.crosscoreop.com/mailman/listinfo/eclipse-users
> 
Hi David,

I will assume by `loops' you mean the `logical loops' (do/2) in ECLiPSe. 

To finish such  loops with success, you must meet the termination condition specified in the
loop specifications (i.e. the fromto and friends), e.g.

for(I,1,5) do write(I)

will terminate when I is 5.

If you want to specify a terminating condition that you can set at run-time, you can use a fromto:

fromto(_,End,End, end) do .....

and in your loop body, when you want to terminate, you unify End with end, End = end.

Note that *all* conditions given in the loop specifications must be meet for the loop to end,
so for example, if you use for(I,1,5), you cannot finish early before I == 5 and successfully exit
the loop.

You can always exit a loop early by failing, but you would loose any variable bindings etc. you 
have made.

You can also exit a loop by throwing an exception, as Paulo suggest (block/3 and exit_block/1),
but you should only use this for catching errors, rather as a general way of leaving loops.

Cheers,

Kish
Received on Wed Jan 24 2007 - 20:00:54 CET

This archive was generated by hypermail 2.3.0 : Tue Apr 16 2024 - 09:13:19 CEST