For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Uber-beginner - question regarding "last" statement









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Uber-beginner - question regarding "last" statement
Trian3

2006-01-31, 6:56 pm

Hi all,

I'm following the book, "Teach Yourself Perl in 24 Hours" (Third Ed).
I'm just getting started and looking at the third chapter, came across
the following example.

for($i=0; $i<100; $i++) {
for($j=0; $j<100; $j++) {
if ($i * $j == 140) {
print "The product of $i and $j is 140\n";
last;
}
}
}

The intention of this example is to illustrate how the "last" statement
works. Well, the program works fine and I *think* I understand the
purpose of the last statement fairly well. However, just to see what
would happen, I hashed it out (#last;) and ran it again. Absolutely no
change in the output. Why is this?

Thanks,

Trian3

Xicheng

2006-01-31, 6:56 pm

Trian3 wrote:
> Hi all,
>
> I'm following the book, "Teach Yourself Perl in 24 Hours" (Third Ed).
> I'm just getting started and looking at the third chapter, came across
> the following example.
>
> for($i=0; $i<100; $i++) {
> for($j=0; $j<100; $j++) {
> if ($i * $j == 140) {
> print "The product of $i and $j is 140\n";
> last;
> }
> }
> }
>
> The intention of this example is to illustrate how the "last" statement
> works. Well, the program works fine and I *think* I understand the
> purpose of the last statement fairly well. However, just to see what
> would happen, I hashed it out (#last;) and ran it again. Absolutely no
> change in the output. Why is this?


sure it's the same, the "last" statement jumps out of the inner "for"
loop not the outer "for" loop. For a given $i, there may exist no more
than one integer $j which satisfies $i*$j = 140 (unless you can find
another such $j from 0 to 99, which is not possilbe in Real integer
numbers......).

Anyway, you may at least benefit from the "last" statement by reducing
some unnecessary iterations.....:-)

Xicheng

> Thanks,
> Trian3


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com