Loops/Nested: Difference between revisions

Line 1,653:
=={{header|Smalltalk}}==
 
Notice that the original answer (see below) was wrong.
original text: "Smalltalk has no ways of escaping from loops (single or nested), even if it is possible to extend its iteration capabilities in several ways. "
 
{{works with|Smalltalk/X}}
Line 1,695:
exitBlock := [:exitValue | ^ exitValue].
[true] whileTrue:[ self value:exitBlock ]</lang>
in the same spirit, exits could be added to many other loop constructs. However, this is really only very rarely needed in Smalltalk, because a ^(return) out of a block returns from the enclosing method. This iswhich usually used to exit early from asearch utility methodmethods.
 
There existsis an additionalalso valueWithExit, which can be used to get out of a block early and provide an alternative value. Using that, the tasks solution is:
<lang smalltalk>|v result|
 
Line 1,714:
]
].
nil
] valueWithExit.
 
Line 1,732 ⟶ 1,733:
 
{{works with|GNU Smalltalk}}
original text: "Smalltalk has no ways of escaping from loops (single or nested), even if it is possible to extend its iteration capabilities in several ways. "
 
The following code implements a BiArray class with a method that allows iteration over the elements (by columns and then by rows) and execution of a block if a condition is true.
<lang smalltalk>"this simple implementation of a bidimensional array
Anonymous user