scugn1zz0
scugn1zz0

Reputation: 337

Convert IF-statements in Switch-statement

I have a series of IF-statements and i would like to convert in a Switch-statement but I can't succeed in inserting an evaluation in case constant1 field of switch.

I know that Switch works that way:

switch ( expression ) {   //in my case:      switch (score) {
  case constant1:
    statement
      break;
  case constant2:
    statement
  default:
    statement
      break;

Now I've tried to put <= 60 in the constant1 field, but of course it doesn't work.

This is the series of IF statements I want to convert in Switch.

if (score <= 60) {
    printf("F");
}
if (score <= 70 && score > 60) {
    printf("D");
}
if (score <= 80 && score > 70) {
    printf("C");
}
if (score <= 90 && score > 80) {
    printf("B");
}
if (score <= 100 && score > 90) {
    printf("A");
}

Thanks to all!

Upvotes: 2

Views: 3248

Answers (6)

Krishnakumar T G
Krishnakumar T G

Reputation: 175

A little digression. You could try following pseudo. It is short and simple.

char cGrade = 9-(score/10)+65;
if( cGrade > 68 )
{
    cGrade = 70; // for 'F'
}
else if( cGrade < 65 )
{
    cGrade = 65; // for 'A'
}

Upvotes: 0

lapk
lapk

Reputation: 3918

You can use if or switch, or you can simply:

 printf("%c", 70 - ((score - 60) > 0 ? (score - 41) / 10 : 0));

Live example on ideone.com

P.S. Of course, similar can be used in switch statement to have exactly five case's.

Upvotes: 0

unalignedmemoryaccess
unalignedmemoryaccess

Reputation: 7441

As mentioned in comment, you can't do it like this since in switch statement, you can have only 1 expression.

Use if-else statement like this:

if (score <= 60) {
    printf("F");
} else if (score <= 70) {
    printf("D");
} else if (score <= 80) {
    printf("C");
}
//More statements

With enabled GCC extensions for switch, you can use like this:

switch (score) {
    case 0...60:
        break;
    case 61...70:
        break;
    //..More cases with range
}

Upvotes: 0

muXXmit2X
muXXmit2X

Reputation: 2765

A switch only checks for equality. Therefore in your case the if-else construct is way better suited.

Still, if you want to use a switch statement you have to do it like this:

switch (score) 
{
  case 0:
  case 1:
  case 2:
  ... // all cases up to 58
  case 59:
  case 60:
    printf("F");
    break;
  case 61:
  ...
}

Not really pretty and very tedious.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

switch statement takes constants, not conditions. For instance, you cannot say >= const, so you need to change the strategy.

For example, in your case you can switch on the first digit of your two-digit score, after subtracting 1 from it:

switch ((score-1) / 10) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5: printf("F"); break;
    case 6: printf("D"); break;
    case 7: printf("C"); break;
    case 8: printf("B"); break;
    case 9: printf("A"); break;
}

Cases 0..4 use C's fall-through mechanism for switch statements, all printing "D".

The code above assumes that you have checked the range of the score to be 1..100, inclusive.

Upvotes: 4

neoaggelos
neoaggelos

Reputation: 640

The correct syntax is case *constant*, so you can't ever write a case < 60.

What you can do, is put several case commands below one another like this:

case 40:
case 41:
case 42:
case 43:
    // do stuff
break;

This will 'do stuff' if the switch statement is equal to 40, 41, 42 or 43. However, may I suggest that unless you have a good reason to convert your if-statements to switch-statements, you shouldn't in this particular occasion.

Upvotes: 0

Related Questions