Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Just a beginner asking for help with 1 line of code

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Just a beginner asking for help with 1 line of code

    Hello everyone. I started learning Java just now and right now writing simple codes. Made a program, where it displays all the numbers from the range 1 - 100 which are divisible by 5
    // beginning from 100 (in reverse order).

    I just have a question about this line of code:

    if (i % 5 == 0) {
    System.out.print(i + " --NEXT-- ,");

    It prints 100 NEXT, 95 NEXT and so on, but what does == (equality operator) and 0 in the end really mean. Can someone explain it to me really simple? Also, when does 0 turn to 1 after ==? Does 0 turn to 1 if for example i is 101 and it goes i % 5==1 because 101 divided by 5 is 20 and 1 is leftover?

    Kind Regards,

    Squish

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,555
    Thanks
    53
    Thanked 2,517 Times in 2,468 Posts

    Default Re: Just a beginner asking for help with 1 line of code

    (i % 5) == 0
    I added ()s to make it easier to read.
    The computer first does the % operation (which returns a int value) and then compares that int value to 0 which returns a true or false value that is used by the if statement to select what to do next.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What does this line in the code mean?
    By jenniferruurs in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2020, 03:41 AM
  2. have code - need one line fixed please!
    By ash12 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 7th, 2012, 07:24 PM
  3. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  4. enum, value of: Why is this code line structured the way it is?
    By SPACE MONKEY in forum Java Theory & Questions
    Replies: 5
    Last Post: March 28th, 2011, 09:15 AM
  5. Help with a line of my code.
    By Miical94 in forum Object Oriented Programming
    Replies: 1
    Last Post: March 7th, 2011, 11:00 AM