# assign values in sequence

assign; I have tried every thing I can think of to shorten multipol ifs. Can It be done?
if (rotation == 1) y = 1; if (rotation == 2) y = 2; if (rotation == 3) y = 3; //only record times for starts and finish
if (rotation==101)y=4; if (rotation==102)y=5; if (rotation==103)y=6;

``````  if (rotation >= 1 && rotation <= 3)
y = rotation ;
``````

etc

I can't find the file that was saved for forum.
arrg, That will work for the first but after that y doesn't = rotation.
Are code tags the part following the //?

Not seeing your code I don't know if this was you're after, but I'm guessing you should probably be using a switch case.

dexterj:
assign; I have tried every thing I can think of to shorten multipol ifs. Can It be done?
if (rotation == 1) y = 1; if (rotation == 2) y = 2; if (rotation == 3) y = 3; //only record times for starts and finish
if (rotation==101)y=4; if (rotation==102)y=5; if (rotation==103)y=6;

You are correct. It looks like after that you’d have:

``````if (rotation >= 101 && rotation <= 103)
y = rotation - 97;
``````

Basic math.

Thank you.

However, 97 is in this case what is called a "magic number". In other words, has no explanatory value. So if it's not bracketed by a lot of comments or the purpose is not made clear somehow, it has the potential to baffle people who are trying to understand the code.

aarg:
However, 97 is in this case what is called a “magic number”. In other words, has no explanatory value. So if it’s not bracketed by a lot of comments or the purpose is not made clear somehow, it has the potential to baffle people who are trying to understand the code.

The way to document that is simply:

``````if (rotation >= 101 && rotation < 101+3
y = rotation - 101 + 4;
``````

And you can #define the various constants too, 3, 101 and 4 with meaningful
names:

``````#define RANGE_START 101
#define RANGE_COUNT 3
#define MAP_TO 4

if (rotation >= RANGE_START && rotation < RANGE_START+RANGE_COUNT)
y = rotation - RANGE_START + MAP_TO;
``````

or use map

``````#define RANGE_START 101
#define RANGE_COUNT 3
#define MAP_TO 4

if (rotation >= RANGE_START && rotation < RANGE_START+RANGE_COUNT)
y = map (rotation,  RANGE_START,  RANGE_START+RANGE_COUNT-1, MAP_TO, MAP_TO+RANGE_COUNT-1) ;
``````

The issue is, what is "y" used for? Is it algorithmically necessary for it to contain consecutive values? Or will it just be used as a status variable, which will later be used in something like:

``````if (y == 5)
{
Serial.println("yay! we have a winner!");
score++;
}
``````

which I would object to as wasteful, since we could just assign y = rotation and:

``````if (y == 102)
{
Serial.println("yay! we have a winner!");
score++;
}
``````

... and that is being generous and assuming that "rotation" itself can't be used instead of y.

It's often good to look at the big picture. That is why I pleaded for the entire sketch. But given the original question, I'm surprised that the subject of enumerated constants didn't come up.

dexterj:
assign; I have tried every thing I can think of to shorten multipol ifs. Can It be done?
if (rotation == 1) y = 1; if (rotation == 2) y = 2; if (rotation == 3) y = 3; //only record times for starts and finish
if (rotation==101)y=4; if (rotation==102)y=5; if (rotation==103)y=6;

``````y = (rotation % 100) + (rotation/100)*3;
``````

I use the "modulus" operator, and I make use of the fact that division of integers truncates.

PaulMurrayCbr:

``````y = (rotation % 100) + (rotation/100)*3;
``````

I use the "modulus" operator, and I make use of the fact that division of integers truncates.

Cool. But there is no bounds check.

aarg:
Cool. But there is no bounds check.

Well … no. The OP didn't mention anything about bounds, so I was assuming that it wasn't a thing. After all - unless y is explicitly assigned before his ifs(), it will contain rubbish anyway. I have to suppose that the fragment he was asking about is surrounded by some other code that checks bounds.

Illustrating the uselessness of snippets.