vendredi 20 avril 2018

If-statement: list of values

I have a use-case where I want to run code, but only when one of my variables doesn't have an invalid value (the value comes from a sensor). To be valid, it needs to be greater than -1500, smaller than +1500, and not have a value of -1, 0 or +1. Right now what I do is:

if ((a > -1500 && a < -1) || (a > 1 && a < 1500) {

However, this criteria is being repeated A LOT throughout my code, and it would be so much easier if I could define somewhere in my code what this range of valid values are, so I could simply do:

if (a == valid) {

What is the best approach for this?

To clarify: I do not know how to tell the system what values are valid

Aucun commentaire:

Enregistrer un commentaire