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 8 of 8

Thread: Modulo operation

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Modulo operation

    Hello! I am not quite sure about the output of this program,meaning my calculation and the programs are diffrent.

    public class test2 {
    	public static void main(String[] args) {
    		char c = 'd';
     
    	Out.println(++c % c--);
    	}
    }

    Now the value of 'd' in the ASCII table is 100 so what I think is happening in the program(the print stament) is the value of d is increased by one and than % with 100.So it should be 101%100 which should print out 1.But the program prints out 0 suggestion it was 100%100.What am I missing here? I know the first one is a preincrement meaning the value of c will be increased but does the modulo operation have precedence or? Some clearification would be great, thanks!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modulo operation

    Why write such tricky code? Simplify it so it is easy to understand and debug.
    No one writes code like that.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Modulo operation

    It is an task from a test ,you are given such complicated and tricky codes and you have to guess what the print out will be, that is why.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modulo operation

    Ok, when that expression is executed:
    what is the value to the left of the %
    what is the value to the right of the %
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Modulo operation

    Well, If you would to ask me I would say 101 left, 100 right. Left we have preincrement, and right we have post increment.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modulo operation

    Read up on the post increment. What value does it return when used in an expression? When is the value incremented?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Modulo operation

    Yea i've figured it out.After the ++c the value of i becomes 101, making the value of i in i-- also 101 thus 101%101 = 0; The i--; will take place after this expression.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Modulo operation

    Yes, that sounds right.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    arhzz (June 23rd, 2020)

Similar Threads

  1. some operation in loop.
    By yosilev in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2014, 05:19 AM
  2. How does Modulo work?
    By Mr.nuub in forum Java Theory & Questions
    Replies: 3
    Last Post: October 19th, 2011, 01:04 PM
  3. Byte Operation
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: March 27th, 2011, 11:19 PM
  4. Operation ==
    By meytalg in forum Java Theory & Questions
    Replies: 6
    Last Post: January 4th, 2010, 07:43 PM