09-15-00
09-15-00

Reputation: 67

What is wrong with the following code segment, and isn't default switch mandatory for "switch" loops?

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:

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

Upvotes: 0

Views: 74

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

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.

Upvotes: 2

Max
Max

Reputation: 3180

switch work on integral values (int, enum).

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

Upvotes: 1

Related Questions