samedi 25 avril 2015

Why should "case statement" be constant?

In JavaScript the following statement is valid.

switch(true) {
    case a > b:
        max = a;
        break;
    case a < b:
        max = b;
        break;
    default:
       max = a;
}

But in C/C++ language, when I write this statement, compiler gives me an error showing case statement must be consist of constant values. Sometimes in particular circumstances writing such switch-case statements would be very useful, but C/C++ will not allow me to do so.

Now I am curious to know what is the point behind this to not allowing variable values to be used in case statements?

Aucun commentaire:

Enregistrer un commentaire