Ask a Question related to PHP Development, Design and Development.
-
Andrew Milne #1
Weird Infinite loop
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...
Andrew Milne Guest
-
Infinite loop mounting disk
Hi, I had an old disk lying around and thought I put it in with my existing Ubuntu system. I used fdisk to create just one partition. It worked... -
Mysql infinite loop?!
this query doesnt work: SELECT d.cID,d.cLName,d.cFName , k.kid FROM cname d, cu_key e, rep_key f , `key` k WHERE (d.cLName LIKE '%') and (((e.cID... -
#25037 [Opn->Asn]: SendText infinite loop
ID: 25037 Updated by: iliaa@php.net Reported By: richard at bradders2000 dot co dot uk -Status: Open +Status: ... -
#25037 [Opn]: SendText infinite loop
ID: 25037 User updated by: richard at bradders2000 dot co dot uk -Summary: SentText infinite loop Reported By: ... -
How do i stop an infinite loop in MX director?
"hanct2002" <webforumsuser@macromedia.com> wrote in message news:bfvrfa$1p$1@forums.macromedia.com... end up having to ctrl-alt-delete .....is... -
Andrew Milne #2
Re: Weird Infinite loop
I 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...
Andrew Milne Guest
-
Virgil Green #3
Re: Weird Infinite loop
"Andrew Milne" <milney_boy@hotmail.com> wrote in message
news:boC5d.5769$B_5.2200@newsfe1-gui.ntli.net...1> 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 =only> 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 toWell, for starters, you are incrementing your limit on each test. $B_x + 1.> 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++){
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...
>
>
Virgil Green Guest
-
Andy Hassall #4
Re: Weird Infinite loop
On Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjg@DESPAMobsydian.com>
wrote:
Certainly it's fully evaluated, but it's an expression not an assignment.>>> 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.
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.
(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
Andy Hassall Guest
-
Virgil Green #5
Re: Weird Infinite loop
"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:k0fjl09o4tgae6qq4ph1g0cb8si6vjjero@4ax.com...1.> On Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjg@DESPAMobsydian.com>
> wrote:
>> >> >> for ($n=1; $n<$B_x+1; $n++){
> >Well, for starters, you are incrementing your limit on each test. $B_x +the>> >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 inOops. Apologies. I misread the code in my haste.> body of the loop as posted modifies either $n or $B_x.
Of course, that still leaves the question of why the loop is not
terminating.
not> (Unless $B_average_total_points is in fact a reference to $B_x, but it's- Virgil> in the code posted)
Virgil Green Guest



Reply With Quote

