开关内部中断无法终止 FOR 循环

发布于 2024-12-03 12:23:01 字数 575 浏览 1 评论 0原文

我有一个代码片段:

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
    }
}

如评论中所述,只有当我声明一个布尔值并在 Switch 测试结束时,我才能直接从 Switch 终止 For 循环

if(LoopShouldTerminate)
    break;

PS:或者也许我很困惑!

[发布] 我收到消息,问题已解决,但我想假设在 for 循环中使用 Switch 不是一个好主意,因为我从很多开发人员那里听说,当我得到想要的结果,所以使用 switch 需要额外的布尔值或将 Int i 值直接推到 50,但是如果我们使用 while 循环会发生什么?

I have a code snippet :

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 a comment I cannot terminate the For Loop directly from the Switch, only if I declare a boolean and at the End of Switch test

if(LoopShouldTerminate)
    break;

PS : or maybe I'm very confused!

[POST]
I got the message ,and the problem is Solved ,but i would like to asume that using Switch within a for loop isn't a god idea ,because i heard from lot of developer's i should break from loop in the moment when i get the desired result ,so using switch need's extra boolean or push the Int i value to 50 direclty , but what would happen if we're using while loop ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

驱逐舰岛风号 2024-12-10 12:23:01

解决方案 1:将循环和 switch 移至不同的方法:

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;
    }
});

Solution 1: Move the loop and the switch to different methods:

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;
    }
}

Solution 2: Adapt the above to eliminate the loop with an eager extension method:

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)

Solution 3: Adapt the above to eliminate the loop and the helper method:

Enumerable.Range(0, 50).DoWhile(i=> 
{ 
    switch(CheckStatus(i))
    {
        case 1 : return true;
        default: return false;
    }
});
深巷少女 2024-12-10 12:23:01

您可以使用 goto 来中断跳出 switch 内的循环。

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

            }
        }

    :outofloop
        // more code

goto 为数不多的好用处之一...

You can use goto in order to break out of the loop within the switch.

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

            }
        }

    :outofloop
        // more code

One of the few good uses of goto...

謌踐踏愛綪 2024-12-10 12:23:01

只需更改 i 的值即可:

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

            }
        }

Just change the value of i:

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

            }
        }
撕心裂肺的伤痛 2024-12-10 12:23:01

如果 switch 之后没有其他代码,您可以在 for 循环本身中检查是否继续循环:

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

    }
}

If there is no other code after the switch you can just check in the for loop itself whether to continue looping:

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

    }
}
赠意 2024-12-10 12:23:01

您可以将循环放在方法内部并只使用 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循环

Can you place the loop inside of a method and just use return?

Example:

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

Another option would be to use a traditional if/else instead of a switch/case. Then you can just use break and it will jump out of your for loop

╰ゝ天使的微笑 2024-12-10 12:23:01

您可以使用布尔值。

                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;
            }

You could use a boolean.

                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;
            }
同尘 2024-12-10 12:23:01

我不知道循环完成后是否需要执行其他逻辑,但您可以尝试使用 return 代替。另一个选项是设置一个布尔标志,并在切换后退出。

switch (n)
{
    case 1:
        return;
}

I don't know if there's other logic that needs to be performed after the loop is finished, but you could try using return instead. The other option is to set a boolean flag, and exit after the switch.

switch (n)
{
    case 1:
        return;
}
偏爱你一生 2024-12-10 12:23:01

这听起来有点糟糕的设计;有几种方法可以解决这个问题:

如果只有一个 switch 条件会破坏外部循环,那么只需在输入 switch 之前检查一下:

 if(n == 1)
    break;

 switch(n) { }

如果多个条件可以破坏循环,请重构为更 linq 风格的查询:

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

这应该调用 checkStatus 最多 50 次,直到遇到 1(或其他中断代码),然后不再继续评估元素。

This smells of bad design; there are a couple of ways you could fix this:

If only one condition of your switch would break the outer loop, then simply check for that before entering the switch:

 if(n == 1)
    break;

 switch(n) { }

If multiple conditions can break the loop, refactor to a more linq style query:

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

That should call checkStatus up to 50 times until it encounters a 1 (or other break codes), and then doesnt continue evaluating elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文