Jump to content

The break; statement


iwato

Recommended Posts

QUESTION: Does the break statement serve any useful purpose in the following code?COMMENT: It is my understanding that the break statement applies only to the if-loop in which it is contained, and that the if-loop is exited automatically anyway, as soon as the last line of code in the loop's code block has been executed.

<?php	function closest_Match($word_str, $word_arr) {		$shortest = -1;		foreach ($word_arr as $word) {			$lev = levenshtein($word_str, $word);			if ($lev == 0) {				$closest = $word;				$shortest = 0;				break;			}			if ($lev <= $shortest || $shortest < 0) {				$closest  = $word;				$shortest = $lev;			}		}		echo "Input word: $word_str<br />";		if ($shortest == 0) {			echo "Exact match found: $closest<br />";		} else {			echo "Did you mean: $closest?<br />";		}	}?>

Roddy

Link to comment
Share on other sites

QUESTION: Does the break statement serve any useful purpose in the following code?
<?php	function closest_Match($word_str, $word_arr) {		$shortest = -1;		foreach ($word_arr as $word) {			$lev = levenshtein($word_str, $word);			if ($lev <= $shortest || $shortest < 0) {				$closest  = $word;				$shortest = $lev;				if ($lev == 0) break;			}		}		echo "Input word: $word_str<br />";		if ($shortest == 0) {			echo "Exact match found: $closest<br />";		} else {			echo "Did you mean: $closest?<br />";		}	}?>

That's how I would do it, but yeah break does serve a purpose if there is no longer a reason to keep looking for words once you have the shortest levenshtein distance possible.

Link to comment
Share on other sites

<?php		foreach ($word_arr as $word) {			$lev = levenshtein($word_str, $word);			if ($lev <= $shortest || $shortest < 0) {				$closest  = $word;				$shortest = $lev;				if ($lev == 0) break;			}		}?>

Based upon what you have written and other uses of the break statement are the following two statements true?
  1. The break statement applies only to the loop in which it is contained and can be found anywhere in the loop.
  2. If the loop that contains the break statement is nested in another loop, the break statement applies only to the nested loop.

Roddy

Link to comment
Share on other sites

Both of those are true, however you can specify an integer "argument" for break that tells it how many levels to break out of. Also note that if blocks aren't loops.

Link to comment
Share on other sites

Both of those are true, however you can specify an integer "argument" for break that tells it how many levels to break out of. Also note that if blocks aren't loops.
Thanks, Synook.I will check out the use of integers with the break statement, as well.Roddy
Link to comment
Share on other sites

COMMENT: It is my understanding that the break statement applies only to the if-loop in which it is contained, and that the if-loop is exited automatically anyway, as soon as the last line of code in the loop's code block has been executed.
I could be understanding you wrong, but just in case...You don't see it in that particular code piece, but the loop is actually terminated immediatly, not after the current iteration is over. If you had something like:
<?php		for ($i=0; $i<1000; $i++) {			if ($i > 5) {				echo '!BOOM at 6!';				break;			}			echo $i;		}?>

You should see "12345!BOOM at 6!". If break was to continue the current iteration, and terminate afterwards, the result would've been "12345!BOOM at 6!6". And yes, conditions aren't a requirement - loops and/or switch is, so in theory, you can have:

<?php		for ($i=0; $i<1000; $i++) {			break;		}?>

Useless, but possible non the less.

Link to comment
Share on other sites

To expand on what Synook hinted at, a break statement will break out of a loop. The if structure is not a loop, so in your original code it will break out of the foreach structure, not the if structure (well, "in addition to" the if structure). Break and continue always operate on loops, so they will operate on the first loop they are in. This demonstrates breaking out of multiple loops:

for ($i = 0; $i < 10; $i++){  for ($j = 0; $j < 10; $j++)  {	for ($k = 0; $k < 10; $k++)	{	  if ($k >= 5 && $i == $j && $j == $k)		break 3;	  	}  }}echo 'i: ' . $i . '<br>';echo 'j: ' . $j . '<br>';echo 'k: ' . $k . '<br>';

In that code, "break" or "break 1" would break out of the k loop. "break 2" would also break out of the j loop, and "break 3" breaks out of all three loops.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...