On Mon, 2016-05-09 at 12:51 -0500, Joseph Alotta wrote: > 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? >
Smalltalk doesn't have a switch/case statement but there are a few ways to implement the equivalent logic: 1) Nest the additional conditions inside ifFalse: statements: cond1 ifTrue: [trueLogic...] ifFalse: [cond2 ifTrue: [trueLogic] ifFalse: [otherLogic...]] This is the simplest method but can get hard to follow/edit pretty quickly re: brackets and formatting. You could also add a 'done' variable to avoid the nesting (and avoid multiple conditions from being satisfied in a single pass): done:=false. cond1 ifTrue: [trueLogic. done:=true]. (cond2 and: [done not]) ifTrue: [trueLogic. done:=true]. (cond3 and: [done not]) ifTrue: [trueLogic. done:=true]. done ifFalse: [otherLogic]. 2) Store the conditional blocks as values in a dictionary: switchBlock := Dictionary new at: condValue1 put: [block1]; at: condValue2 put: [block2]; at: condValue3 put: [block3]; yourself. then to evaluate it: (switchBlock at: condValue) value 3) Implement a switch method and call it: switchMethod: switchVal switchVal = cond1 ifTrue: [trueLogic. ^ whatever]. switchVal = cond2 ifTrue: [trueLogic. ^ whatever]. swit chVal = cond3 ifTrue: [trueLogic. ^ whatever]. otherLogic This is one area where I wish Smalltalk had nicer syntax / syntactic sugar for... any of the above is a bit clunky and you need it often enough where this can get tedious. > Sincerely, > > Joe. > > > PS. This is not a homework project. > > > > _______________________________________________ > Beginners mailing list > Beginners@lists.squeakfoundation.org > http://lists.squeakfoundation.org/mailman/listinfo/beginners _______________________________________________ Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners