What does this line of code mean?

后端 未结 7 1110
抹茶落季
抹茶落季 2021-01-21 04:39

I am wondering what this line of code mean?

b = (gen_rand_uniform()>0.5)?1:0;

The gren_rand_uniform() is a function to generate

7条回答
  •  忘掉有多难
    2021-01-21 05:09

    What you are seeing here is a ternary expression. http://en.wikipedia.org/wiki/Ternary_operation This is (as others here has pointed out) a conditional construct, but one that is specific to expressions, meaning that a value is returned.

    This construct exist in most languages (but not in eg. VB.Net) and has the form of

    condition ? valueiftrue: valueiffalse
    

    An example of this in action is:

    var foo = true;
    var bar = foo ? 'foo is true' : 'foo is false';
    // bar = 'foo is true'
    

    Also note that the condition can be any expression (like in your case gen_rand_uniform() > 0.5) and can infact contain a nested ternary expression, all it has to do is evaluate as a non-false value.

提交回复
热议问题