• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

1.3 Leap

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone.
My code for this assignment goes something like this:
1) boolean leap = false ;
2) if ( (year % 400) == 0 )leap = true
3) else if ( year % 100 == 0 ) leap = false
4) else if ( year % 4 == 0 ) leap = true
The objection I received was that on line 2 "isn't leap already false? " because it was initialized false. But I need to have this line to get the logic to quit the block in the case of year % 4 = 100 (and skip line 4). Is there a better way to breakout of an if block? Sorry if I don't respond for awhile, I'm taking inventory at work.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In English your statements read:
If (year is evenly divisible by 400), leap = true
Otherwise if (year is not evenly divisible by 400, but is evenly divisible by 100), leap is false (but you never changed it because the first if statement was false or else you wouldn't be in this statement)
Otherwise if (year is not evenly divisible by 400 and year is not evenly divisible by 100, but is evenly divisible by 4), leap is true.
else if statements are mutually exclusive.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic