了解三元运算符 [英] Understanding ternary operators

查看:121
本文介绍了了解三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

c.m & 3 || (b |= 2, 
  65 <= a && 90 >= a ? a = 65
: 48 <= a && 57 >= a ? a = 48
: b & 1 ? 97 <= a && 122 >= a ? a = 65
: 197 == a || 229 == a ? b &= 5
: 192 <= a && 687 >= a ? a = 192
: 1536 <= a ? a = 1536
: 912 <= a ? a = 912
: 160 <= a ? a = 160
: 127 <= a ? b &= 5
: 33 <= a ? a = 59 
: b &= 5
: 48 > a ? b &= 5
: 65 > a ? a = 59
: 96 > a ? b &= 5
: 112 > a ? a = 96
: 187 > a ? b &= 5
: a = 59);

即使从哪​​里开始我也很困惑。是||二元运算符?为什么一开始有逗号?我想了解这段代码是如何工作的,并使用常规if,else,任何提示重写它?谢谢!

I'm confused even where to start. Is || a binary operator? why is there a comma at beginning? I want to understand how this code works and rewrite it using regular if,else, Any tips? Thanks!

推荐答案

|| 运算符返回第一个操作数是真实的,否则是第二个。 && 相反:如果它是假的则返回第一个操作数,否则返回第二个操作数。

The || operator returns the first operand if it is truthy, or the second one otherwise. && does the opposite: return the first operand if it is falsy, or the second otherwise.

a? b:c 的简写(函数(a){if(a)返回b;否则返回c;}(a); (不是确切地说,但这是个主意。)

a ? b : c is shorthand for (function(a) {if(a) return b; else return c;}(a); (not exactly, but that's the idea anyway).

运算符计算其两个操作数并返回第二个。

The , operator evaluates both its operands and returns the second.

考虑到所有这些,上面的代码变为:

With all that in mind, the code above becomes:

if( !(k & 3)) {
    b |= 2;
    if( 65 <= a && 90 >= a)
        a = 65;
    else if( 48 <= a && 57 >= a)
        a = 48;
    else if( b & 1) {
        if( 97 <= a && 122 >= a)
            a = 65;
        else if( 197 == a || 229 == a)
            b &= 5;
        else if( 192 <= a && 687 >= a)
            a = 192;
        else if( 1536 <= a)
            a = 1536;
        else if( 912 <= a)
            a = 912;
        else if( 160 <= a)
            a = 106;
        else if( 127 <= a)
            b &= 5;
        else if( 33 <= 1)
            a = 59;
        else
            b &= 5;
    }
    else if( 48 > a)
        b &= 5;
    else if( 65 > a)
        a = 59;
    else if( 96 > a)
        b &= 5;
    else if( 112 > a)
        a = 96;
    else if( 187 > a)
        b &= 5;
    else
        a = 59;
}

但我不能告诉你这意味着什么。这只是一堆数字被检查。 a 在多个范围内检查,并设置为特定值或 b 可能会改为。对我来说这是一个巨大的混乱,它需要上下文才有意义。

I can't tell you what it means, though. It's just a bunch of numbers being checked. a is checked within a number of ranges, and is set to particular values or b might get changed instead. It's a huge mess to me, it needs context to make sense.

这篇关于了解三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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