1) I'm not quite sure what you mean by "set one of the if statement conditions". But if you mean that the b
-related test evaluates to true while the others still relate to false, then the behaviour is intended. You're using a logical AND, &&
, to join your conditions. Which means that the overall condition is true if and only if all of the individual conditions are true.
If you want the first block to be executed when any of the individual conditions is true, then you use should use an OR (||
) instead.
2) An if
statement always requires a single boolean condition - which in this case you've formed from a conjunction of 150 individual conditions. If you need to inspect 150 fields to reach the overall decision, then somehow these will all need to be accessed.
However, you might be able to make it easier on yourself. For one thing, if the a
and the b
etc., are part of an object, then you can provide a nice public method that describes what the condition is, and hides the nasty details away (e.g. public boolean isReadyToExecute()
).
Also, if you have 150 variables, chances are they're all instances of the same sort of thing. Say, for example, you're tracking 150 files to see whether they've been generated. Instead of having variables like:
private boolean fileReady1;private boolean fileReady2;// ...private boolean fileReady150;
You can use an array (or some other collection) instead:
private boolean[] filesReady = new boolean[150];
Now you don't have to spell out every variable by hand, in order to come up with your boolean condition:
public boolean isReadyToLoad() { for (int i = 0; i < filesReady.length; i++) { if (!filesReady[i]) { // Found a file that wasn't ready, so we're not ready overall return false; } } // Got through all the files without finding an unready one return true;}
The exact approach will depend on the nature of your problem. But I reckon that some combination of information hiding (i.e. wrapping things in nice methods) and loops will cover anything you need to do.
(And don't forget that wrapping things in methods can be nested. Perhaps the first 20 fields represent one set of things, the next 30 represent another, etc. Then you could wrap each of these in a method, and implement isReadyToLoad()
as something like allFilesLoaded() && status.isSignedOff() && userDao.allUsersHavePasswords()
...)