Colby Toner

Greenhorn

Posts: 11

Zachary Griggs

Ranch Hand

Posts: 83

10

posted 1 year ago

- 1

% 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.

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

posted 1 year ago

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.

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

Colby Toner

Greenhorn

Posts: 11

posted 1 year ago

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.

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.

Learning starts with a question and continues with a discovery Stay hungry for knowledge.

Zachary Griggs

Ranch Hand

Posts: 83

10

posted 1 year ago

- 1

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.

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

posted 1 year ago

- 2

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.

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

posted 1 year ago

- 1

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,

Check the first if statement

Divide 0 by 10. What is the remainder?

The rest of the else ifs are skipped (since they are else if)

Second iteration,

Check the first if statement

Divide 1 by 10. What is the remainder?

Check the else if statement

Divide 1 by 10. What is the remainder?

Skip the rest of the else ifs.

Try finding out what happens for the next iteration.

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.

Don't get me started about those stupid light bulbs. |