Results 1 to 4 of 4

Thread: loops

  1. #1
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default loops

    Hello,

    I saw some loops examples that uses something like

    while (i%5 ==0)....

    what is the % for or mean? I am new to java.

    thanks

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It's the modulus operator, which finds the remainder of a number when divided out into the second number.

    So let's say you start with this: 7%2. To determine its value, you take 7, and subtract 2 until you cannot anymore. Like so:
    7 - 2 = 5
    5 - 2 = 3
    3 - 2 = 1
    (Can't subtract 2 anymore, since 1 - 2 < 0)
    Therefore, 7%2 == 1. You'll also notice if you do long division for 7 divided by 2, the remainder will be 1. This holds true for any modulus operation.

    In your case, basically it's checking to see if "i" is divisible by 5. That meas 5, 10, 15, ..... etc.

    For more information:
    Modulo operation - Wikipedia, the free encyclopedia
    The Remainder or Modulus Operator in Java
    modulus computing - Google Search

    Good luck!

  3. #3
    curioustoknow is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default

    so the following example case2 is just checking if the year is divisible by 4,100 and 400? I dont understand why are we doing this?


    for (int m=1; m<=12; m++)
    {
    String monthN="";
    int numD=0;
    switch (m)
    {
    case 1:
    monthN="January ";
    numD=31;
    break;
    case 2:
    monthN="February ";
    if ((yr%4 == 0 && yr%100 != 0) || yr%400 == 0 )
    numD=29;
    else numD=28;
    break;
    case 3:
    monthN="March ";
    numD=31;
    break;

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by curioustoknow View Post
    so the following example case2 is just checking if the year is divisible by 4,100 and 400? I dont understand why are we doing this?

    with if ((yr%4 == 0 && yr%100 != 0) || yr%400 == 0 ) the code is calculating if the year is a leap year and if true the number of days in the month FEBRUARY is 29.
    Last edited by j2me64; 02-06-2011 at 02:09 PM.

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM
  3. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  5. how to use do while loops
    By mikeitalydz in forum New To Java
    Replies: 32
    Last Post: 09-26-2009, 08:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •