dimanche 29 décembre 2019

Execute code only if one of the if statements ran

Take a look at this example code.

boolean check = false;

if (condition) {
    do x;
    check = true;
}
else  if (condition2) {
    do y;
    check = true;
}
else if (condition3) {
    do z;
    check = true;
}

if (check) {
    do a;
} else {
    do b;
}

There has to be a better way to do this than a boolean variable, no? I mean it works, but it looks kind of messy to me.

Aucun commentaire:

Enregistrer un commentaire