[Newbies] next or break in a loop

Andreas Wacknitz A.Wacknitz at gmx.de
Mon May 9 18:57:41 UTC 2016


Am 09.05.16 um 19:51 schrieb Joseph Alotta:
> Greetings,
>
> I am in need of a way to go to the end of a do loop.
>
> myCollection do:  [ :item |
>
>                  (blah blah) ifTrue: “we found an item of the first type”
>                   next item.
>
>                  (blah blah) ifTrue: “we found an item of the second type”
>                   next item.
>   
>                  (blah blah) ifTrue: “we found an item of the third type”
>                  next item.
>    
>                  (blah blah) ifTrue: “we found an item of the fourth type”
>                  next item.
>
>                  (blah blah) ifTrue: “we found an item of the fifth type”
>
> ].
>
>
> Some other languages have “break” or “next” or “goto LABEL” to skip the processing of the rest of the loop in case the item is found in the first test.
>
> How do I implement this in Squeak?
I don't have a good answer for your problem. Smalltalk offers a great 
interface for iterating over collections, >>do: is not the only one.
Alas there is no-one I am aware of that supports what is needed here 
(something like a split into different sub-collections).
 >>select: could be used to get all the items for one of your 
conditions. Thus, for your problem you would need five select:-loops,
which is sub-optimal performance-wise.
Maybe your problem can be rephrased such that Smalltalk's collection 
interface offers a better solution. Nevertheless, I suggest to take
a closer look at the remaining selectors like >>select:, >>reject:, 
 >>collect, ...

Regards
Andreas




More information about the Beginners mailing list