for loop inside a switch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Aug 31, 2007, at 6:26 PM, Robert Cummings wrote:

On Fri, 2007-08-31 at 15:56 -0700, Dan wrote:
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. 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++)
    {
       $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

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux