Does Smalltalk have "break" method to jump out of a loop ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Does Smalltalk have "break" method to jump out of a loop ?

RedTigerFish
This post was updated on .
In C loop, there is break method which allows the program to jump out of the
loop when certain criteria has been met.

What is the counterpart in Smalltalk if there's one?

The following is "break" example in C. How to implement that in Smalltalk?

#include <stdio.h>
void main()
{  
     for (int i = 0; i < 100; i++)
         if (i == 50)
              break;
}




-----
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@lists.squeakfoundation.org
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

Benoit St-Jean
It can be done like this with a return, for example

1 to: 10 do: [:each |  (each = 1) ifTrue: [^'Found it!'].

The caret makes you "exit"


-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Friday, November 17, 2017, 12:21:20 AM EST, RedTigerFish <[hidden email]> wrote:


In C loop, there is break method which allows the program to jump out of the
loop when certain criteria has been met.

What is the counterpart in Smalltalk if there's one?



-----
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
--
_______________________________________________
Beginners mailing list

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
bpi
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

bpi
In reply to this post by RedTigerFish
There is none because there are no loops just message sends. What we have is the return statement that exits the whole method, which is oftenn used instead. If you post your Smalltalk code with a break statement I can show you how I would rewrite it to be idiomatic Smalltalk.

Cheers,
Bernhard

> Am 17.11.2017 um 06:21 schrieb RedTigerFish <[hidden email]>:
>
> In C loop, there is break method which allows the program to jump out of the
> loop when certain criteria has been met.
>
> What is the counterpart in Smalltalk if there's one?
>
>
>
> -----
> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

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?

#include <stdio.h>
void main()
{  
     for (int i = 0; i < 100; i++)
         if (i == 50)
              break;
}



-----
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

Benoit St-Jean
0 to: 99 by: 1 do: [ :each | each = 50 ifTrue: [ ^self ] ]


-----------------
Benoît St-Jean
Yahoo! Messenger: bstjean
Twitter: @BenLeChialeux
Pinterest: benoitstjean
Instagram: Chef_Benito
IRC: lamneth
Blogue: endormitoire.wordpress.com
"A standpoint is an intellectual horizon of radius zero".  (A. Einstein)


On Friday, November 17, 2017, 1:00:34 AM EST, RedTigerFish <[hidden email]> wrote:


*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?

#include <stdio.h>
void main()

    for (int i = 0; i < 100; i++)
        if (i == 50)

              break;
}



-----
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

RedTigerFish
Thanks a lot.



-----
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Dig, dig where you are,
Down below's well.
Let those that walk in darkness shout,
Down below's hell.
bpi
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

bpi
In reply to this post by RedTigerFish
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.

main
        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:

allSatisfy: aBlock
        "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]].
        ^ true

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.

Cheers,
Bernhard
 

> Am 17.11.2017 um 07:00 schrieb RedTigerFish <[hidden email]>:
>
> *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?
>
> #include <stdio.h>
> void main()
> {  
>     for (int i = 0; i < 100; i++)
>         if (i == 50)
>              break;
> }
>
>
>
> -----
> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

marcel.taeumel
Hi, there.

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 |
   each isNumber
      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.
   each isNumber
      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:

myThousandsOfObjects
   select: [:each | each shouldParticipate]
   thenDo: [:each | each doSomeWork].

Best,
Marcel

Am 17.11.2017 07:54:34 schrieb Bernhard Pieber <[hidden email]>:

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.

main
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:

allSatisfy: aBlock
"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]].
^ true

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.

Cheers,
Bernhard

> 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?
>
> #include
> void main()
> {
> for (int i = 0; i < 100;="" i++)="">
> if (i == 50)
> break;
> }
>
>
>
> -----
> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Does Smalltalk have "break" method to jump out of a loop ?

Mateusz Grotek-2
In reply to this post by RedTigerFish
To make you curious :-)

[ [ thisContext sender return: 34 ] value. 56 ] value


W dniu 17.11.2017 05:21:12, RedTigerFish napisał(a):

> In C loop, there is break method which allows the program to jump out  
> of the
> loop when certain criteria has been met.
>
> What is the counterpart in Smalltalk if there's one?
>
>
>
> -----
> 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
> [hidden email]
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners