Thread: "FAQ > How do I... (Level 2) > Work with dates and times" - Queries.

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    "FAQ > How do I... (Level 2) > Work with dates and times" - Queries.

    Dear experts,

    I was browsing through the FAQs when I stumbled on one portion:

    Code:
    int leap_year ( int year )
    {
      return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 );
    }
    I do not understand the expression. Can it be broken down to a few more lines so that newbies like me can understand? Please?

    Thanks.
    If I'm able to turn back time, I would learn C as my first language.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    int leap_year(int year)
    {
     	if(year % 4 ==0)
    	 {
    	  		if(year % 100 != 0 || year % 400 == 0)
    	  		return true;
         }
    }
    ssharish2005

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    if(year % 4 ==0)

    ...This means if 4 percent of the variable year equals zero.
    I hope you know what Percent means:-


    Code:
    4/100 * year
    if(year % 100 != 0 || year % 400 == 0)

    ...And this means if 100 percent of year does not equal zero or 400 percent equals zero return true.

    I hope this is helpful



    Ps. I'm lying

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    That's not very nice. I'm gonna get Webmaster and have you band.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by treenef
    if(year % 4 ==0)

    ...This means if 4 percent of the variable year equals zero.
    I hope you know what Percent means:-


    Code:
    4/100 * year
    if(year % 100 != 0 || year % 400 == 0)

    ...And this means if 100 percent of year does not equal zero or 400 percent equals zero return true.

    I hope this is helpful



    Ps. I'm lying
    EDIT: What are you trying to say?
    Last edited by stevong; 12-03-2005 at 10:39 AM.
    If I'm able to turn back time, I would learn C as my first language.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by ssharish2005
    Code:
    int leap_year(int year)
    {
     	if(year % 4 ==0)
    	 {
    	  		if(year % 100 != 0 || year % 400 == 0)
    	  		return true;
         }
    }
    ssharish2005
    Thanks. That's sweet.
    If I'm able to turn back time, I would learn C as my first language.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by stevong
    Why?
    I seriously seriously hope you saw the whited out text in Treenef's post... and I was talking to him, by the way, and it was a joke.
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by SlyMaelstrom
    I seriously seriously hope you saw the whited out text in Treenef's post... and I was talking to him, by the way, and it was a joke.
    Haha. yea. I didnt read his post clearly.

    I almost fell off my chair when I read his post all over again.
    If I'm able to turn back time, I would learn C as my first language.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    So... you do understand what the modulus operator is, right?
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Quote Originally Posted by SlyMaelstrom
    So... you do understand what the modulus operator is, right?
    Yes I do.

    The intention of this thread is to hope that someone can break down that single line of code into simpler statements in which layman/newbies can understand.

    Thanks to ssharish2005.

    Code:
    return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 );
    Code:
     	if(year % 4 ==0)
    	 {
    	  		if(year % 100 != 0 || year % 400 == 0)
    	  		return true;
         }
    If I'm able to turn back time, I would learn C as my first language.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by stevong
    Code:
    int leap_year ( int year )
    {
      return year % 4 == 0 && ( year % 100 != 0 || year % 400 == 0 );
    }
    I do not understand the expression. Can it be broken down to a few more lines so that newbies like me can understand? Please?
    Return true if the year is evenly divisible by 4 and {the year is not evenly divisible by 100 or it is evenly divisible by 400}; otherwise return false.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed