# Using Switch statement

This is a discussion on Using Switch statement within the C Programming forums, part of the General Programming Boards category; Dear all, I have one question. Enter a persons weight in kilograms and height in metres. Calculate the persons Quetelet ...

1. ## Using Switch statement

Dear all,

I have one question.

Enter a persons weight in kilograms and height in metres. Calculate the persons Quetelet Index (kolos/(meters *meters)). Print out the Quetelet Index and an appropriate message as indicated by the table below.
Below 20 Underweight
20 to below 35 Healthy weight
25 to below 30 Midly overweight
30 to below 40 Very overweight
40 and above Extermely overweight

I did this using If else statement but now i want to do this with switch statement.I tried but not successfull. Please help and guide.

2. Frankly, a switch looks inappropriate here, in my opinion.

3. Definetely, as you'd have to test an large amount (>40) amounts of numbers. In if/else you only need 5 statements.

4. Hi Ubha ,

Actually if is good here but as you want it in switch there is one way, first calculate the index, let it be in variable a, then u can write like this
Code:
```sw=a/10;
switch(sw)
{
case 0:
case 1:
printf("Under weight");
break;
case 2:
if (a>=20 && a<25)
printf("Healthy weight");
if (a<=25 && a<30)
printf("Midly weight");
break;
case 3:
printf("Very overweight");
break;
default:
printf("Extremely overweight");
}```
Hope, this will solve your problem, atleast some part of it. Any comments are welcome.

5. Code:
```case 2:
if (a>=20 && a<25)
printf("Healthy weight");
if (a<=25 && a<30)
printf("Midly weight");
break;```
has some problems
Code:
```case 2:
if (a<25)
printf("Healthy weight");
else
printf("Midly weight");
break;```

6. Like stated, you cannot test a condtion in a switch, unless you nested the if statements
within the switch

Code:
```switch ( choice )
{
case 1:
if ( a < b )
// do somthing
break;
}```

7. I have no idea what you're trying to say with the above post. The only thing wrong with their code was that their second if statement should have been testing >= instead of <=.

Quzah.

8. I was just re-iterating the example of vart, showing you can test a condition within a switch. It was not supposed to be the answer to the OP question, that had already been done

9. Originally Posted by quzah
I have no idea what you're trying to say with the above post. The only thing wrong with their code was that their second if statement should have been testing >= instead of <=.

Quzah.

thank you Quzah, yes I made mistake there, ye it should be the way you have stated it. Thank you once again.
And sorry I couldn't visit you all for a long time.