Using nested if()… else… statements
Sometimes, we can make the logic of if()… else…
statements clearer by nesting if()… else…
statements within either one or both clauses of the if()… else…
statements.
In our isLeap()
example, someone new to the intricacies of Gregorian calendar development and the subtleties of a century leap year calculation might have to pause and wonder a bit about our if/else fall-through logic. Actually, this case is pretty simple; much more tangled examples of such logic can be found. Nonetheless, we can make our logic a bit clearer by nesting an if()… else
…
statement within one of the if()… else…
clauses.
Copy leapYear2.c
to leapYear3.c
, which we will now modify. Our isLeap()
function now looks like this:
bool isLeapYear( int year ) {
bool isLeap = false;
// Gregorian calendar leap year calculation.
...