Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
I have this piece of code, and since i put it on a new webserver, it has
caused an infinite loop. As you can see, n begins at 1, and B_x always = 1
here. So surely the loop begins by checking 1<2 then next it will be 2<2
whuch is false. So why does it continue to execute?
B_score only has one entry (at index [1]), and the code is supposed to only
output the items that exist (hence B_x =1).
Can anyone see a problem? I have looked at this time and time again, and
still cannot see a problem. I have checked to see if there is something
externally affecting it, such as $n being changed somehow, but cannot find
anything.
I am starting to hope that I AM doing something stupid!
Many thanks,
Andrew
CODE:
$B_average_total_points = 0;
$n=1;
echo "n = " . $n . ", B_x = " . $B_x . "<br><br>";
for ($n=1; $n<$B_x+1; $n++){
echo $n . ": " . $B_score[$n] . "<BR>";
flush();
$B_average_total_points = $B_average_total_points + $B_score[$n];
}
OUTPUT:
n = 1, B_x = 1
1: 23
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
etc...
Post Follow-up to this messageI have managed to get the code to work by changing the for loop to:
for ($n=1; $n<=$B_x; $n++){
However, on another part of the code, I have had to do the opposite to get
it to execute properly, ie, changing <=x to <x+1
Andrew
"Andrew Milne" <milney_boy@hotmail.com> wrote in message
news:boC5d.5769$B_5.2200@newsfe1-gui.ntli.net...
Hello,
I have this piece of code, and since i put it on a new webserver, it has
caused an infinite loop. As you can see, n begins at 1, and B_x always = 1
here. So surely the loop begins by checking 1<2 then next it will be 2<2
whuch is false. So why does it continue to execute?
B_score only has one entry (at index [1]), and the code is supposed to only
output the items that exist (hence B_x =1).
Can anyone see a problem? I have looked at this time and time again, and
still cannot see a problem. I have checked to see if there is something
externally affecting it, such as $n being changed somehow, but cannot find
anything.
I am starting to hope that I AM doing something stupid!
Many thanks,
Andrew
CODE:
$B_average_total_points = 0;
$n=1;
echo "n = " . $n . ", B_x = " . $B_x . "<br><br>";
for ($n=1; $n<$B_x+1; $n++){
echo $n . ": " . $B_score[$n] . "<BR>";
flush();
$B_average_total_points = $B_average_total_points + $B_score[$n];
}
OUTPUT:
n = 1, B_x = 1
1: 23
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
etc...
Post Follow-up to this message"Andrew Milne" <milney_boy@hotmail.com> wrote in message
news:boC5d.5769$B_5.2200@newsfe1-gui.ntli.net...
> Hello,
>
> I have this piece of code, and since i put it on a new webserver, it has
> caused an infinite loop. As you can see, n begins at 1, and B_x always =
1
> here. So surely the loop begins by checking 1<2 then next it will be 2<2
> whuch is false. So why does it continue to execute?
>
> B_score only has one entry (at index [1]), and the code is supposed to
only
> output the items that exist (hence B_x =1).
>
> Can anyone see a problem? I have looked at this time and time again, and
> still cannot see a problem. I have checked to see if there is something
> externally affecting it, such as $n being changed somehow, but cannot find
> anything.
>
> I am starting to hope that I AM doing something stupid!
>
> Many thanks,
>
> Andrew
>
>
> CODE:
>
> $B_average_total_points = 0;
> $n=1;
> echo "n = " . $n . ", B_x = " . $B_x . "<br><br>";
>
> for ($n=1; $n<$B_x+1; $n++){
Well, for starters, you are incrementing your limit on each test. $B_x + 1.
That part of the FOR statement is fully evaluated on each pass.
Next time you are trying to debug something like this, echo the values of
your controlling variable from inside your loop so you can see what is
actually going on.
> echo $n . ": " . $B_score[$n] . "<BR>";
> flush();
> $B_average_total_points = $B_average_total_points + $B_score[$n];
> }
>
> OUTPUT:
>
> n = 1, B_x = 1
>
> 1: 23
> 2:
> 3:
> 4:
> 5:
> 6:
> 7:
> 8:
> 9:
> 10:
> 11:
> 12:
> 13:
> 14:
> 15:
> 16:
> 17:
> 18:
> 19:
> etc...
>
>
Post Follow-up to this messageOn Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjg@DESPAMobsydian.com> wrote: > >Well, for starters, you are incrementing your limit on each test. $B_x + 1. >That part of the FOR statement is fully evaluated on each pass. Certainly it's fully evaluated, but it's an expression not an assignment. Nothing in the for loop above modifies anything except $n, and nothing in th e body of the loop as posted modifies either $n or $B_x. (Unless $B_average_total_points is in fact a reference to $B_x, but it's not in the code posted) -- Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk> <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Post Follow-up to this message"Andy Hassall" <andy@andyh.co.uk> wrote in message news:k0fjl09o4tgae6qq4ph1g0cb8si6vjjero@ 4ax.com... > On Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjg@DESPAMobsydian.com> > wrote: > 1. > > Certainly it's fully evaluated, but it's an expression not an assignment. > Nothing in the for loop above modifies anything except $n, and nothing in the > body of the loop as posted modifies either $n or $B_x. Oops. Apologies. I misread the code in my haste. Of course, that still leaves the question of why the loop is not terminating. > (Unless $B_average_total_points is in fact a reference to $B_x, but it's not > in the code posted) - Virgil
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.