...

View Full Version : do...while loop not working????



DineSh
06-24-2005, 10:25 AM
my do while loop is not working ......
it loops for only one iteration...
here's the 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

anshul
06-24-2005, 01:12 PM
Syntax of do while loop is:
do {

everything
one or more lines
}
while(one or more conditions separated by OR(||), AND(&&));

e.g.,

$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.";
?>

emehrkay
06-24-2005, 03:16 PM
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;
}

Kurashu
06-24-2005, 03:56 PM
emehrkay, too bad that isn't the syntax for a do-while block.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum