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 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question do...while loop not working????

    my do while loop is not working ......
    it loops for only one iteration...
    here's the code:
    PHP Code:
    <?php
    $tot_days
    =200;
    $day=array('31','28','31',' '......................);//array that contains days of respective  month
    $i=0;
    do{
    $tot_days - =$day[$i];
    $i++;
    }
    while(
    $tot_days<0);       ///while($tot_days>0); also tried
    echo $i+1;
    echo 
    $tot_days;
    }
    it should give $i=7 and $tot_days=19
    but it is giving $i=2and $tot_days=169

    Please debug this ..................would be very greatful.....ok thanks for reading

  • #2
    Regular Coder
    Join Date
    Jul 2004
    Posts
    134
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Syntax of do while loop is:
    Code:
    do {
    
    everything one or more lines
    } while(one or more conditions separated by OR(||), AND(&&));
    e.g.,
    Code:
    $tot_days = 365;
    $day = array('31','28','31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
    $i = 0;
    do {
    	$tot_days = $tot_days - $day[$i];
    	$i = $i + 1; //++;
    }
    while($tot_days > 0);
    echo $i . " months.";
    echo "<br>";
    echo $tot_days . " remaining days of the year.";
    ?>
    Last edited by anshul; 06-24-2005 at 01:15 PM.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    190
    Thanks
    1
    Thanked 2 Times in 2 Posts
    while($tot_days<0); ///while($tot_days>0); also tried
    echo $i+1;
    echo $tot_days;
    }

    change to

    while($tot_days<0){ //see the "{"
    echo $i+1;
    echo $tot_days;
    }

  • #4
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    emehrkay, too bad that isn't the syntax for a do-while block.


  •  

    Posting Permissions

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