[Newbies] Why the variable "a" is different out of the loop when codes are executed as a whole or by two steps?

Ben Coman btc at openinworld.com
Sat Nov 18 11:42:59 UTC 2017


Try the same thing, but exclude the local variable declarations from your
selection.
.
.
The reason for the different behaviour is that within the Workspace you
don't actually have declare your variables.

If you don't declare variables within the scope of the selection, then they
are stored in the scope of Workspace and you use them after your code
completes.
If you do declare variables within the scope fo the selection, then they
are stored within the scope selection and that scope goes away when your
cocde completes.

HTH
cheers -ben

On 18 November 2017 at 15:36, RedTigerFish <chihuyu at gmail.com> wrote:

>
>
>
>
> Hello, friends.
>
> This problem which confuses me comes from a  question
> <http://forum.world.st/quot-Print-specific-integers-quot-
> Error-message-how-to-improve-and-what-causes-that-td5025067.html>
> I asked in "Squeak - Beginners" days ago.
>
> These are some special integers: myArray := #(2 32 44 67 89 111 123)
> These special integers need to be specially labelled from integer 1 to 130.
> The rest normal numbers which myArray doesn't include will be printed also.
>
> Here's the code:
>
> *|a n myArray|
>
> myArray := #(2 32 44 67 89 111 123).
>
> n := 1.
> a := myArray at: n.
>
> 1 to: 130 do: [:i|
>         i = a
>         ifTrue: [
>              Transcript show: ('Special number:  i = {1}, a = {2}' format:
> {i. a}).
>                  Transcript cr.
>                 n := n + 1.
>                     n = 8
>                    ifTrue: [a := 'I am no longer a number'.]
>                    ifFalse: [a := myArray at: n. ].
>                 ]
>           ifFalse: [
>                    Transcript show: ('i = {1}, a = {2}' format: {i. a}).
>                    Transcript  cr.
>                       ].
>         ].
>
>
> Transcript show: a; cr.     'Last line'
> *
>
> Now comes something that makes my head big.
>
> If I choose everything and do them, the last line, which should tell me
> what
> a is when program jumps out of the loop, exactly shows that a is "I am no
> longer a number".
>
> However, if I choose everything except the last line, and do them. After
> that, I do the last line. The output is nil.
>
> Why is a nil ?   Why's that?
>
> In order to make my question explicit, I recorded a video:
>
> https://www.youtube.com/watch?v=pUQAEO4uSpk
>
>
>
> -----
> 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
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/beginners/attachments/20171118/182b19bc/attachment-0001.html>


More information about the Beginners mailing list