Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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...



Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Milne
09-26-04 08:55 PM


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...




Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Milne
09-26-04 08:55 PM


Re: Weird Infinite loop
"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...
>
>



Report this thread to moderator Post Follow-up to this message
Old Post
Virgil Green
09-28-04 09:00 PM


Re: Weird Infinite loop
On 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

Report this thread to moderator Post Follow-up to this message
Old Post
Andy Hassall
09-28-04 09:00 PM


Re: Weird Infinite loop
"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



Report this thread to moderator Post Follow-up to this message
Old Post
Virgil Green
09-29-04 03:45 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:25 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.