简体   繁体   English

如果没有 else 三元运算符

[英]If without else ternary operator

So far from I have been searching through the net, the statement always have if and else condition such as a ? b : c到目前为止,我一直在网上搜索,该语句始终具有 if 和 else 条件,例如a ? b : c a ? b : c . a ? b : c I would like to know whether the if ternary statement can be used without else .我想知道if三元语句是否可以在没有else情况下使用。 Assuming i have the following code, i wish to close the PreparedStatement if it is not null假设我有以下代码,如果它不为空,我希望关闭PreparedStatement

(I am using Java programming language.) (我正在使用 Java 编程语言。)

PreparedStatement pstmt;

//.... 

(pstmt!=null) ? pstmt.close : <do nothing>;

No, you cannot do that. 不,你不能那样做。 Instead try this:而是试试这个:

if(bool1 && bool2) voidFunc1();

Why using ternary operator when you have only one choice?当您只有一种选择时,为什么要使用三元运算符?

if (pstmt != null) pstmt.close(); 

is enough!够了!

Just write it out?直接写出来?

if(pstmt != null) pstmt.close();

It's the exact same length.这是完全相同的长度。

Ternary if operator is the particular ternary operator .三元if运算符是特定的三元运算符 One of a kind.一种。

From Wiki:来自维基:

In mathematics, a ternary operation is an n-ary operation with n = 3.在数学中,三元运算是 n = 3 的 n 元运算。

It means all 3 operands are required for you.这意味着您需要所有 3 个操作数。

As mentioned in the other answers, you can't use a ternary operator to do this.正如其他答案中所述,您不能使用三元运算符来执行此操作。

However, if the need strikes you, you can use Java 8 Optional and lambdas to put this kind of logic into a single statement:但是,如果需要,您可以使用 Java 8 Optional和 lambdas 将这种逻辑放入单个语句中:

Optional.of(pstmt).ifPresent((p) -> p.close())

A ternary operation is called ternary beacause it takes 3 arguments, if it takes 2 it is a binary operation.三元运算称为三元运算,因为它需要 3 个参数,如果它需要 2 个参数,则它是一个二元运算。

And as noted above, it is an expression returning a value.如上所述,它是一个返回值的表达式

If you omit the else you would have an undefined situation where the expression would not return a value.如果省略 else ,则会出现未定义的情况,即表达式不会返回值。

So as also noted in other answer, you should use an if statement .因此,正如其他答案中所述,您应该使用 if语句

You cannot use ternary without else, but to do a "if-without-else" in one line, you can use Java 8 Optional class.您不能在没有 else 的情况下使用三元,但要在一行中执行“if-without-else”,您可以使用 Java 8 Optional类。

PreparedStatement pstmt;

//.... 

Optional.ofNullable(pstmt).ifPresent(pstmt::close); // <- but IOException will still happen here. Handle it.

pstmt != null && pstmt.close;

上面的代码行转换为当表达式的左侧“转换”为 true -> 执行右侧。

Well in JavaScript you can simply do:在 JavaScript 中,您可以简单地执行以下操作:

expression ? doAction() : undefined

since that's what's literally actually happening in a real if statement, the else clause is simply undefined.因为那是真正的 if 语句中实际发生的事情,所以 else 子句只是未定义的。 I image you can do pretty much the same thing in (almost?) any programming language, for the else clause just put a null-type variable that doesn't return a value, it shouldn't cause any compile errors.我想象你可以在(几乎?)任何编程语言中做几乎相同的事情,因为 else 子句只是放置一个不返回值的空类型变量,它不应该导致任何编译错误。

or just make a function to return if all else fails或者只是创建一个函数来返回如果所有其他方法都失败

function oy(x1,x2){if(x1) return x2();}

oy(etzem==6, ()=>yichoyliss=8);

Yes you can do that actually (in JavaScript at least):是的,您实际上可以做到(至少在 JavaScript 中):

condition && x = true;

or (in JavaScript, and there might be a similar way to do this in Java):或(在 JavaScript 中,在 Java 中可能有类似的方法):

void(condition && x = true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM