Benefits of using the conditional ?: (ternary) operator

后端 未结 17 1030
孤街浪徒
孤街浪徒 2020-11-22 06:55

What are the benefits and drawbacks of the ?: operator as opposed to the standard if-else statement. The obvious ones being:

Conditional ?: Operator

17条回答
  •  再見小時候
    2020-11-22 07:35

    One thing to recognize when using the ternary operator that it is an expression not a statement.

    In functional languages like scheme the distinction doesn't exists:

    (if (> a b) a b)

    Conditional ?: Operator "Doesn't seem to be as flexible as the if/else construct"

    In functional languages it is.

    When programming in imperative languages I apply the ternary operator in situations where I typically would use expressions (assignment, conditional statements, etc).

提交回复
热议问题