I am making a folder launcher skin with the feature to adjust the number of folders that is visible.
The maximum number of folders is 16, and should you hit the max, the skin should go back to 1 and start over.
And vice versa in the other direction.

This bang works perfectly when counting up: (Counts from 1) [!SetVariable "NumberofFolders" "#NumberofFolders#%16+1"]

But i can only find the following formula to count down and wrap back around to 16:
Problem is that it counts to 0, and THEN 1. [!SetVariable "NumberofFolders" "(#NumberofFolders#-1+(16+1))%(16+1)"]

I bet it's as simple as moving around or deleting a few parentheses, but i just can't see it.

Something like this maybe: [!SetVariable NumberofFolders "(((#NumberofFolders#-1)<1)?16:(#NumberofFolders#-1))"]. With this formula you can control easily both, when to wrap back to the largest possible value (through the red parameter above) and this largest value (16 in this case).

Something like this maybe: [!SetVariable NumberofFolders "(((#NumberofFolders#-1)<1)?16:(#NumberofFolders#-1))"]. With this formula you can control easily both, when to wrap back to the largest possible value (through the red parameter above) and this largest value (16 in this case).

To be honest I think some parenthesis are needed here. Correctly it would be: [!SetVariable "NumberofFolders" "(#NumberofFolders#%16+1)"]

I see what you did there. And it works great
I think i understand it fully now:
Is #NumberofFolders#-1 less than 1? then it's 16. If not then it's #NumberofFolders#-1.

And for counting up:
(((#NumberofFolders#+1)>16)?1:(#NumberofFolders#+1))

I see what you did there. And it works great
I think i understand it fully now:
Is #NumberofFolders#-1 less than 1? then it's 16. If not then it's #NumberofFolders#-1.

Exactly. Probably not too tricky, but usually it works and is very efficient, being easy to modify, if needed.

I see what you did there. And it works great
I think i understand it fully now:
Is #NumberofFolders#-1 less than 1? then it's 16. If not then it's #NumberofFolders#-1.

And for counting up:
(((#NumberofFolders#+1)>16)?1:(#NumberofFolders#+1))

And for counting up:
(((#NumberofFolders#+1)>16)?1:(#NumberofFolders#+1))

Yes, for counting up this can help.
If you count up and is good to wrap back to 0 when you reach the largest value (in this case 16), probably the easiest formula is [!SetVariable "NumberofFolders" "((#NumberofFolders#+1)%17)"]. But if you want to wrap back not to 0, but to 1 (or to any other value), this is ok.

Something like this maybe: [!SetVariable NumberofFolders "(((#NumberofFolders#-1)<1)?16:(#NumberofFolders#-1))"]. With this formula you can control easily both, when to wrap back to the largest possible value (through the red parameter above) and this largest value (16 in this case).

Just recently I was working on the same issue: counting down and resetting to the largest value when reaching one, rather than when reaching zero. I knew it could be done with the conditional operator, but was thinking that doing it with the modulus operator would be nice, as it would be symmetrical with the count up loop.

But after banging my head against this wall for a while , I decided to search the forums, and came upon this thread. So I wondered, if that example were rewritten like this: [!SetVariable NumberofFolders "((#NumberofFolders#=1)?16:(#NumberofFolders#-1))"] one would save a few CPU cycles by doing the subtraction only once; but the savings would likely be entirely negligible on modern systems. [It's just the old machine-language/assembly-language programmer in me fighting for every cycle! ]

But just out of interest, can anyone come up with a modulus-based formula for this?

But just out of interest, can anyone come up with a modulus-based formula for this?

For example, in the following code you have to set the limits of counting. There are two variables, Min and Max, which probably you've figured out what do mean:

Unfortunately as you can see I had to use two measures, to make the count down. Probably it's not possible to make it, using one single measure (me at least couldn't find a way to do this in a such way).
You can set even negative limits (for example try the Min=-5 and Max=12 variables)

For example, in the following code you have to set the limits of counting. There are two variables, Min and Max, which probably you've figured out what do mean:

Unfortunately as you can see I had to use two measures, to make the count down. Probably it's not possible to make it, using one single measure (me at least couldn't find a way to do this in a such way).
You can set even negative limits (for example try the Min=-5 and Max=12 variables)

Thanks, balala; appreciate your taking the time to look at it. I believe the following formula will count down to one, and loop: (((#counter#+#counterMax#-2)%#counterMax#)+1)
and present the following data for counterMax=7:

where OOB = out of bounds. This was designed simply to count down by one, from a maximum to one, and then loop back to maximum. No consideration was given to increments other than one. Nor was consideration given to out-of-bound starting numbers, although it appears to automatically bring such back into bounds. Having some extra sets of eyes look it over would be helpful.

This isn't necessarily better than using the conditional operator, but does satisfy the requirements of using the modulus operator to be symmetrical with count-up loop.

(supposing as can be seen above that the measure is named [MeasureCount]).
By simply using your Formula won't change the value of the counter variable and the measure won't count down.
Otherwise congratulations, as you can see, as well as many other times, here are more possible solutions, from which we can choose if we need to.