[Newbies] "Print specific integers" Error message, how to improve ?

Ben Coman btc at openinworld.com
Thu Nov 16 10:33:12 UTC 2017


On 16 November 2017 at 16:19, RedTigerFish <chihuyu at gmail.com> wrote:

> Hello, *what I want is:*
> From integers 1 to 130, I want to print some specific integers already
> given
> in an array.
> They are: 2 32 44 67 89 111 123 which are stored in small-to-big order.
>
> *Here's my codes:*
>
> |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: i; cr.
>                 n := n + 1.
>                 a := myArray at: n.
>                 ].
>         ].
>
> The output is very good except for an Error Message.
>
> <http://forum.world.st/file/t371379/errormessage.png>
>
> By my current level, I have no idea why that Error Message appears.
>
> Q1: Why  Error Message appears ?
>

Rather than give a direct answer, I think you'll gain the most if shown the
path how to solve it yourself.
The key question is... What is the value of 'n' when the error occurs?
Click on the line Undefined>>DoIt line to open a debugger at that point and
observe the instance variables.
Lets call that 'problemN'.  How does this compare to the number of elements
in your array?

Then try this...
problemN := "whatever it is".
n := 1.
a := myArray at: n.

1 to: 130 do: [:i|
        i = a
        ifTrue: [
                Transcript show: i; cr.
                n := n + 1.
                (n+1 = problemN) ifTrue: [ self halt].
                a := myArray at: n.
                ].
        ].

and from where it halts, practice with StepOver, StepThrough and StepInto
to observe the code execution.



> Q2: How can I improve that?
>

Consider what is the value of 'n' when 123 is assigned to 'a',
and then what happens when next 'i=a' is true.



cheers -ben
Feed a man a fish, and he eats for a day.
Teach a man to fish, and he eats for life.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/beginners/attachments/20171116/46882589/attachment.html>


More information about the Beginners mailing list