# problem with if statements (leap year)

• 03-30-2007
problem with if statements (leap year)
how would you write an if statement to determine whether or not it is a leap year. A year is a leap year if it is evenly divisible by four hundred or if it is evenly divisible by four, but not by one hundred

i tried to start it like so
Code:

`if (((((year/400)%2)== 0)||((((year/4)%2)== 0)&&(((year/100)%2)!= 0)))&&((month==2)&&(day==29)))`
• 03-30-2007
The Brain
Code:

```//if year is evenly divisible by 400 OR year is evenly divisible by 4 AND year is not evenly divisible by 100 if(year&#37;400 == 0 || year%4 == 0 && !(year%100 == 0))```
• 03-30-2007
robatino
Quote:

Originally Posted by The Brain
Code:

```//if year is evenly divisible by 400 OR year is evenly divisible by 4 AND year is not evenly divisible by 100 if(year&#37;400 == 0 || year%4 == 0 && !(year%100 == 0))```

You might want to write this as
Code:

`if (year%400 == 0 || (year%4 == 0 && year%100 != 0))`
since not everyone knows off the top of their head that && has higher precedence than || (I had to look it up).

Edit: If efficiency matters, you can also rewrite it as
Code:

`if ((year%4 == 0 && year%100 != 0) || year%400 == 0)`
since the first condition is much more likely to be true, allowing the || shortcutting to skip the second condition. The two conditions for && should be left as is, since the first one is much more likely to be false, allowing the && shortcutting to skip the second one.
• 03-30-2007
thnx. i think i was thinking too hard
• 03-30-2007
The Brain
Quote: