Range in switch statement.

Feb 18, 2014 at 8:30am
//I'm a beginner and I just started doing these test exercises on the forum.
//I'm doing the first one.

http://www.cplusplus.com/forum/articles/12974/

//So, I made this program in which I'm required to have a switch between a range //of numbers. But i can figure out how.

#include <iostream>

using namespace std;

int main()
{
int score;

cout << "Enter your score.\n";
cin >> score;

switch (score)

{
case 0-59: cout << "Your grade is F.";break;

case 60-69: cout << "Your grade is D.";break;

case 70-79: cout << "Your grade is C.";break;

case 80-89: cout << "Your grade is B.";break;

case 90-100: cout << "Your grade is A.";break;
}

return 0;
}
Feb 18, 2014 at 8:35am
switch cannot do that. if .. else if .. else if .... else
Feb 18, 2014 at 8:36am
It's better to use if statements for this because switch cases can only have one value each which means you would have to list all the values in the range if you use a switch.
Feb 18, 2014 at 8:55am
Ok so I made this program now.

Its giving correct cout for less than 59. But not for other numbers :/

#include <iostream>

using namespace std;

int main()
{
int score;

cout << "Enter your score.\n";
cin >> score;

if (score <= 59)

{cout << "Your grade is F.";}

else if (score = 60,score <= 69)

{cout << "Your grade is D.";}

else if (score = 70,score <= 79)

{cout << "Your grade is C.";}

else if (score = 80,score <= 89)

{cout << "Your grade is B.";}

else if (score = 90,score <= 100)

{cout << "Your grade is A.";}


return 0;
}
Feb 18, 2014 at 8:57am
closed account (iAk3T05o)
You use || to represent or not ','
1
2
3
4
5
6
7
else if (score >= 60 || score <= 69)
{
}
//not
else if (score >= 60, score <= 69)
{
}

and equality is == not =.
Last edited on Feb 18, 2014 at 9:04am
Feb 18, 2014 at 8:59am
1
2
// else if (score = 60,score <= 69)
else if( score >= 60 && score <= 69 ) 

and likewise for the rest.

With a switch:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int main()
{
    int score;

    std::cout << "Enter your score (0-100): ";
    std::cin >> score;

    char grade = 'F' ;

    switch( score/10 )
    {
        case 6: grade = 'D' ; break ;
        case 7: grade = 'C' ; break ;
        case 8: grade = 'B' ; break ;
        case 9: case 10: grade = 'A' ;
    }

    std::cout << "Your grade is " << grade << '\n' ;
}
Feb 18, 2014 at 9:14am
It worked. I used >= and its working just fine now. Thank you so much for your help, everyone.
Feb 18, 2014 at 9:38am
@JLBorges.

//Just like your I made this one using switch statement. But instead of //telling the grade. It tells the score I put in.

#include <iostream>

using namespace std;

int main()
{
int grade;
int score;

cout << "Enter your score.\n";
cin >> grade;

switch (score/10)

{
case 0: case 1: case 2: case 3: case 4: case 5:grade = 'F';

case 6:grade = 'D';

case 7:grade = 'C';

case 8:grade = 'B';

case 9: case 10:grade = 'A';


}

cout << "Your grade is " << grade << endl;


return 0;
}
Last edited on Feb 18, 2014 at 9:39am
Feb 18, 2014 at 9:39am
Hello talhabhatti5

You can define ranges using 2 points

switch(score)
{
case 0..39: grade = 'D' ; break ;
case 40..59: grade = 'C' ; break ;
case 60..79: grade = 'B' ; break ;
case 80..100: grade = 'A' ; break;
}

In this case your range for the first case is between 0 and 39 both limits included is the same as writing

if (score >= 0) && (score <= 39 )
Feb 18, 2014 at 9:43am
I made it using the method you told. It not working for me. Why is that?

#include <iostream>

using namespace std;

int main()
{
int grade;
int score;

cout << "Enter your score.\n";
cin >> grade;

switch (score)

{
case 0..59:grade = 'F';

case 60..69:grade = 'D';

case 70..79:grade = 'C';

case 80..89:grade = 'B';

case 90..100:grade = 'A';


}

cout << "Your grade is " << grade << endl;


return 0;
}
Feb 18, 2014 at 9:54am
closed account (iAk3T05o)
Use if and else.
Feb 18, 2014 at 10:01am
No. I have already made it with 'if else'

But now I'm trying to make it using a switch. And the method Jecs9 told is not working for me.
Feb 18, 2014 at 10:03am
@Jecs9 What language is that?
Feb 18, 2014 at 10:04am
Hello talhabhatti5

Sorry, That way of using the case is for ST, I got confused, in C/C++ if you want to use a kind of range you would need to define every case, for example

switch(score)
{
case 0:
case 1:
case 2:
case 3:
case 4:
// and so on until 59
grade = 'F';break ;
// then do the same for 60-69

here is an example

switch(grade)
{
case 'A' :
cout << "Excellent!" << endl;
break;
case 'B' :
case 'C' :
cout << "Well done" << endl;
break;
case 'D' :
cout << "You passed" << endl;
break;
case 'F' :
cout << "Better try again" << endl;
break;
}

If you don't type a break; after a case and you state another case, both cases will do the same action, as you can see in case 'B' and case 'C', both will output "Well done" because there is no break after case 'B'.

So the best option is using the if statement as some users told you before

the difference between using if and case is that in if each 'if' will be evaluated one by one, in the case, all the cases are evaluated at the same time, I mean, if you are using "if else if" your code first will evaluate if the first condition is true, then the second, third, and so on, if you use case it will execute the corresponding line so the "switch" statement is faster than "if else if"
Feb 18, 2014 at 12:06pm
the difference between using if and case is that in if each 'if' will be evaluated one by one, in the case, all the cases are evaluated at the same time

Please explain. The switch has a haphazard list of constants that it has to compare the integral value against. These are obviously simpler tests for equality than what the if-clauses might contain. However, the if..else if does not need to evaluate any further than to the first true.

The use of score/10 maps range 60-69 into value 6 due to the nature of integer division.
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// switch
const int val = score/10;
switch ( val )
{
case 5: foo(); break;
case 7: bar(); break;
default: gaz();
}

// equivalent if
const int val = score/10;
if ( 5 == val ) foo();
else if ( 7 == val ) bar();
else gaz();

// direct if 1 (complicated conditions)
if ( 49 < score && score < 60 ) foo();
else if ( 69 < score && score < 80 ) bar();
else gaz();

// direct if 2 (looks worse due to discontinuous "match ranges")
if ( score < 50 ) gaz();
else if ( score < 60 ) foo();
else if ( score < 70 ) gaz();
else if ( score < 80 ) bar();
else gaz();

// direct if 2 with more clear scopes
if ( score < 50 ) gaz();
else
  {
    if ( score < 60 ) foo();
    else
      {
        if ( score < 70 ) gaz();
        else
          {
            if ( score < 80 ) bar();
            else gaz();
          }
      }
  }
Feb 19, 2014 at 3:26pm
Its ok Jesc9 :) And thank you for explaining.

@ keskiverto: This is getting way too complicated for me.I'm gonna stay with my simple program :)
Topic archived. No new replies allowed.