You could just follow your logic to the bitter end.
Starting with what you wrote, pseudo-code for the algorithm could look something like the following:
Code :
IF (year IS DIVISIBLE BY 400) THEN
It is a leap year. // Divisible by 400 is all you need to know!
ELSE IF (year IS DIVISIBLE BY 4) THEN
IF (year IS DIVISIBLE BY 100)) THEN
It is not a leap year. // Not divisible by 400. Divisible by 100.
ELSE
It is a leap year. // Divisible by 4. Not divisible by 100
END IF
ELSE
It is not a leap year. // Not divisible by 4.
END IF
Of course there are other sequences of tests that also give the correct answer. (For example: Test for divisibility by 400, then by 100, then by 4, stopping wherever appropriate.)
More experienced programmers are always tempted to compress things to one or two compound conditionals instead of using nested conditionals. Sometimes that makes things more clear, but sometimes cleverness and "elegant" terseness makes it more difficult to understand. You should lay out the logic steps in a way that is "obviously correct" from your own point of view.
Notes:
Why the heck do you use a double precision floating point variable for something that, by its very nature,an integer? In Java you might get the correct output with floating point variables, but, in my opinion, it's just wrong-headed. There are situations where it is appropriate to use floating point. arithmetic This is simply not one of those.
Why the heck do you use numerical variables for the result of tests of divisibility? I mean, you might be able to get the correct answer, but it's just an indication of muddy thinking (perhaps influenced by previous experience with C or some other language that doesn't have boolean data types, or the effect of getting help from someone like that).
As far as that goes, why even have variables for the results of divisibility tests at all? Wherever possible I think that statements should be self explanatory and not make a program maintainer (or person trying to debug the program) have to refer back to some previous assignment statement to see what condition is being tested:
Code java:
if ((year % 400) == 0) {
// Whatever...
}
else if ((year % 4) == 0) {
// Whatever
}
Bottom line: Get into the habit of doing whatever you think you can do to make it "correct at a glance." You will thank me later.
Cheers!
Z