Question:

Are these valid cases for a switch statement in c++ ?

by  |  earlier

0 LIKES UnLike

non-constant integers

constant characters

non-constant characters

constant floating points

are these usable as cases in a switch statement in the c++ language

 Tags:

   Report

2 ANSWERS


  1. The C and C++ standards require cases to reduce to an integer constant. A constant character reduces to one (promotes, technically). A constant floating point does not.

    Being able to use a const floating point would be a useless feature anyway. Comparing a floating point number (for equality) to anything other than zero gives unpredictable results.

    So the answers are: no, yes, no, no. You didn't ask about constant integers, but the answer is yes.


  2. non constant characters and non-constant integers can not be case in switch statement

    mm some explaining...

    switch(variable)

    case: constant

    variable can be lets say integer type from 1 to 10

    case: its constant e.g.

    case 1:

    some statement

    break;

    case 2:

    some statement

    break.

    1 and 2 are numeric hard values and are constant

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.