Why can't I use break in a C# ternary expression?

允我心安 提交于 2019-12-02 13:38:52

It isn't possible using the ternary operator, but you can simplify your code structure as follows:

string input;
do {
    Console.WriteLine("Enter 3 words seperated by spaces: ");
    input = Console.ReadLine();
    if (input != "") {
        ConvertToPascal(input);
    }
} while(input != "");
B.M.

Because the ternary is not a shorter way to write an if-else structure, it's a short way to write an expression that picks one of two values based on some condition. break is a flow-control statement, not a value.

If it helps, think of:

someVar = cond ? a : b;

as of:

someVar = getValue(cond);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!