for loop inside a switch
| From: | jekillen | Date: | Sat, 01 Sep 2007 05:05:11 +0000 |
| Subject: | for loop inside a switch | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-261581@lists.php.net to get a copy of this message | ||
On Aug 31, 2007, at 6:26 PM, Robert Cummings wrote:
On Fri, 2007-08-31 at 15:56 -0700, Dan wrote:the break statement will break out of the switch, then inside the iteration code, after the switch block, test the variable for true. If it is true the use another break instruction. That should break out of the loop altogether at the end on that particular iteration. like so: for($i = 0; $i < somevalue; $i++)Sanjeev is right. You're thinking about the problem backwards. You're trying to build a Switch inside a loop. Remember, if you ever have to do some operating multiple times you're using a forloop, then the thing that you want to repeat(switch case) is INSIDE the for loop.Not always true. Sometimes once you've made the switch it applies to all members of a data array. In which case switching on every iteration is a waste of processor since you can check a single time outside the loop. Cheers, Rob. -- You can run a switch inside a for loop and when a case is matched set a variable to true.
{
$bar = false;
switch($anotherValue[$i]) // /this assumes you are looping through an indexed array
{ case 'foo':
$bar = true
//other code;
break;
default;
// $bar is not set to true so the loop continues
}
if($bar == true)
{
break;
}
/* instead of the default line in the switch statement
else
{
continue;
}
*/
}
Hope this is useful.
Jeff K