单行if语句,如何转换这个if-else语句 [英] One-liner if statements, how to convert this if-else-statement

查看:191
本文介绍了单行if语句,如何转换这个if-else语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的总菜鸟要温柔。我到处寻找,似乎无法找到答案。如何压缩以下内容?

Total noob here so be gentle. I've looked everywhere and can't seem to find the answer to this. How do I condense the following?

if (expression)
{
    return true;
}
else
{
    return false;
}

我无法让它工作,因为它返回的东西与设置的东西。我已经看过这样的事情了:

I can't get it to work since it's returning something vs. setting something. I've already seen things like this:

somevar = (expression) ? value1 : value2;

就像我说的那样,请温柔:)

Like I said, please be gentle :)

推荐答案

return (expression) ? value1 : value2;

如果 value1 value2 实际上是 true false 就像在你的例子中一样,你可能也只是

If value1 and value2 are actually true and false like in your example, you may as well just

return expression;

这篇关于单行if语句,如何转换这个if-else语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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