如果语句执行所有条件 [英] If statement executing all conditions

查看:161
本文介绍了如果语句执行所有条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能实际上是一个愚蠢的问题,但我想知道是否有可能有if语句执行所有条件。我解释一下:

This may actually be a silly question but I am wondering if it is possible to have an if statement executing all conditions. I explain:

if (methodA() && methodB() && methodC()) {code}

代码真正。关键是当方法返回false时,不执行其余方法。这对于性能来说通常是一件好事,但是如果我真的需要独立于返回的方式执行所有方法,那么在评估表达式后进入if或者if。同样适用于OR或其他什么

code is executed when all three methods return true. The point is that when a method returns false, the rest of the methods aren't executed. This is normally a good thing for performance but what if I really need to execute all methods independently of what they are returning and after that evaluate the expression and go into the if or not. The same is applied for OR or whatever

有没有办法告诉java这样做?
我目前的工作是将它分成三个ifs,但这并不总是有效,看起来真的很难看。

Is there a way to tell java to behave that way? My current work around is to split it in three ifs but this not always does the trick and looks really ugly.

推荐答案

这很简单:使用& 运算符代替&&

That’s quite simple: use the & operator instead of &&.

来自: http:/ /docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.23


条件和算子&&就像& (§15.22.2),但仅当其左侧操作数的值为真时才计算其右侧操作数。

The conditional-and operator && is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true.

这篇关于如果语句执行所有条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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