Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Have been asked to remove return statements from my loop and it is difficult to do that and still get the same output i want.

int[] a = getFreePositionToFit(product);
       if (a == null ) {
           return false;
       }
       if (maxWeight < product[Data.WT]) {
           return false;
       }
       for (int i = a[0]; i < a[0] + product[Data.WID]; i++) {
           for (int j = a[1]; j < a[1] + product[Data.LEN]; j++) {
               BOX[i][j] = product[Data.ID];
           }
       }
       maxWeight -= product[Data.WT];
       return true;
   }


What I have tried:

i have tried to declare a boolean variable and use that.

Boolean result= true;
Posted
Updated 29-May-17 3:10am
Comments
[no name] 29-May-17 8:53am    
"boolean variable and use that."... AND?

1 solution

You are right to use a boolean variable, next is to use if else if, e.g.
boolean result = true;
if (condition 1) {
  result = false;
} else if (condition 2) {
  result =  false;
} else { // none of the above
  // do something
}
return result;

You can also combine conditions 1 and 2 with a || operator.
 
Share this answer
 
v3
Comments
CPallini 30-May-17 3:10am    
5.
Peter Leow 30-May-17 8:12am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900