Home > Mobile >  What is wrong with the following code segment, and isn't default switch mandatory for "swi
What is wrong with the following code segment, and isn't default switch mandatory for "swi

Time:11-24

I am tired of my university, my midterm exams and I am tired of thinking what the hell is wrong here.

double n;
n = 3.5; // line 2
switch( n){
case 2.5 : printf(“High”); break;
case 0.5 : printf(”Low”); break;}

In general, I thought default statement is missing because n is 3.5 and every switch statement we wrote included a default statement.

Options are:

  • I)default statement is missing (What I ticked.)

    II)there is an error in line 2

    III)semi colon is missing at the end of the switch statement

    IV)double data type cannot be used with switch statement (Correct answer.)

Can anyone explain me why IV is correct answer? Thank you.

CodePudding user response:

The label default may be omitted in the switch statement. However the case labels shall be of integer type.

So the switch statement is incorrect at least due to incorrect case labels.

Also the expression in the switch statement shall have an integer or an enumeration type.

CodePudding user response:

switch work on integral values (int, enum).

If you want to "switch" on double values, you will need to use if/else statements.

  • Related