Internal switch cannot complete the FOR loop

I have a piece of code:

int n = 0;
for (int i = 0; i < 50;i++)
{
    n = checkStatus();
    switch (n)
    {
       case 1:
          break;
          break;//This is unreachable and so i cannot Terminate the For Loop within the SWITCH
    }
}

As described in the comment, I cannot complete the For Loop loop directly from the switch only if I declare a boolean and in the switch completion test

if(LoopShouldTerminate)
    break;

PS: or maybe I'm very confused!

[POST] I received a message and the problem was resolved, but I would like to mention that using a switch in a for loop is not an idea of ​​God, because I heard from many developers that I should exit the loop at the moment I receive the desired result, so when using the switch, an additional logical value is required or press the Int value i up to 50 direclty, but what happens if we use the while loop?

+5
8

1. :

for(int i = 0; i < 50; ++i)
{
    if (DoCheckStatus(i)) break;
}

...

bool DoCheckStatus(int i)
{
    switch(CheckStatus(i))
    {
        case 1 : return true;
        default: return false;
    }
}

2. , :

static void DoWhile<T>(this IEnumerable<T> sequence, Func<T, bool> predicate)
{
    foreach(T item in sequence) 
        if (!predicate(item)) return;
}

...

Enumerable.Range(0, 50).DoWhile(DoCheckStatus)

3. , :

Enumerable.Range(0, 50).DoWhile(i=> 
{ 
    switch(CheckStatus(i))
    {
        case 1 : return true;
        default: return false;
    }
});
+11

goto, switch.

        int n = 0;
        for (int i = 0; i < 50;i++)
        {
            n = checkStatus();
            switch (n)
            {
                case 1:
                    goto outofloop;

            }
        }

    :outofloop
        // more code

goto...

+6

return?

:

myLoopingMethod()
{
    int n = 0;
    for (int i = 0; i < 50;i++)
    {
        n = checkStatus();
        switch (n)
        {
            case 1:
                return;
        }
    }
}

- if/else switch/case. break, for

+2

i:

int n = 0;
        for (int i = 0; i < 50;i++)
        {
            n = checkStatus();
            switch (n)
            {
                case 1:
                    i += 50;
                    break;

            }
        }
+2

switch , for: :

bool doContinue = true;
for (int i = 0; i < 50 && doContinue; i++)
{
    n = checkStatus();
    switch (n)
    {
        case 1:
            doContinue = false;

    }
}
+2

.

                int n = 0;
            for (int i = 0; i < 50; i++)
            {
                bool shouldBreak = false;
                n = checkStatus();
                switch (n)
                {
                    case 1:
                        shouldBreak = true;
                        break;
                }

                if (shouldBreak)
                    break;
            }
0

, , , return. - .

switch (n)
{
    case 1:
        return;
}
0

; :

, :

 if(n == 1)
    break;

 switch(n) { }

, linq:

Enumerable.Range(0, 50).FirstOrDefault(x => listOfBreakCodes.Contains(checkStatus());

This should call checkStatusup to 50 times until it encounters 1 (or other interrupt codes) and then continues to evaluate the elements.

0
source

All Articles