switch

Switch Limitations

If-else statements are much more flexible than switch statements. In fact, the case clauses in switch statements can only make comparisons between integer values. Switch cases can also compare characters like in the example code because C++ is actually converting the characters to integers.

On the other hand, if statements can make comparisons between floating point numbers as well as between integers.

   int variable = integer;

    switch(variable) {
        case 1:
            code statements;
            break;
        case 2 :
            code statements;
            break;
        case 3:
            code statements;
            break;
        case 4:
            code statements;
            break;
        case etc ... 
    }

Last updated