Reputation: 2436
I have several independent if conditions and in each condition i will evaluate a boolean variable value either true or false.
if the boolean variable value gets false in the first if condition then how can i skip the rest of all conditions.
private static boolean isRecommended(Fruit fruit) {
boolean isRecommended = true;
if(fruit.weight > 2){
isRecommended = false;
}
if(!"red".equals(fruit.color)){
isRecommended = false;
}
if(!"sweet".equals(fruit.taste)){
isRecommended = false;
}
if(!fruit.isPerishable){
isRecommended = false;
}
return isRecommended;
}
if the first if() condition is executed then is it possible to return the value. I know in the loops we can use continue
keyword to skip the remainder of the loop execution. How can we achieve something similar here.
Update:
i do not mean exactly on the first if() condition, if any of the if() condition is executed then what is best way of skipping the rest of the conditions like continue does in loop
Upvotes: 4
Views: 11500
Reputation: 3
private static boolean isRecommended(Fruit fruit) {
while(true)
{
boolean isRecommended = true;
if(fruit.weight > 2){
isRecommended = false;
break;
}
if(!"red".equals(fruit.color)){
isRecommended = false;
break;
}
if(!"sweet".equals(fruit.taste)){
isRecommended = false;
break;
}
if(!fruit.isPerishable){
isRecommended = false;
break;
}
}
return isRecommended;
}
Upvotes: 0
Reputation: 2436
I have found another solution, that is the usage of label blocks with break statement. Below is the code
private static boolean isRecommended(Fruit fruit) {
boolean isRecommended = true;
labelA:
{
if(fruit.weight > 2){
isRecommended = false;
break labelA;
}
if(!"red".equals(fruit.color)){
isRecommended = false;
break labelA;
}
if(!"sweet".equals(fruit.taste)){
isRecommended = false;
break labelA;
}
if(!fruit.isPerishable){
isRecommended = false;
}
}
return isRecommended;
}
Upvotes: 0
Reputation: 83567
Wrap the rest of the if
statements in an else
statement:
private static boolean isRecommended(Fruit fruit) {
boolean isRecommended = true;
if(fruit.weight > 2){
isRecommended = false;
} else {
if(!"red".equals(fruit.color)){
isRecommended = false;
}
if(!"sweet".equals(fruit.taste)){
isRecommended = false;
}
if(!fruit.isPerishable){
isRecommended = false;
}
}
return isRecommended;
}
p.s. I highly recommend using spaces rather than tabs for indenting. Tabs often do not port to other editors or environments they way you want.
Upvotes: 0
Reputation: 838994
For a general solution you could use else if
:
if(fruit.weight > 2){
isRecommended = false;
}
else if(!"red".equals(fruit.color)){
//etc...
}
But in your specific example you can just use boolean logic:
return !(
fruit.weight > 2 ||
!"red".equals(fruit.color) ||
!"sweet".equals(fruit.taste) ||
!fruit.isPerishable
);
You could use your IDE to refactor the logic of this expression by applying De Morgan's laws. Most decent IDEs can do this for you with a few keystrokes.
Upvotes: 9
Reputation: 19185
return fruit.weight <= 2
&& "red".equals(fruit.color)
&& "sweet".equals(fruit.taste)
&& fruit.isPerishable;
Upvotes: 28
Reputation: 22332
The simple answer is to return
within the if
blocks, rather than setting a value. However, that's not exactly scalable, and return
ing within the blocks increases code complexity.
The complicated, but more flexible answer, is to create an interface
that allows you to implement custom logic.
interface RecommendationFilter<T>
{
boolean recommend(T item);
}
And then, in some implementation, one can use a bunch of generically loaded RecommendationFilter
s to loop through.
class FruitChecker
{
private final Set<RecommendationFilter<Fruit>> filters = ...;
public boolean isRecommended(Fruit fruit)
{
boolean recommended = true;
for (RecommendationFilter<Fruit> filter : filters)
{
if ( ! filter.recommend(fruit))
{
recommended = false;
break;
}
}
return recommended;
}
}
This idea scales up pretty well, and it enables some pretty interesting implementations.
Upvotes: 0
Reputation: 382434
The tests will stop at the first not verified.
private static boolean isRecommended(Fruit fruit) {
return
fruit.weight <= 2
&& "red".equals(fruit.color)
&& "sweet".equals(fruit.taste)
&& fruit.isPerishable
;
}
Upvotes: 5
Reputation: 579
You can condense it like this, by using multiple return statements that will return early and skip the rest. For added brevity I also removed the unneeded curly braces.
private static boolean isRecommended(Fruit fruit) {
if(fruit.weight > 2)
return false;
if(!"red".equals(fruit.color))
return false;
if(!"sweet".equals(fruit.taste))
return false;
if(!fruit.isPerishable)
return false;
return true;
}
Upvotes: 8
Reputation: 7837
if the first if() condition is executed then is it possible to return the value.
Isn't that just it?
if(fruit.weight > 2){
return false; // etc.
}
Or am I misunderstanding your question?
Upvotes: 6
Reputation: 650
You can just write
return isRecommended;
inside your ifs. return can be used multiple times.
Upvotes: 3