Udjuni.com

udjuni.com search:
Home | Shopping Area | School management Home



Breaking out of loops with break statement in PHP


So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero is undefined", you may want to skip a chance to divide by zero.Bellow is a small piece of code that can help you accomplish this by using the break statement:

 $counter = -10;
for($i = $counter; $i <= 10; $i++){
  if($counter == 0){
     break;    //break out of the loop if $i = 0
  
  } else {
     $answer = 200 / $i;
     
echo "200 divided by $i = $answer"; } }

Why would we want to skip the $i = zero?


In natural language, dividing anything with zero results in an undefined answer. In the computing language however, trying to divide anything with zero results in warnings



RELATED ARTICLES:


Breaking out of loops with break statement
div class='floatleft'> So far, we have discussed how the while and for loops work. Assuming that you were looping through a set of values, dividing each value by a number between -10 and 10. Given that you are aware of the rule that "Anything divided by zero

make it or break it bond energies


breaking code periodic table


What the cash flow statement tell us
The cash flow statement is a financial document that shows how changes in balance sheet and income statements affect cash and cash equivalents. As an analytical tool, the cash flow statement is useful in determining the short-te

The Anatomy of an IF statement
In programming, there are cases when you need the program to perform a certain function when a certain condition is met. If such conditions are met then a certain piece of code is executed, otherwise skip it and execute an alternative code. This is ac

Did not find it? Try udjuni.com search:







© 2009 UdjunI LLC. All rights reserved | Privacy policy | Tutorial Index | RSS Feed