sonesay 7 Report post Posted January 11, 2008 (edited) Ok I've been doing some revision and came across this operator again. I've seen it before but havent fully understood what it does or its surpose to do. Taken from "Objects first in java 2006" The modulo operatorThe last method in the NumberDisplay class increments the display value by 1. It takes care that the value resets to zero when the limit is reached:public void increment(){ value = (value + 1) % limit;}This method uses the modulo operator (%). The modulo operator calculates the remainder of an integer division. For example, the result of the division27 / 4can be expressed in integer numbers asresult = 6 remainder = 3The modulo operator returns just the remainder of such a division. Thus the result of the expression ( 27 % 4) would be 3. The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused. For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number./sigh someone help explain this to me so I can figure this out. Edited January 11, 2008 by sonesay (see edit history) Share this post Link to post Share on other sites
rvalkass 5 Report post Posted January 11, 2008 The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work.Modulo is what most people know as "child's division". When you are first taught to divide, you are told to give an answer like "6 remainder 2". Modulo returns the remainder. As far as I can tell, you understand this operation, and what is actually outputted when you use modulo? What you don't understand is how it is being used in the counter. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused.Lets go for an example. We'll start with value set to 0 and limit set to 7, OK? Good. Below I'll list what the code does and what output you'll get. Perhaps you'll spot the pattern and why it works:value = (0+1)%7 = 1%7 1/7 = 0 remainder 1 OUTPUT: 1value = (1+1)%7 = 2%7 2/7 = 0 remainder 2 OUTPUT: 2value = (2+1)%7 = 3%7 3/7 = 0 remainder 3 OUTPUT: 3value = (3+1)%7 = 4%7 4/7 = 0 remainder 4 OUTPUT: 4value = (4+1)%7 = 5%7 5/7 = 0 remainder 5 OUTPUT: 5value = (5+1)%7 = 6%7 6/7 = 0 remainder 6 OUTPUT: 6value = (6+1)%7 = 7%7 7/7 = 1 remainder 0 OUTPUT: 0value = (0+1)%7 = 1%7 8/7 = 0 remainder 1 OUTPUT: 1Continuing in an infinite loop...As value is increased each time, the number you are dividing by 7 also increases. As this happens, the remainder you get also increases. When you reach a number that is in the 7 times table, there is no remainder (as it divides exactly by 7) so the counter appears to reset to 0. This also resets 'value' to 0, and the sequence begins again. For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number.The lower number divided by a large number is shown in my example above. The large number goes into the small number 0 times, leaving a remainder equal to the small number. Share this post Link to post Share on other sites