Switch?

I'm writing a program that the student inputs his grade and it outputs what grade they got! I'm ussing a switch statment;however, i dont know how to code 90-100,80-89,70-79,60-69,50-59 in my switch statment? i only know how to write single numbers...
You have to list all the numbers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch (number)
{
case 90:
case 91:
case 92:
case 93:
case 94:
case 95:
case 96:
case 97:
case 98:
case 99:
case 100:
	// do something
	break;
case 80:
case 81:
...


It is probably better that you use if statements.
1
2
3
4
5
6
7
if (number >= 90 && number <= 100)
{
	// do something
}
else if (number >= 80 && number <= 89)
{
	...
Last edited on
Actually the correct sintax is like that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
switch (number)
{
case 90: { //do something 
                 break;
               }
case 91: { // do something
                 break;
               }
...............
...............
...............
default: { // do something
                break;
              }
}

So, more exactely:
In the switch condition you have a variable: number. The switch-structure checks if that number is equal with one of that cases (if number==90; number==91; etc.) If it's so, the program will "do something"(print something on the screen, or anything) and then, will break the switch structure. This is a very important instruction and it MUST be placed in every case, otherwise the program will not work properly.
The last part of the switch-structure is the default case. That means, if none of the cases is equal with the number, the program will perform the instructions placed in default (here must be the break instruction either).

Is better to use switch instead of if statements because the switch performs a single condition and the if performs 2 conditions as you see below (because in if the number must be veryfied if is in that interval) so if you use if the complexity will be double.
@Cosmin: The code in the post previous to yours was correct. Yours is correct as well, but totally inappropriate for this case (case - pun - haha!) Braces are only needed if you want to declare a local variable in the case.

@hulibarri: You can map your range to a smaller set of values to make it easier to use a switch.

1
2
3
4
5
6
7
8
9
10
11
switch( score/10 )
{
     case 10:
     case 9: grade = 'A';  break;

     case 8: grade = 'B';  break ;

     // ...

     default: grade = 'F' ;
}
@cire: if he maps his range, he loses some time because of score/10 instruction, as far as I know. In the code previous to me isn't the break; instruction in each case.
@CosminNTG That break instruction is for cases 90-100. It's just a fall-through switch and saves time rather than duplicating code for each branch of the switch.

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// This...
switch(number)
{
   case 91:
      cout << "Number over 90\n";
      break;
   case 92:
      cout << "Number over 90\n";
      break;
   default:
      cout << "Default\n";
      break;
}

// Can be shortened to...

switch(number)
{
   case 91:
   case 92:
      cout << "Number over 90\n";
      break;
   default:
      cout << "Default\n";
      break;
}
Last edited on
@cosmin: If you are using the same statement for a numerous cases,you should not put break; statement in each case . switch has a property that it will merge all other cases without break statement with the succeeding case with break; statement but the condition is the statements should be written only in the case with break;
ok, thanks like this?
switch(grade){
case 100:
case 99:
case 98:
case 97:
case 96:
case 95:
case 94:
case 93:
case 92:
case 91:
case 90:
cout<<"You got an A!"<<endl;
break;
case 89:
case 88:
case 87:
case 86:
case 85:
case 84:
case 83:
case 82:
case 81:
case 80:
cout<<"You got a B"<<endl;
break;
Yeah, that should work. You'd want to have a default case too, though.

Have you considered using an 'if' statement? It'd be a lot shorter to write in a case like this.

1
2
3
4
5
6
7
8
if (grade >= 90 && grade <= 100)
{
   cout << "You got an A!\n";
}
else if (grade >= 80 && grade <= 89)
{
   cout << "You got a B!\n";
}
Topic archived. No new replies allowed.