php continue X - equivalent in c#
Question
in php :
continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
like
for ($i = 1; $i <= $countArray - 2; $i++) {
for ($j = $i+1; $j <= $countArray - 1; $j++) {
for ($k = $j+1; $k <= $countArray; $k++) {
if(condition){
# found
continue 3;
}
}
}
}
What is equivalent in c# ?
a simple way to do this ?
Solution
If you really really wanted to do it you could use goto statements:
for (int i = 0; i < 10; i++)
{
Level1:
for (int j = 0; j < 10; j++)
{
Level2:
for (int k = 0; k < 10; k++)
{
if (k < 5)
{
goto Level1;
}
if ( k == 7)
{
goto Level2;
}
}
}
}
OTHER TIPS
A goto can be used to get out of deeply nested loops. A C# equivalent for your PHP code could be:
for (int i = 1; i <= countArray - 2; i++) {
for (int j = i+1; j <= countArray - 1; j++) {
for (int k = j+1; k <= countArray; k++) {
if(condition){
// found
goto Found;
}
}
}
}
Found:
Console.WriteLine("Found!");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow