Jump to content


Photo

crash pc while loop and for loop


  • Please log in to reply
11 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 04:49 PM

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?


<?

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;
?>


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 05:01 PM

[!--quoteo(post=357012:date=Mar 21 2006, 04:49 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 04:49 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$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;
?>

[/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.

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 05:17 PM


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.

<?

//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;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 05:17 PM

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.

<?

//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;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 05:23 PM

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

<?

//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;
?>
[/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.
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 05:27 PM

[!--quoteo(post=357024:date=Mar 21 2006, 05:23 PM:name=redbullmarky)--][div class=\'quotetop\']QUOTE(redbullmarky @ Mar 21 2006, 05:23 PM) View Post[/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


<?

//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;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 05:31 PM

[!--quoteo(post=357025:date=Mar 21 2006, 05:27 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 05:27 PM) View Post[/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???
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#8 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 21 March 2006 - 05:32 PM

[!--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 if statement that says if $john is bigger than 2 incriment john. $john will NEVER be bigger than 2.

#9 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 05:39 PM

[!--quoteo(post=357028:date=Mar 21 2006, 05:32 PM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Mar 21 2006, 05:32 PM) View Post[/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 if 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.


<?

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

echo "<br>".$john;

if($john==10)break;

}

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


?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#10 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 05:46 PM

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?
for($john=1; $john<25; $john++){

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


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:

$john=1; 

while ($john<11) {
   $john++;
}

"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#11 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 21 March 2006 - 06:01 PM

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.



Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#12 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 21 March 2006 - 06:09 PM

[!--quoteo(post=357040:date=Mar 21 2006, 06:01 PM:name=redarrow)--][div class=\'quotetop\']QUOTE(redarrow @ Mar 21 2006, 06:01 PM) View Post[/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:
while ($row = mysql_fetch_assoc($db_result))
{
   echo $row['name'].'<br>';
}

3) do...while loops - same as 2, but when i want to execute a loop at LEAST once. eg:
$row = mysql_fetch_assoc($db_result);
do {
   echo $row['name'].'<br>';
while ($row = mysql_fetch_assoc($db_result));
hope that'll clear it up.
Cheers
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users