Jump to content

crash pc while loop and for loop


redarrow

Recommended Posts

I was working with the for and while loop and the if statement and i used the below code, the sever came to a halt and untill i delete the below code the server wouldnt work.

strange is a while loop and for loop copied in the below format a crashing problam in genral.

If you delete the while loop and leave the for loop alls ok why?


[code]
<?

for($john=1; $john<25; $john++){

echo "<br>".$john;

if($john==10)break;

}

echo "<br> loop stoped at ".$john;


?>



<?

$john=10;
while($john<10) if($john==2){
echo $john;
$john++;
}
echo "<br> loop stoped at ".$john;
?>

[/code]
Link to comment
Share on other sites

[!--quoteo(post=357012:date=Mar 21 2006, 04:49 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 04:49 PM) [snapback]357012[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]
$john=10;

// if john is more or equal to 10, this while loop wont execute.
while($john<10)
   if($john==2){
     echo $john;
     $john++; <-- means that john will only ever increment when its value is 2
   }
   echo "<br> loop stoped at ".$john;
?>

[/code]
[/quote]

forgive me if i'm missing something, but i can't actually see why the while loop would even start, never mind hang. you set $john to 10, then specify that the while loop only runs whilst $john is LESS than 10. but lets say for example that the while loop started, and $john started off at 0, like in the 'for' loop.
the logic in your script would only ever cause $john to be incremented if $john reached 2. as $john starts off at 10 (or even if you started it from 0), this condition of $john being equal to 2 would never be met, $john's value would never be incremented, so $john would always be less than 10 so the while loop would go infinitely.
Link to comment
Share on other sites


With the code i was trying to get the if to stop and out put the loop.

Also can someone tell me about script hang sever please thank you.

[code]
<?

//set john to 1

$john=1;

//add while johnless then 10

while($john<10)

//add if john equal 2
if($john==2){


//add 1
$john++;

}
echo "<br> loop stoped at ".$john;
?>
[/code]
Link to comment
Share on other sites

With the code i was trying to get the if to stop and out put the loop.

Also can someone tell me about script hang sever please thank you.

[code]
<?

//set john to 1

$john=1;

//add while johnless then 10

while($john<10)

//add if john equal 2 brake to stop loop
if($john==2)brake;{


//add 1
$john++;

}
echo "<br> loop stoped at ".$john;
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=357019:date=Mar 21 2006, 05:17 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 05:17 PM) [snapback]357019[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Also can someone tell me about script hang sever please thank you.

[code]
<?

//set john to 1

$john=1;

//add while johnless then 10

while($john<10)

//add if john equal 2
if($john==2){
//add 1
$john++;

}
echo "<br> loop stoped at ".$john;
?>
[/code]
[/quote]

i just did. you're sending the while loop into an infinite cycle.

you set $john to 1, and you only increment it if $john = 2. but as nothin else increments $john, $john will NEVER equal 2, so the while loop will keep going because $john will ALWAYS be less than 10.
Link to comment
Share on other sites

[!--quoteo(post=357024:date=Mar 21 2006, 05:23 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Mar 21 2006, 05:23 PM) [snapback]357024[/snapback][/div][div class=\'quotemain\'][!--quotec--]
i just did. you're sending the while loop into an infinite cycle.

you set $john to 1, and you only increment it if $john = 2. but as nothin else increments $john, $john will NEVER equal 2, so the while loop will keep going because $john will ALWAYS be less than 10.
[/quote]

what about if john larger then 2


[code]
<?

//set john to 1

$john=1;

//add while johnless then 10

while($john<10)

//add if john larger then 2
if($john>2){
//add 1
$john++;

}
echo "<br> loop stoped at ".$john;
?>
[/code]
Link to comment
Share on other sites

[!--quoteo(post=357025:date=Mar 21 2006, 05:27 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 05:27 PM) [snapback]357025[/snapback][/div][div class=\'quotemain\'][!--quotec--]
what about if john larger then 2

[/quote]


i think you're misunderstanding either me or the 'while' function.
ask yourself this - how is $john ever going to be greater than 2, if it's only ever incremented when $john is greater than 2 and starts with the value of 1???
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]With the code i was trying to get the if to stop and out put the loop.[/quote]
Sorry, but that makes no sense.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Also can someone tell me about script hang sever please thank you.[/quote]
Your server will hang and eventually crash when you create an infinant loop. Ie, one that never ends. Which is exectly what your loop is. You firstly set $john equal to 1, then you start your loop, inside the loop you have an [i]if[/i] statement that says if $john is bigger than 2 incriment john. $john will NEVER be bigger than 2.
Link to comment
Share on other sites

[!--quoteo(post=357028:date=Mar 21 2006, 05:32 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Mar 21 2006, 05:32 PM) [snapback]357028[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Sorry, but that makes no sense.

Your server will hang and eventually crash when you create an infinant loop. Ie, one that never ends. Which is exectly what your loop is. You firstly set $john equal to 1, then you start your loop, inside the loop you have an [i]if[/i] statement that says if $john is bigger than 2 incriment john. $john will NEVER be bigger than 2.
[/quote]
thank you.


so how can i get the same result in a while loop as my for loop thank u.

[code]

<?

for($john=1; $john<25; $john++){

echo "<br>".$john;

if($john==10)break;

}

echo "<br> loop stoped at ".$john;


?>
[/code]
Link to comment
Share on other sites

to be honest, i don't know what youre trying to achieve. if youre actually doing this for a final project, dont. if youre doing it for a school/college project, don't. there are better ways of achieving the same thing.

here, you set up a for loop to go from 1 to 24. however, values 11-24 will never get served, cos you break at 10. why?
[code]
for($john=1; $john<25; $john++){
[/code]

why not do this instead? and you wont even need the 'break' line:
[code]
for($john=1; $john<11; $john++){
[/code]


i'm not even sure if 'break' works with while loops. but wrapping the only method of getting to the end of the while loop within a condition (IF) that will never be met is never going to help you.
perhaps if you were to explain what youre eventually trying to achieve, we could help better.

but the equivalent of your for loop anyway would be:

[code]
$john=1;

while ($john<11) {
   $john++;
}
[/code]
Link to comment
Share on other sites

Thank you for your help the while and for loop i was learning for my collage work.


The example i gave was on the collage cd i got, but they did not say use a while loop i thort you could sorry.

Also the brake and die was used from the cd tutor for an example how to use the die; and brake statement.

I have read your post and thank you for your time cheers.


Link to comment
Share on other sites

[!--quoteo(post=357040:date=Mar 21 2006, 06:01 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 06:01 PM) [snapback]357040[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I have read your post and thank you for your time cheers.
[/quote]

not a problem. honestly, i remember some of the stuff they used to teach me at college...

as for what type of loop to use, i generally have a few rules of thumb when deciding which one to use:

1) 'for' loops - i use these when i know how many times the loop needs to be executed, and i need to keep tabs on the counter

2) while loops - i use these if i need to test a condition or initialise some data BEFORE executing a loop an unspecified amount of times. for example, when i want to pull out a row from the database and display it, but dont initially necessarily know (or care) how many rows i have. this example pulls EVERY result from a database query:
[code]
while ($row = mysql_fetch_assoc($db_result))
{
   echo $row['name'].'<br>';
}
[/code]

3) do...while loops - same as 2, but when i want to execute a loop at LEAST once. eg:
[code]
$row = mysql_fetch_assoc($db_result);
do {
   echo $row['name'].'<br>';
while ($row = mysql_fetch_assoc($db_result));
[/code]
hope that'll clear it up.
Cheers
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.