Ask a Question related to PHP Development, Design and Development.

  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. Similar Questions and Discussions

    1. 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...
    2. 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...
    3. #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: ...
    4. #25037 [Opn]: SendText infinite loop
      ID: 25037 User updated by: richard at bradders2000 dot co dot uk -Summary: SentText infinite loop Reported By: ...
    5. 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...
  3. #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_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

  4. #3

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

    Virgil Green Guest

  5. #4

    Default Re: Weird Infinite loop

    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 + 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 / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
    <http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
    Andy Hassall Guest

  6. #5

    Default 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:
    >
    > >> 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

Posting Permissions

  • You may not post new threads
  • You may 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