math - maximum and minimum constraint on a variable -


I type so many ways by looking at something like this:

  / * myVal Between 10 and 90 * / int myVal = foo; If (myVal & lt; 10) {myVal = 10; } And if (myVal> 90) {myVal = 90; }   

Is there another great way to do this? Obviously you can easily write a method, but I wonder if there was a more natural way of establishing a barrier in any language, or something else which I am missing.

Language is agnostic because I am interested in how it can behave in different languages.

In most programming languages, you can

  myVal = min (max Foo, 10), 90);   

Or just write a clip () macro which does the same thing.

Comments