Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How do you use the "Mod" Block?
- Neon-Bot
-
13 posts
How do you use the "Mod" Block?
I just need information on the “Mod” block and how to actually use it in scripting. If you have an answer, please do post it below, it'd really help, thanks.
- master_tolkien
-
100+ posts
How do you use the "Mod" Block?
Well, mod stands for modulo and it returns the remainder of division between two numbers. One way you could use it is to figure out if a number is even. You would do
Hope this helps
((number) mod (2))and if the number is even then it would return zero, and otherwise it would not. This could possibly be used in a game where you need the player to get a power-up or something every two points they get. In this game, you could increase the number you are dividing by to make the gap between power-ups greater.
Hope this helps

- Thejbomber
-
100+ posts
How do you use the "Mod" Block?
If there is a block you don't know, right click the block and select help.
- MoreGamesNow
-
100+ posts
How do you use the "Mod" Block?
Modulo is a really neat operation. Basically “17 mod 5” it answers the question “if I keep taking away 5s from 17, what number do I end up with”. In this case if you take away one 5 from 17 you get 12. Take away another 5 and you get 7. Take away another 5 and you get 2. You can't take away 5 from 2 (without going below zero!) so “17 mod 5” is equal to 2.
Another way to think of it is “how close is 17 to being divisible by 5”. Well if you take away 2 form 17 you get 15, which is divisible by 5, so 17 is “2 away” form being divisible by 5.
You can also think of it as giving you the “remainder” after you do division. For instance 16 mod 3 is 1, and 16 divided by 3 is "4 with a remainder of 1“
Some common uses are to check if a number is divisible by another number. For instance, to check if 24 is divisible by 4, we do ”24 mod 4“ and get 0. This means 24 is ”zero away“ from being divisible by 4 – in other words, it means 24 is divisible by 4. Likewise, we can see if 21 is divisible by 4 by using ”21 mod 4". We get 1, so 21 is almost divisible by 4, but not quite.
In fact, the "1' we get from 21 tells us how much we need to take away from 21 to make it divisible by 4. If we take away 1 from 21, we get the biggest number that is divisible by 4 and less than to 21.
So, you can think of modulo in two different ways:
- first: if you keep taking away the second number from the first number, how much do you have left in the end.
- second: how much should you take away from the first number to make it divisible by the second number
- third: the remainder of a division problem
It has a number of uses in scripting. As mentioned above, you can check to see if a number is even by seeing if it is divisible by 2. In other words:
is the same as asking “is x even”.
You can also use it to find out what the one's place of a number is. For example:
- 23 mod 10“ is 3
- ”217 mod 10“ is 7
- ”1945 mod 10“ is 5
Or the ones and ten's place of a number
- 23 mod 100” is 23
- “217 mod 100” is 17
- “1945 mod 100” is 45
It is also useful when dealing with grids, or, to give a practical application, with chessboards). Many people like to store their boards in a list, where each item says what piece is on a certain tile. Usually the first element stores what is one the top-left corner, the second element stores the piece on the tile next to it, etc.

An interesting question, then, is to say “if I have a tile, how do I get it's x and y coordinate?”. The y coordinate can be found with division and rounding, but to find the x coordinate we need to use modulus. In fact the formula is “X = ((T - 1) mod 8) + 1”.
The third way I often use modulus is if I am doing something a lot of times, but every 100 times I want to do something else.
Another way to think of it is “how close is 17 to being divisible by 5”. Well if you take away 2 form 17 you get 15, which is divisible by 5, so 17 is “2 away” form being divisible by 5.
You can also think of it as giving you the “remainder” after you do division. For instance 16 mod 3 is 1, and 16 divided by 3 is "4 with a remainder of 1“
Some common uses are to check if a number is divisible by another number. For instance, to check if 24 is divisible by 4, we do ”24 mod 4“ and get 0. This means 24 is ”zero away“ from being divisible by 4 – in other words, it means 24 is divisible by 4. Likewise, we can see if 21 is divisible by 4 by using ”21 mod 4". We get 1, so 21 is almost divisible by 4, but not quite.
In fact, the "1' we get from 21 tells us how much we need to take away from 21 to make it divisible by 4. If we take away 1 from 21, we get the biggest number that is divisible by 4 and less than to 21.
So, you can think of modulo in two different ways:
- first: if you keep taking away the second number from the first number, how much do you have left in the end.
- second: how much should you take away from the first number to make it divisible by the second number
- third: the remainder of a division problem
It has a number of uses in scripting. As mentioned above, you can check to see if a number is even by seeing if it is divisible by 2. In other words:
<((x) mod [2]) = [0]>
is the same as asking “is x even”.
You can also use it to find out what the one's place of a number is. For example:
- 23 mod 10“ is 3
- ”217 mod 10“ is 7
- ”1945 mod 10“ is 5
Or the ones and ten's place of a number
- 23 mod 100” is 23
- “217 mod 100” is 17
- “1945 mod 100” is 45
It is also useful when dealing with grids, or, to give a practical application, with chessboards). Many people like to store their boards in a list, where each item says what piece is on a certain tile. Usually the first element stores what is one the top-left corner, the second element stores the piece on the tile next to it, etc.

