Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple checks in a if statement

In java is there a faster way of doing this?

if (keyCode != 66 && keyCode != 8 && keyCode != 21 && keyCode != 22) {

}

keyCode is an int.

like image 200
Josh Avatar asked Apr 09 '26 04:04

Josh


1 Answers

Faster? Is it too slow for you? Don't play optimizer. Write readable code and leave microoptimizations to the optimizer. Premature optimization is the root of all evil

Edit after josh's comment:

If you have really many of them, put them in a container (such as a set or an array) and find keyCode in it. If you found it, then your condition is false. Otherwise it's true.

As per Dave's comment:

if(!MyCodesSet.Contains(keyCode)){
}
like image 84
Armen Tsirunyan Avatar answered Apr 11 '26 18:04

Armen Tsirunyan