By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

switch

P: 2
can any one help me
to print students grade using switch in 'c'
if(marks>80)then print very good
if(60<marks<80) good ......
how can i write the code using switch,where can i specify the condition
Oct 4 '06 #1
Share this Question
Share on Google+
7 Replies


Banfa
Expert Mod 5K+
P: 8,916
You can't, switches do not do that sort of comparison they can only perform the == comparison so unless you can reduce your problem to the point where you only require == you can't use a switch.
Oct 4 '06 #2

P: 30
As Banfa told you switch is used only for == operation.

But if you want to use only switch then you have to write all the condition. which will be tooooo long.
like........

switch (i)
{

case 60:
case 61:
|
|
|
case 80: (follow this condition)
break:
case 81:
|
|
case 100: (follow this condition)
break;
default:
(follow this conditoin)
Oct 4 '06 #3

10K+
P: 13,264
As Banfa told you switch is used only for == operation.

But if you want to use only switch then you have to write all the condition. which will be tooooo long.
like........

switch (i)
{

case 60:
case 61:
|
|
|
case 80: (follow this condition)
break:
case 81:
|
|
case 100: (follow this condition)
break;
default:
(follow this conditoin)

you could still use the switch by transforming a bit

int a;
if 0 < grade 50 a = 1
else if 50 < grade < 70 a = 2
else if etc

then
switch(a) etc
Oct 4 '06 #4

100+
P: 293
D_C
Where are the divisions? Are they all equally spaced?

Suppose A = 90-100, B = 80-89, C = 70-79, D=60-69,F < 60
Expand|Select|Wrap|Line Numbers
  1. static const int GRADE_RANGE = 10;
  2. if((grade >= 0) && (grade <= 100))
  3. {
  4.   grade /= GRADE_RANGE;
  5.   switch(grade)
  6.   {
  7.    case 10:
  8.     case 9:  cout << "A" << endl;
  9.              break;
  10.     case 8:  cout << "B" << endl;
  11.              break;
  12.     case 7:  cout << "C" << endl;
  13.              break;
  14.     case 6:  cout << "D" << endl;
  15.              break;
  16.    default:  cout << "F" << endl;
  17.              break;
  18.   }
  19. }
Oct 4 '06 #5

10K+
P: 13,264
Where are the divisions? Are they all equally spaced?

Suppose A = 90-100, B = 80-89, C = 70-79, D=60-69,F < 60
Expand|Select|Wrap|Line Numbers
  1. static const int GRADE_RANGE = 10;
  2. if((grade >= 0) && (grade <= 100))
  3. {
  4.   grade /= GRADE_RANGE;
  5.   switch(grade)
  6.   {
  7.    case 10:
  8.     case 9:  cout << "A" << endl;
  9.              break;
  10.     case 8:  cout << "B" << endl;
  11.              break;
  12.     case 7:  cout << "C" << endl;
  13.              break;
  14.     case 6:  cout << "D" << endl;
  15.              break;
  16.    default:  cout << "F" << endl;
  17.              break;
  18.   }
  19. }
Wouldn't work for uneven ranges but that's a neater way of doing it
Oct 4 '06 #6

Banfa
Expert Mod 5K+
P: 8,916
Personally in this case I would favour an

if (...)
else if (...)
else

construct
Oct 4 '06 #7

P: 2
Personally in this case I would favour an

if (...)
else if (...)
else

construct
thnak u very much for ur guidance,iam new to c programmingand a student,i was not expecting reply,but ur replies really encourageed me a lot,once again thank u very much to all of u.
Oct 5 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.