I have following code
if ( !handleOptions(args) ) {
return false;
}
if ( !initConfig() ) {
logger.error("Error initializing configuration. Terminating");
return false;
}
And the code itself is self explaining until I noticed that there are no else statements and yet, methods handleOptions and initConfig are called and executed. How does that work? From what I know, the parameters of if clause (in this case) are either determined true and then exception is thrown, or, they are false in which case I would expect else, yet I dont see one and code is still executed.
The function is called first, then its return value is tested to determine whether to go into the body of the block.
Another way to think of it is that this:
if ( !handleOptions(args) ) {
return false;
}
is exactly like this, but without the variable:
boolean result = handleOptions(args);
if ( !result ) {
return false;
}
If you think about it, it has to be that way; we can't know whether the result of calling the function matches a given condition without calling the function and getting its result.
the parameters of if clause (in this case) are either determined true and then exception is thrown
if
doesn't throw exceptions. (The expression being tested might, but if
itself doesn't.)
..., or, they are false in which case I would expect else, yet I dont see one and code is still executed.
else
is optional. If there isn't one, and the condition is false, it's just that nothing happens. :-)