如何通过速记"if-else"打破循环.结果? [英] How to break a loop by a shorthand "if-else" result?

查看:75
本文介绍了如何通过速记"if-else"打破循环.结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在这种情况下,我在循环中有一个简写的if-else语句:

Suppose I have got a shorthand if-else statement inside a loop as in this case :

for(...)
{
    a = b == c ? b : c;
    // More unnecessary code if the result was true.
}

我想通过条件的结果来打破循环:

And I would like to break the loop by the result of the condition:

for(...)
{
    a = b == c ? b break : c;
    // Now the unnecessary code is not executed.
}

我意识到我可以像下面的示例一样以完整的方式键入它:

I realize I could just type it in a full way as in this example:

for(...)
{
    if( b == c )
    {
        a = b;
        break;
    }
    else
    {
        a = c;
    }
    // Now the unnecessary code is not executed.
}

但是它太长了,因为其中有多个中断条件,所以我试图将所有中断条件都放在一行中.

But it is too long and I am trying to have every breaking condition in a single line since there are several of them.

推荐答案

以下是有关三元运算符的一些信息,它的用法是: ?:运算符(C#)

Here is some information on the ternary operator and it's uses: ?: Operator (C#)

您要做的只是不可能的……这不是它的工作原理.

What you want to do is just impossible... it's not how it works.

这篇关于如何通过速记"if-else"打破循环.结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