[Newbies] Does Smalltalk have "break" method to jump out of a loop ?
marcel.taeumel at hpi.de
Fri Nov 17 07:26:28 UTC 2017
If you want to abort some kind of repetition/enumeration in Smalltalk and "method return" is an option, sure, then you can just do that as stated in the examples before.
Else, consider the following examples, which might not be considered "idiomatic" but should illustrate aspects of the solution space --- especially in prototypical/exploratory source code:
#(a b c d 1 2 3) detect: [:each |
ifFalse: [Transcript showln: each. false "continue"]
ifTrue: [true "break"]].
break := false.
things := #(a b c d 1 2 3).
i := 0.
[break or: [i = things size]] whileFalse: [ | each |
i := i + 1.
each := things at: i.
ifFalse: [Transcript showln: each]
ifTrue: [break := true]].
break := false.
(1 to: 1000) detect: [:ea |
"Do some work here. Switch 'break' if needed."
break] ifNone: ["All numbers enumerated."].
In general, you should not have to "break" such kind of object enumeration in Smalltalk. Just select the objects you want to send a message to:
select: [:each | each shouldParticipate]
thenDo: [:each | each doSomeWork].
Am 17.11.2017 07:54:34 schrieb Bernhard Pieber <bernhard at pieber.com>:
Well, the first statement is the keyword message #to:do: sent to the receiver 1 with the parameters 130 and a one-argument block. The second is the keyword message #timesRepeat: sent to the receiver 100 with a zero-argument block as a parameter. What I meant is that it is no different from any other message send like 3 + 4. Neither looping nor conditionals are part of the syntax. None of the usual suspects like if, else, for, while etc. are reserved words. What follows is you can easily implement all kinds of useful „loop“ types yourself just by using messages and block closures.
1 to: 5 do: [:i | i = 50 ifTrue: [^self]]
However, this is not an ideal example to show idiomatic Smalltalk. Here is one from the Collection class:
"Evaluate aBlock with the elements of the receiver.
If aBlock returns false for any element return false.
Otherwise return true."
self do: [:each | (aBlock value: each) ifFalse: [^ false]].
The message enumerates the collection and as soon as an element is found, which does not satisfy the condition given by the block, it returns false from the method and therefore also breaks out of the loop.
> Am 17.11.2017 um 07:00 schrieb RedTigerFish :
> *1 to: 130 do: [:i| ] * or *100 timesRepeat: [ ] *
> Maybe because I learned some C before. I thought the above two are loops.
> The following is "break" example in C. How to implement that in Smalltalk?
> void main()
> for (int i = 0; i < 100;="" i++)="">
> if (i == 50)
> Dig, dig where you are,
> Down below's well.
> Let those that walk in darkness shout,
> Down below's hell.
> Sent from: http://forum.world.st/Squeak-Beginners-f107673.html
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
Beginners mailing list
Beginners at lists.squeakfoundation.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Beginners