Forum Moderators: coopster

Message Too Old, No Replies

difference between while loop and for loop

So weird

         

nanat

3:44 am on Oct 7, 2009 (gmt 0)

10+ Year Member



hi guys.. i just want to know if you experience it the same with i do for my loops..

this my for statement


if(isset($_POST["SQuery"]))
{

$value = count($_POST["packTy"]);value 2
$production = count($_POST["production"]);input type value 16

$x = 8;

for($y=0; $y<$value; $y++)
{
$PackTye = $_POST["packTy"][$y];

for($i=0; $i<$x; $i++)
{

echo '<table>';
echo '<tr>';
$production = $_POST["production"][$i];
$days = $_POST["days"][$i];
$location = $_POST["location"][$i];

echo '<td>'.$production.'--</td>';
echo '<td>'.$days.'--</td>';
echo '<td>'.$location .'--</td>';
echo '<td>'.$PackTye.'';
echo '</tr>';
echo '</table>';

}

}

if($production == $x)
{
$y = $x;

}
else
{
$x = $x + 8;

}
}

}


it will return 1st loops 0 to 15 second 0 to 7 it supposed to be 1st loop 0 to 7 2nd loop 0 = 7;

then i change it to do while looping


if(isset($_POST["SQuery"]))
{

$value = count($_POST["packTy"]);
$production = count($_POST["production"]);

$x = 8;
$i=0;
$y=0;
while ($y<$value)
{

$PackTye = $_POST["packTy"][$y];

while ($i < $x )
{
echo '<table>';
echo '<tr>';
$production = $_POST["production"][$i];
$days = $_POST["days"][$i];
$location = $_POST["location"][$i];

echo '<td>'.$production.'--</td>';
echo '<td>'.$days.'--</td>';
echo '<td>'.$location .'--</td>';
echo '<td>'.$PackTye.'';
echo '</tr>';
echo '</table>';

$i++;

}
if($production == $x)
{
$y = $x;

}
else
{
$x = $x + 8;

}
$y++;
}

}

two input loops

this code run smoothly but when i 1st input sequentially to '8'
then 2nd input sequentially to '9' this codes only return the 1st input '8' but 2nd input 9 vanishes..

and i tried 7 and 8 combination it works fine.. so weird T_T..

i thought for loops and do while loops are the same but i was wrong because of this differences

$y=0;
while ($y<$value)
{
$y++
}

and

for($y=0; $y<$value; $y++)

[edited by: coopster at 12:25 pm (utc) on Oct. 28, 2009]

rahul anand77

5:36 am on Oct 7, 2009 (gmt 0)

10+ Year Member



Hello,
The Three loops are diffrent and they used according to the situation.like "DO_WHILE" is used when you want to execute your script atleast once either the condition in the loop is true or false. "FOR" loop is used to decrease the probability of mistakes like the increment condition or else.
and "WHILE" loop is used when we know about the Number of Rotations.

Thanks