An interesting question, then, is to say “if I have a tile, how do I get it's x and y coordinate?”. The y coordinate can be found with division and rounding, but to find the x coordinate we need to use modulus. In fact the formula is “X = ((T - 1) mod 8) + 1”.
The third way I often use modulus is if I am doing something a lot of times, but every 100 times I want to do something else.
set [n v] to [0]
repeat [10000]
change [n v] by [1]
if <((n) mod [100]) > [0]>
do something
else
do something else
end
end
Last edited by MoreGamesNow (July 21, 2015 14:27:30)
- Neon-Bot
-
13 posts
How do you use the "Mod" Block?
Modulo is a really neat operation. Basically “17 mod 5” it answers the question “if I keep taking away 5s from 17, what number do I end up with”. In this case if you take away one 5 from 17 you get 12. Take away another 5 and you get 7. Take away another 5 and you get 2. You can't take away 5 from 2 (without going below zero!) so “17 mod 5” is equal to 2.
Another way to think of it is “how close is 17 to being divisible by 5”. Well if you take away 2 form 17 you get 15, which is divisible by 5, so 17 is “2 away” form being divisible by 5.
You can also think of it as giving you the “remainder” after you do division. For instance 16 mod 3 is 1, and 16 divided by 3 is "4 with a remainder of 1“
Some common uses are to check if a number is divisible by another number. For instance, to check if 24 is divisible by 4, we do ”24 mod 4“ and get 0. This means 24 is ”zero away“ from being divisible by 4 – in other words, it means 24 is divisible by 4. Likewise, we can see if 21 is divisible by 4 by using ”21 mod 4". We get 1, so 21 is almost divisible by 4, but not quite.
In fact, the "1' we get from 21 tells us how much we need to take away from 21 to make it divisible by 4. If we take away 1 from 21, we get the biggest number that is divisible by 4 and less than to 21.
So, you can think of modulo in two different ways:
- first: if you keep taking away the second number from the first number, how much do you have left in the end.
- second: how much should you take away from the first number to make it divisible by the second number
- third: the remainder of a division problem
It has a number of uses in scripting. As mentioned above, you can check to see if a number is even by seeing if it is divisible by 2. In other words:<((x) mod [2]) = [0]>
is the same as asking “is x even”.
You can also use it to find out what the one's place of a number is. For example:
- 23 mod 10“ is 3
- ”217 mod 10“ is 7
- ”1945 mod 10“ is 5
Or the ones and ten's place of a number
- 23 mod 100” is 23
- “217 mod 100” is 17
- “1945 mod 100” is 45
It is also useful when dealing with grids, or, to give a practical application, with chessboards). Many people like to store their boards in a list, where each item says what piece is on a certain tile. Usually the first element stores what is one the top-left corner, the second element stores the piece on the tile next to it, etc.
An interesting question, then, is to say “if I have a tile, how do I get it's x and y coordinate?”. The y coordinate can be found with division and rounding, but to find the x coordinate we need to use modulus. In fact the formula is “X = ((T - 1) mod 8) + 1”.
The third way I often use modulus is if I am doing something a lot of times, but every 100 times I want to do something else.set [n v] to [0]
repeat [10000]
change [n v] by [1]
if <((n) mod [100]) > [0]>
do something
else
do something else
end
end
Awesome explanation, that really helps a lot, thank you so much!
- Chippy2Test
-
68 posts
How do you use the "Mod" Block?
Divide the two numbers, the answer is the remainder between.
((30) mod (4)) //2the remainder between the dividers.
((30) mod (4) is (2). ::operators) // don't ask.
(30) mod (4) is(2). ::ring operators //a stack block
- DrKat123
-
1000+ posts
How do you use the "Mod" Block?
Sorry, but I think that this post is already resolved. Please check the date of the post before posting. Thanks Divide the two numbers, the answer is the remainder between.((30) mod (4)) //2the remainder between the dividers.
((30) mod (4) is (2). ::operators) // don't ask.
(30) mod (4) is(2). ::ring operators //a stack block
- Discussion Forums
- » Help with Scripts
-
» How do you use the "Mod" Block?