Professional Web Applications Themes

Weird Infinite loop - PHP Development

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

  1. #1

    Default 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

  2. #2

    Default 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_boyhotmail.com> wrote in message
    news:boC5d.5769$B_5.2200newsfe1-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

  3. #3

    Default Re: Weird Infinite loop

    "Andrew Milne" <milney_boyhotmail.com> wrote in message
    news:boC5d.5769$B_5.2200newsfe1-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...
    >
    >

    Virgil Green Guest

  4. #4

    Default Re: Weird Infinite loop

    On Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjgDESPAMobsydian.com>
    wrote:
    >> 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.
    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.

    (Unless $B_average_total_points is in fact a reference to $B_x, but it's not
    in the code posted)

    --
    Andy Hassall / <andyandyh.co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage ysis tool
    Andy Hassall Guest

  5. #5

    Default Re: Weird Infinite loop

    "Andy Hassall" <andyandyh.co.uk> wrote in message
    news:k0fjl09o4tgae6qq4ph1g0cb8si6vjjero4ax.com...
    > On Tue, 28 Sep 2004 14:41:47 GMT, "Virgil Green" <vjgDESPAMobsydian.com>
    > wrote:
    >
    > >> 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.
    >
    > 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


    Virgil Green Guest

Similar Threads

  1. Infinite loop mounting disk
    By Michael in forum Ubuntu
    Replies: 1
    Last Post: May 14th, 11:16 PM
  2. Mysql infinite loop?!
    By StockN@gmail.com in forum MySQL
    Replies: 1
    Last Post: May 9th, 11:06 PM
  3. #25037 [Opn->Asn]: SendText infinite loop
    By iliaa@php.net in forum PHP Development
    Replies: 0
    Last Post: August 11th, 01:58 PM
  4. #25037 [Opn]: SendText infinite loop
    By richard at bradders2000 dot co dot uk in forum PHP Development
    Replies: 0
    Last Post: August 11th, 11:01 AM
  5. How do i stop an infinite loop in MX director?
    By David Downie in forum Macromedia Director Lingo
    Replies: 1
    Last Post: July 27th, 06:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139