Tips'n'Tricks #1: Automatic Loop Counter
You might already know about the trick to automatically switch between 0 and 1, by simply writing
pv = 1 - pv
if you start at 0, consecutive calls of the event will set pv to:
pv = 1 - 0 = 1
pv = 1 - 1 = 0
pv = 1 - 0 = 1
etc.
So far, so good. But what if you wanted to switch between 1 and 2? Or count from 1 to 5, repeatedly?
There's a treasure, we can use here. It's called mod (short for modulo, or modulus), and in CC you use the %-sign to indicate modulo calculation. Modulo calculates the integer remainder of a integer division. For example, 5 % 4 equals 1. To better understand it, let's look at the calculation from the other end. 4 fits into 5 exactly one time (remember: integer division)) and it remains 1. Got it? Ok, what about 11 % 4 ? 4 fits into 11 exactly two times and it remains 3 (2*4 = 8, 11 - 8 = 3). So 11 % 4 equals 3. But why the heck do I tell you this? Because the remainder will never exceed the second number of the calculation. See this list:
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0
See? It loops. And with a simple but clever trick you can use this to your advantage and implement an automatic loop-counter that doesn't need any bounds check.
pv = (pv % [upperbound]) + 1
Fire and forget. Let's say you want to count from 1 to 4 in a loop. 4 will be your upperbound. If you start at 1, consecutive calls of the event will set pv to:
pv = (1 % 4) + 1 = 2
pv = (2 % 4) + 1 = 3
pv = (3 % 4) + 1 = 4
pv = (4 % 4) + 1 = 1
pv = (1 % 4) + 1 = 2
pv = (2 % 4) + 1 = 3
etc.
You can do a lot of other things, too. Say you want to switch between 3 and 5. Take an upper bound of 4 but add 2 instead of 1. Here you go:
pv = (pv % 4) + 2
pv = (3 % 4) + 2 = 5
pv = (5 % 4) + 2 = 3
pv = (3 % 4) + 2 = 5
pv = (5 % 4) + 2 = 3
etc.
You prefer switching between 3 and 4? Alright:
pv = (pv % 2) + 3
pv = (4 % 2) + 3 = 3
pv = (3 % 2) + 3 = 4
pv = (4 % 2) + 3 = 3
pv = (3 % 2) + 3 = 4
etc.
Or maybe you want to count from 4 down to 2 in a loop? Tricky, but possible:
pv = (pv % 3) + 2
pv = (2 % 3) + 2 = 4
pv = (4 % 3) + 2 = 3
pv = (3 % 3) + 2 = 2
pv = (2 % 3) + 2 = 4
pv = (4 % 3) + 2 = 3
etc.
"Right", you say, "now do something completely weird. Your dumb trick will never succeed. I want you to automatically calculate the numbers 5, 2, 4, 6, 3 exactly in this order and looped. HA!" ...Hmm, you mean like so?
pv = (pv % 5) + 2
pv = (3 % 5) + 2 = 5
pv = (5 % 5) + 2 = 2
pv = (2 % 5) + 2 = 4
pv = (4 % 5) + 2 = 6
pv = (6 % 5) + 2 = 3
pv = (3 % 5) + 2 = 5
etc.
You can even do an addition that doesn't add anything, but always stays at the number you add. Ok, it doesn't make sense, but it is possible <img src="smileys/smiley36.gif" border="0" align="middle" /> :
pv = (pv % 1) + anynumber
pv can be any number too, e.g. 143
pv = (143 % 1) + 5 = 5
pv = (5 % 1) + 5 = 5
etc.
Now that you learned the magic of modulo, go out and spread the word. Find wonderful rows of looped numbers, or find new ways of using modulo. The fantastic world of modulo counting awaits you...