记住三元运算符语法 [英] Remembering the Ternary Operator Syntax

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

问题描述

有人记得标准的三元语法吗?

Anyone have a good trick to remember the standard ternary syntax?

具体来说,?或:排在最前面。这些年来,我一直在退缩。

Specifically whether the '?' or ':' comes first. I have consistently gotten this backwards over the years.

推荐答案

您要检查的条件有点像一个问题,所以这个问题标记优先。

The condition you are checking is kind of like a question, so the question mark comes first.

x > 0 ? 1 : 0

将此语句视为三个英语句子: x是否大于0?然后1否则,0。语句中的每个子句都有一个句子。

Think of this statement as three English sentences: "Is x greater than 0? Then 1. Else, 0." You have one sentence for each clause in the statement.

谓词:

x > 0 ? /* Is x greater than 0? */

真实分支:

1 /* Then 1. */

false分支:

: 0 /* Else, 0. */

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

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