<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 16 November 2017 at 16:19, RedTigerFish <span dir="ltr"><<a href="mailto:chihuyu@gmail.com" target="_blank">chihuyu@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello, *what I want is:*<br>
>From integers 1 to 130, I want to print some specific integers already given<br>
in an array.<br>
They are: 2 32 44 67 89 111 123 which are stored in small-to-big order.<br>
<br>
*Here's my codes:*<br>
<br>
|a n myArray|<br>
<br>
myArray := #(2 32 44 67 89 111 123).<br>
<br>
n := 1.<br>
a := myArray at: n.<br>
<br>
1 to: 130 do: [:i|<br>
        i = a<br>
        ifTrue: [<br>
                Transcript show: i; cr.<br>
                n := n + 1.<br>
                a := myArray at: n.<br>
                ].<br>
        ].<br>
<br>
The output is very good except for an Error Message.<br>
<br>
<<a href="http://forum.world.st/file/t371379/errormessage.png" rel="noreferrer" target="_blank">http://forum.world.st/file/<wbr>t371379/errormessage.png</a>><br>
<br>
By my current level, I have no idea why that Error Message appears.<br>
<br>
Q1: Why  Error Message appears ?<br></blockquote><div><br></div><div>Rather than give a direct answer, I think you'll gain the most if shown the path how to solve it yourself. </div>The key question is... What is the value of 'n' when the error occurs? </div><div class="gmail_quote">Click on the line Undefined>>DoIt line to open a debugger at that point and observe the instance variables.<br><div class="gmail_extra">Lets call that 'problemN'.  How does this compare to the number of elements in your array?</div><div class="gmail_extra"><br></div><div class="gmail_extra">Then try this...</div><div class="gmail_extra">problemN := "whatever it is".</div><div class="gmail_extra"><span style="font-size:12.8px">n := 1.</span><br style="font-size:12.8px"><span style="font-size:12.8px">a := myArray at: n.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">1 to: 130 do: [:i|</span><br style="font-size:12.8px"><span style="font-size:12.8px">        i = a</span><br style="font-size:12.8px"><span style="font-size:12.8px">        ifTrue: [</span><br style="font-size:12.8px"><span style="font-size:12.8px">                Transcript show: i; cr.</span></div><div class="gmail_extra"><span style="font-size:12.8px">                n := n + 1.</span></div><div class="gmail_extra">                (n+1 = problemN) ifTrue: [ self halt].<br style="font-size:12.8px"><span style="font-size:12.8px">                a := myArray at: n.</span><br style="font-size:12.8px"><span style="font-size:12.8px">                ].</span><br style="font-size:12.8px"><span style="font-size:12.8px">        ].</span><br></div><div><br></div><div>and from where it halts, practice with StepOver, StepThrough and StepInto to observe the code execution.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Q2: How can I improve that?<br></blockquote><div><br></div><div>Consider what is the value of 'n' when 123 is assigned to 'a', </div><div>and then what happens when next 'i=a' is true.</div><div><br></div><div><br></div><div><div><br class="gmail-Apple-interchange-newline">cheers -ben</div><div>Feed a man a fish, and he eats for a day.</div><div>Teach a man to fish, and he eats for life.</div></div></div></div></div>