CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
for loop problem

 



ToeKnuckle
New User


Dec 28, 2013, 5:16 AM

Post #1 of 4 (1797 views)
for loop problem Can't Post

Hello I am new to perl I could use some help. I can't figure out why my for loop keeps reiterating continuously.


Code
#!/usr/bin/perl 
print "Content-type: text/html\n\n";
use strict;
use warnings;
use 5.010;

for (my $i = 1;$i < 6;$i++)
{
for (my $k = 1; $i < 6;$k++)
{
print "$i times $k equals " . $i * $k . "\n";
}
}


exit;



ToeKnuckle
New User


Dec 28, 2013, 5:53 AM

Post #2 of 4 (1796 views)
Re: [ToeKnuckle] for loop problem [In reply to] Can't Post

I figured it out..


Code
for (my $i = 1;$i < 6;$i++) 
{
for (my $k = 1; $i < 6;$k++)
{
print "$i times $k equals " . $i * $k . "\n";
last;
}
}



Laurent_R
Veteran / Moderator

Dec 28, 2013, 9:18 AM

Post #3 of 4 (1783 views)
Re: [ToeKnuckle] for loop problem [In reply to] Can't Post

OK, the addition of the last statement prevents your program from looping forever, but I doubt that's really what you want.

Output of your program:

Code
1 times 1 equals 1 
2 times 1 equals 2
3 times 1 equals 3
4 times 1 equals 4
5 times 1 equals 5


That's not really the multiplication tables that you presumably want to print out (otherwise I don't see the point of nested loops).

The error in your program lies in this line:

Code
for (my $k = 1; $i < 6;$k++)

The condition to stop the loop is on $i instead of being on $k.

Remove the last statement, and change $i to $k in your inner loop statement, this changes your program to this:

Code
for (my $i = 1;$i < 6;$i++)  
{
for (my $k = 1; $k < 6;$k++)
{
print "$i times $k equals " . $i * $k . "\n";
}
}


Now the output is probably what you are looking for:

Code
1 times 1 equals 1 
1 times 2 equals 2
1 times 3 equals 3
1 times 4 equals 4
1 times 5 equals 5
2 times 1 equals 2
2 times 2 equals 4
2 times 3 equals 6
2 times 4 equals 8
2 times 5 equals 10
3 times 1 equals 3
3 times 2 equals 6
3 times 3 equals 9
3 times 4 equals 12
3 times 5 equals 15
4 times 1 equals 4
4 times 2 equals 8
4 times 3 equals 12
4 times 4 equals 16
4 times 5 equals 20
5 times 1 equals 5
5 times 2 equals 10
5 times 3 equals 15
5 times 4 equals 20
5 times 5 equals 25


Having said that, Perl has much better looping constructs than C and can do what you want in much less typing:


Code
for my $i (1..5) { 
print "$i times $_ equals " . $i * $_ . "\n" for (1..5);
}


Which produces the following output:

Code
$ perl multipl.pl 
1 times 1 equals 1
1 times 2 equals 2
1 times 3 equals 3
1 times 4 equals 4
1 times 5 equals 5
2 times 1 equals 2
2 times 2 equals 4
2 times 3 equals 6
2 times 4 equals 8
2 times 5 equals 10
3 times 1 equals 3
3 times 2 equals 6
3 times 3 equals 9
3 times 4 equals 12
3 times 5 equals 15
4 times 1 equals 4
4 times 2 equals 8
4 times 3 equals 12
4 times 4 equals 16
4 times 5 equals 20
5 times 1 equals 5
5 times 2 equals 10
5 times 3 equals 15
5 times 4 equals 20
5 times 5 equals 25



BillKSmith
Veteran

Dec 29, 2013, 8:34 AM

Post #4 of 4 (1737 views)
Re: [ToeKnuckle] for loop problem [In reply to] Can't Post

It is usually considered poor style to use c-style for loops. Your mistake would not even have been possible in the preferred style.


Code
use strict; 
use warnings;
for my $i (1..6) {
for my $k (1..6) {
print "$i times $k equals " . $i * $k . "\n";
}
}

Good Luck,
Bill

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives