jeudi 25 juin 2015

Simplify the if statement

I have an if statement like this

int val = 1;
if (val == 0 || val == 1 || val == 2 || ...);

Is there a way to do it in a more simplified? For example:

int val = 1;
if (val == (0 || 1 || 2 || ...));

I decided to solve this by creating a function like this:

public boolean ifor(int val, int o1, int o2, int o3) {
    return (val == o1 || val == o2 || val == o3);
}

But this is not enough, because if I wanted to add another parameter in ifor, for example o4, I could not do (should I create another function with the new parameter), or if I wanted to reduce the parameters in o1 and o2. I honestly do not know if I explained, if you ask and I'll try to explain.

Aucun commentaire:

Enregistrer un commentaire