Jump to content

understand brake and continue properly......


redarrow

Recommended Posts

advance thank you.......

 

Can you give me a proper example, why we need the brake and continue command

for the, for loop please....

 

understand  break and continue but when do we use it or what for

 

thank you.........

 

<?php


// ((continue)) forces the code to carry on looping, and show's the result minus the if condition.........

$a=array("john","paul","petter","lucke");


for($b=0; $b<count($a); $b++){


if($a[$b]=="petter"){

	continue;

}

echo " <br> continued minus condition $a[$b] <br>";

}

?>

 

 


<?php


// ((break)) forces the loop to stop, and only shows the remaining echoed condition before the break.........

$a=array("john","paul","petter","lucke");


for($b=0; $b<count($a); $b++){


if($a[$b]=="petter"){

	break;

}

echo " <br> braked minus condition $a[$b] <br>";

}

?>

Link to comment
https://forums.phpfreaks.com/topic/135623-understand-brake-and-continue-properly/
Share on other sites

Use continue if you want to skip one iteration of the loop. So when $a[$b]=="petter" one iteration of loop will be skipped, but the loop will run efterwards.

Use break, if you want to stop the loop. When $a[$b]=="petter" then the loop stops and script execution restarts from the first line after it.

 

 

Real life examples:

 

Consider a query that returns result set as this

SKIP, 1
foo, 2
bar, 3
SKIP, 4
baz, 5
STOP, 6
bazbar, 7

 

while($row = mysql_fetch_array($result)) {
  if ($row[0] == "SKIP") continue;
  if ($row[0] == "STOP") break; 
  echo $row[1]."<br\>/n";
}

 

This will echo

2

3

5

 

output

<br> paul <br> <br> lucke <br> <br> ron <br>

 

 

<?php

$a=array("john","paul","petter","lucke","ron");

for($b=0; $b<count($a); $b++){


if($a[$b]=="john"){continue;}

if($a[$b]=="petter"){continue;}

if($a[$b]=="luke"){break;}

echo " <br> $a[$b] <br>";

}
?>

continue is commonly used to filter results, that are not needed.

 

For example: I have a textarea form into which some data from Excel is pasted, and then posted to PHP

In php I analyze the data line by line. If there's an empty line, I just skip it.

 

 

break is used to stop processing, when for example a value has been found, or the result of calculation fits your requirements

 

In the example with the textarea before: I could have stop processing using break, when 10 non-empty lines were processed.

 

The pseudo code would look like this

 

$data = $_POST['textarea'];
$lines = explode("\n",$data);
$count = 0;
foreach ($lines as $key => $line) {
if(empty($line)) continue;   //the line is empty, nothing to do, go to next line
process_line($line);  // a function that does something with line
$count++;   //increase number of lines processed
if ($count >= 10) break;  //stop the loop after 10 processed lines
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.