Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By Tolls
  • 2 Post By pbrockway2

Thread: help explaining small loop

  1. #1
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default help explaining small loop

    public static void main (String[] args)
    {
    int num = 1, max = 20;

    while (num < max)
    {
    if (num%2 == 0 )
    System.out.println (num);
    num++;
    }

    }

    this is the output when i run it :

    2
    4
    6
    8
    10
    12
    14
    16
    18

    how is the output in a +2 pattern?

    also, I'm a little confused by (num%2 == 0 ) loop. To start off, it would read ( 1%2 == 0 ). How is the inner loop executed considering the math? 1 / 2 = 0.5 . Isn't the decimal portion(.5) the remainder and thus .5 =/= 0??
    Last edited by derb2k2; 02-12-2012 at 07:29 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: help explaining small loop

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default Re: help explaining small loop


  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: help explaining small loop

    So, everything clear now?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    XeroXen is offline Member
    Join Date
    Feb 2012
    Posts
    1
    Rep Power
    0

    Default Re: help explaining small loop

    The result of the output comes from your if statement, you are only printing numbers where the num variable % 2 == 0.

    The % sign represents modulus division, which returns the REMAINDER of the division problem (essentially, you do division to the nearest whole number, and you return the remainder). So, 3 % 5 would return 2, because 3 / 5 in integer division returns 1, and there is a remainder of 2.

    In this case, you are doing modulus division by two in order to determine if the number is a divisor of 2, in which case the remainder would be 0. Therefore, any number that is divisible evenly by two is printed, and the other numbers are not.

    I hope that helped.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: help explaining small loop

    Quote Originally Posted by derb2k2 View Post
    also, I'm a little confused by (num%2 == 0 ) loop. To start off, it would read ( 1%2 == 0 ). How is the inner loop executed considering the math? 1 / 2 = 0.5 .
    It's not a loop.
    It's an 'if' statement.
    Quote Originally Posted by derb2k2 View Post
    Isn't the decimal portion(.5) the remainder and thus .5 =/= 0??
    Exactly, which in an 'if' statement would mean...?
    derb2k2 likes this.

  7. #7
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default Re: help explaining small loop

    thank you Xero and Tolls. I'm looking into it now as I've been super busy.

  8. #8
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default Re: help explaining small loop

    ahah. Man, I'm so dumb. I see it now. I made a mistake in reading the code if that's possible with this small fragment of code.


    if (num%2 == 0 )
    System.out.println (num);
    num++;


    question: the "num++" operation is executed regardless of the (num%2 == 0) condition right?



    thank you guys for the help

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: help explaining small loop

    Yes the increment occurs whether or not the print took place.

    For this reason many people (ie me and all others I consider to be right thinking) use braces: even with one line blocks.

    Java Code:
    if(num % 2 == 0) {
        System.out.println(num);
    }
    num++;
    DarrylBurke and derb2k2 like this.

  10. #10
    derb2k2 is offline Noob
    Join Date
    Jul 2011
    Location
    M-I-yayo
    Posts
    11
    Rep Power
    0

    Default Re: help explaining small loop

    repped. you guys rock.


    hopefully i can start getting a good grip on java.

Similar Threads

  1. Replies: 15
    Last Post: 03-08-2011, 07:03 PM
  2. Explaining Exceptions
    By sunde887 in forum New To Java
    Replies: 5
    Last Post: 01-22-2011, 08:00 AM
  3. Help explaining the difference.
    By Romally in forum New To Java
    Replies: 1
    Last Post: 11-14-2010, 02:40 PM
  4. Replies: 4
    Last Post: 01-01-2009, 12:03 PM
  5. A small Question
    By Eku in forum JDBC
    Replies: 7
    Last Post: 09-01-2008, 06:10 AM

Posting Permissions

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