Skip if conditions when the flag is false

I have several independent if conditions, and in each condition I will evaluate the value of the boolean variable to both true and false.

if the value of boolean variable gets false in the first if condition, then how can I skip the rest of the 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 condition if () is satisfied, is it possible to return the value. I know that in loops we can use the continue keyword to skip the rest of the loop. How can we achieve something like this here.

Update:

i does not mean exactly in the first condition if (), if any of the conditions if () is satisfied, then the best way to skip the remaining conditions, such as continue, is executed in a loop

+4
source share
11 answers
 return fruit.weight <= 2 && "red".equals(fruit.color) && "sweet".equals(fruit.taste) && fruit.isPerishable; 
+27
source

For a general solution, you can use else if :

 if(fruit.weight > 2){ isRecommended = false; } else if(!"red".equals(fruit.color)){ //etc... } 

But in your specific example, you can simply use logical logic:

 return !( fruit.weight > 2 || !"red".equals(fruit.color) || !"sweet".equals(fruit.taste) || !fruit.isPerishable ); 

You can use your IDE to refactor the logic of this expression using the laws of De Morgan . Most decent IDEs can do this for you with a few keystrokes.

+9
source

You can condense it this way using several return statements that will be returned early and skip the rest. For added brevity, I also removed the unnecessary 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; } 
+8
source

if the first condition if () is satisfied, is it possible to return a value.

Is that not so?

 if(fruit.weight > 2){ return false; // etc. } 

Or I do not understand your question?

+6
source

Tests will be stopped at the first not checked.

 private static boolean isRecommended(Fruit fruit) { return fruit.weight <= 2 && "red".equals(fruit.color) && "sweet".equals(fruit.taste) && fruit.isPerishable ; } 
+5
source

You can just write

 return isRecommended; 

inside your ifs. return can be used several times.

+3
source

Just put return false in your first conditional.

0
source

The simple answer is to return in if blocks, not when setting a value. However, this is not entirely scalable, and returning inside blocks increases code complexity.

A complex but more flexible answer is to create an interface that allows user logic to be implemented.

 interface RecommendationFilter<T> { boolean recommend(T item); } 

And then, in some implementation, you can use a bunch of loops loaded into the general RecommendationFilter .

 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 quite well, and it allows for some pretty interesting implementations.

0
source

Wrap the remaining if in an else :

 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; } 

ps I highly recommend using spaces rather than tabs for indentation. Tabs are often not transferred to other editors or environments that they want.

0
source

I found another solution, i.e. using shortcut 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; } 
0
source
  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; } 
0
source

Source: https://habr.com/ru/post/1438074/


All Articles