标签: conditional-operator

PHP中的?:运算符('Elvis运算符')

我今天在一些PHP代码中看到了这个:

$items = $items ?: $this->_handle->result('next', $this->_result, $this);
Run Code Online (Sandbox Code Playgroud)

我不熟悉?:这里使用的运算符.它看起来像一个三元运算符,但是省略了判断谓词是否为真的表达式.这是什么意思?

php coding-style language-construct conditional-operator

240
推荐指数
5
解决办法
11万
查看次数

JavaScript中的问号和冒号

我遇到了以下几行

hsb.s = max != 0 ? 255 * delta / max : 0;
Run Code Online (Sandbox Code Playgroud)

什么的?,并:在这方面是什么意思?

javascript syntax operators conditional-operator

239
推荐指数
4
解决办法
19万
查看次数

返回类型'?:'(三元条件运算符)

为什么第一个返回引用?

int x = 1;
int y = 2;
(x > y ? x : y) = 100;
Run Code Online (Sandbox Code Playgroud)

而第二个不?

int x = 1;
long y = 2;
(x > y ? x : y) = 100;
Run Code Online (Sandbox Code Playgroud)

实际上,第二个根本没有编译 - "没有左边的赋值".

c++ types reference conditional-operator lvalue

207
推荐指数
3
解决办法
2万
查看次数

将null返回为允许使用三元运算符的int,而不是if语句

让我们看看以下代码段中的简单Java代码:

public class Main {

    private int temp() {
        return true ? null : 0;
        // No compiler error - the compiler allows a return value of null
        // in a method signature that returns an int.
    }

    private int same() {
        if (true) {
            return null;
            // The same is not possible with if,
            // and causes a compile-time error - incompatible types.
        } else {
            return 0;
        }
    }

    public static void main(String[] args) {
        Main m = …
Run Code Online (Sandbox Code Playgroud)

java autoboxing nullpointerexception conditional-operator

185
推荐指数
5
解决办法
2万
查看次数

PowerShell中的三元运算符

据我所知,PowerShell似乎没有所谓的三元运算符的内置表达式.

例如,在支持三元运算符的C语言中,我可以编写如下内容:

<condition> ? <condition-is-true> : <condition-is-false>;
Run Code Online (Sandbox Code Playgroud)

如果在PowerShell中确实不存在,那么实现相同结果的最佳方法(即易于阅读和维护)是什么?

powershell ternary-operator conditional-operator

182
推荐指数
11
解决办法
9万
查看次数

要三元还是不三元?

我个人是三元运算符的拥护者:()?:; 我确实意识到它有它的位置,但我遇到了许多完全反对使用它的程序员,而且有些人经常使用它.

你有什么感受?你看到了什么有趣的代码?

multilingual ternary-operator conditional-operator

180
推荐指数
17
解决办法
4万
查看次数

如何检查我的字符串是否等于null?

我想只在我的字符串有一个有意义的值时执行一些操作.所以,我试过这个.

if (!myString.equals("")) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)

还有这个

if (!myString.equals(null)) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)

还有这个

if ( (!myString.equals("")) && (!myString.equals(null))) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)

还有这个

if ( (!myString.equals("")) && (myString!=null)) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)

还有这个

if ( myString.length()>0) {
doSomething
}
Run Code Online (Sandbox Code Playgroud)

在所有情况下,我的程序doSomething尽管我的字符串是空的.它等于null.那么,那有什么问题?

添加:

我找到了问题的原因.该变量被声明为一个字符串,因此,null分配给该变量的变换为"null"!所以,if (!myString.equals("null"))有效.

java string null equals conditional-operator

166
推荐指数
11
解决办法
47万
查看次数

使用Null Coalescing运算符的独特方法

我知道在C#中使用Null合并运算符的标准方法是设置默认值.

string nobody = null;
string somebody = "Bob Saget";
string anybody = "";

anybody = nobody   ?? "Mr. T"; // returns Mr. T
anybody = somebody ?? "Mr. T"; // returns "Bob Saget"
Run Code Online (Sandbox Code Playgroud)

但还有什么可以??用于?它不像三元运算符那样有用,除了比以下更简洁和更容易阅读:

nobody = null;
anybody = nobody == null ? "Bob Saget" : nobody; // returns Bob Saget
Run Code Online (Sandbox Code Playgroud)

所以考虑到甚至更少知道空合并运算符......

  • 你有没有用过??别的东西?

  • ??必要的,还是应该只使用三元运算符(大多数人都熟悉)

c# null coding-style conditional-operator null-coalescing-operator

157
推荐指数
11
解决办法
6万
查看次数

Nullable类型问题?:条件运算符

有人可以解释为什么这在C#.NET 2.0中有效:

    Nullable<DateTime> foo;
    if (true)
        foo = null;
    else
        foo = new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

......但这不是:

    Nullable<DateTime> foo;
    foo = true ? null : new DateTime(0);
Run Code Online (Sandbox Code Playgroud)

后一种形式给我一个编译错误"无法确定条件表达式的类型,因为'<null>'和'System.DateTime'之间没有隐式转换."

并不是说我不能使用前者,但第二种风格与我的其余代码更加一致.

c# generics nullable conditional-operator

154
推荐指数
3
解决办法
6万
查看次数

单行列表理解:if-else变体

它更多的是关于python列表理解语法.我有一个列表理解,可以生成给定范围的奇数列表:

[x for x in range(1, 10) if x % 2]
Run Code Online (Sandbox Code Playgroud)

这是一个过滤器 - 我有一个源列表,我删除偶数(if x % 2).我想在这里使用if-then-else之类的东西.以下代码失败:

>>> [x for x in range(1, 10) if x % 2 else x * 100]
  File "<stdin>", line 1
    [x for x in range(1, 10) if x % 2 else x * 100]
                                         ^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)

有一个像if-else的python表达式:

1 if 0 is 0 else 3
Run Code Online (Sandbox Code Playgroud)

如何在列表理解中使用它?

python list-comprehension ternary-operator conditional-operator

150
推荐指数
4
解决办法
25万
查看次数