How avoid multiple IF loops in Java
This question already has an answer here:
Use &&
. &&
is logical and. &&
combines two values and returns a boolean which is true if and only if both of its operands are true
if(address!=null && firstName!=null && lastName!=null)
{
}
For instance
boolean b;
b = 3 > 2 && 5 < 7; // b is true
b = 2 > 3 && 5 < 7; // b is now false
if loop
is a wrong word. You should say if statements
As in you case you can use OR (||) or AND (&&)statement like this
if(address!=null && firstName!=null && lastName!=null)
{
}
Try AND(&&)
if you want to pass all checks or intead of nested if statements and try OR(||)
for non nested like else if
or simply say if you want to pass anyone of your condition
But if all of these are Strings then you should try like this
"yourValue".equals(stringValue)
This will skip the null check.
Use and operator (&&)
if(address!=null && firstName!=null && lastName!=null)
{
//DoSomething here
}
And I suggest you to see Short circuit evaluation
链接地址: http://www.djcxy.com/p/13294.html下一篇: 如何避免Java中的多个IF循环