Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Infinite loop?

  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post

    Infinite loop?

    hi.
    me newbie php.
    the following code works fine:
    PHP Code:
    $count = -3;
    while (
    $count <= 10) {
    echo 
    $count ", ";
    $count ++; 

    this code ALSO runs fine:
    PHP Code:
    $count = -3;
    while (
    $count <= 10) {
    if (
    $count == 5) {
    echo 
    "FIVE!, ";
    } else {
    echo 
    $count ", ";
    }
    $count ++; 

    So, how come THIS code seems to run in infinite loop? All I did was remove the curly-brace between the last echo and increment, and move it to the end.
    PHP Code:
    $count = -3;
    while (
    $count <= 10) {
    if (
    $count == 5) {
    echo 
    "FIVE!, ";
    } else {
    echo 
    $count ", ";
    $count ++; 
    }

    Notice there is no curly brace between echo and increment is first code example.
    Last edited by PonchoX; 03-22-2010 at 07:56 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,531
    Thanks
    8
    Thanked 1,091 Times in 1,082 Posts
    Because if count = 5, it will just keep printing "FIVE" and never increment.

    Increment outside of the "if-else" statement ....
    PHP Code:
    $count = -3
    while (
    $count <= 10) { 
    if (
    $count == 5) { 
    echo 
    "FIVE!, "
    } else { 
    echo 
    $count ", "
    }
    $count ++; 


  • #3
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    Thanks.
    I don' yet know enuf about PHP to fully understand what you said, but it seems if it never increments, that's what puts me into the infinite loop.

    Seems I made the increment part of the else statement when it shoulda went with the while-loop, yes?

    It never did print 'FIVE!' btw (not even once). It just went to 100%-CPU and I had to kill the server. (WAMP)

    "Increment outside of the "if-else" statement" .. seems to be the lesson here.
    Last edited by PonchoX; 03-21-2010 at 02:32 AM.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    It should increment eventually that is if you didn't have the space here $count ++;

    Remove it.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    PonchoX (03-22-2010)

  • #5
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    Ah, good catch. Thanks.

    Me newbie, obviously.

    Newbie mistake. Won't make that mistake again.

    Wonder why it worked with the space.


  •  

    Posting Permissions

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