• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on how the % works in a certain group of code.  RSS feed

 
Colby Toner
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm very confused as to what the code above does inside the if statement. Any explanation would be helpful. Thanks!
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
% is the modulo math operation. It's related to division - but while division throws out the remainder (when using ints of course), modulo throws out the result and keeps only the remainder.

For example: 10 % 10 == 0, since the remainder of 10 divided by 10 is nothing (divisible)
5 % 10 == 5, since the remainder is 5.

Some uses for this operation:
- Testing divisibility (num % divisible == 0 if it's divisible)
- Changing a number into the certain range (result of modulo will always be [0,n-1] where n is the divisor) which can be useful for fixed list sizes.
- From this, we can use it for wrapping around numbers or fitting a hash code into a list.
 
Colby Toner
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if I wish to have many "if else" statements after the "if" statement I would simply make the equation equal one number higher? For example in my code, i%10 == 0 which would give a remainder of 9 if i'm correct.
Would I simply add one to the 0 to have i%10 == 1. Which would return 8? Thank for the quick response.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you did i%10==1, this would be true when the remainder is one. Or for the following numbers: 1, 11, 21, 31, 41, 51, 61, 71. (remember, the code is looping from 0 to 80)
 
Colby Toner
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright that response really confused me. I thought I understood, but now I don't think I do. Can you give another simple example of using the %. And how I would use the % in an else if statement for loop
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Coby,

Just think of the % as dividing but is only concerned of the remainder:

So if we have:



So if you were to place this in your if statement when the remainder is 1 it is true.

It is kind of like a key (algorithm), if the if statement is true print the statement if not put the thread asleep for 200 seconds or 200 Milli-seconds.

Play around the code I gave you and use your own numbers. The modulus is a great tool!
Does this make sense.

Bill F.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. The function a%b divides a by b, then takes the remainder of the operation.

So let's start out with an application of modulo.
Let's say we want to see whether a number is or is not divisible by 5. Here are some examples of how we might use it.

1 / 5 = 0, with a remainder of 1. Therefore 1%5==1
Is 1 divisible by 5? No.

3 / 5 = 0, with a remainder of 3. Therefore 3%5==3
Is 3 divisible by 5? No.

5 / 5 = 1, with a remainder of 0. Therefore 5%5==0
Is 5 divisible by 5? Yes.

So then we can write the function:

Which will check whether or not it's divisible. Since this return statement only is true if the remainder of test/5 is zero (like 5/5, or 10/5)

Using it in loop depends on what exactly you want to do. We could test if the number is divisible by 10 or 11 with the following, for example:

Try to guess what the output would be and run the code.
 
Colby Toner
Greenhorn
Posts: 11
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the reply and yes this is starting to make a tad bit of sense. I played around with the code for a little bit and made a little snake type animation that is sloppy but beside the point. Still a little confused how the code works and what kind of situation would you use it.
EDIT: My question wasn't directly stated, i'm really confused what is happening each line of the if and if else statements and why they work to keep printing out the animation.
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason they keep happening is because it is in a for loop - from 0 to 80 non inclusive, so it will run exactly 80 times.
If you want an understanding of what's happening, try taking a pen and paper and go through some iterations.

In our first iteration, i=0
Check the first if statement (i%10==0)
Divide 0 by 10. What is the remainder? 0. Therefore the first if statement runs.
The rest of the else ifs are skipped (since they are else if)

Second iteration, i=1
Check the first if statement (i%10==0)
Divide 1 by 10. What is the remainder? 1. 1 != 0, so this if statement does not happen.
Check the else if statement (i%10==1)
Divide 1 by 10. What is the remainder? 1. 1 == 1, so this statement runs and prints that line.
Skip the rest of the else ifs.

Try finding out what happens for the next iteration.
 
Bill foster
Ranch Hand
Posts: 66
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's why I love programming, it is like math. Grab a piece of paper and a pencil and figure it out. This help great with loops!
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

These lines don't make sense. If you divide anything by 10, will the remainder ever by 10 or 11?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!