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

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

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

RedTigerFish




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
[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: Why the variable "a" is different out of the loop when codes are executed as a whole or by two steps?

Benoit St-Jean
nil is the sole instance of UndefinedObject.

It's the value assigned by default to every object (more or less but we'll get there when you'll get the basics) when they are created.  In your case, when you execute/inspect the very last line of your workspace, a is uninitialized, hence nil.

You must not see the whole workspace as a "program".  The environment, in a workspace, execute/prints/inspects just what you  *select* and executes/prints/inspects ONLY that! The workspace doesn't care what you executed before!  All it cares about evaluating is the portion of code that you highlight.

-----------------
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)



From: RedTigerFish <[hidden email]>
To: [hidden email]
Sent: Saturday, November 18, 2017 2:36 AM
Subject: [Newbies] Why the variable "a" is different out of the loop when codes are executed as a whole or by two steps?





Hello, friends.

This problem which confuses me comes from a  question
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:




-----
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
Reply | Threaded
Open this post in threaded view
|

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

Ben Coman
In reply to this post by RedTigerFish
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 <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


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