446,305 Members | 1,588 Online 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
7 Replies

 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 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 static const int GRADE_RANGE = 10; if((grade >= 0) && (grade <= 100)) {   grade /= GRADE_RANGE;   switch(grade)   {    case 10:     case 9:  cout << "A" << endl;              break;     case 8:  cout << "B" << endl;              break;     case 7:  cout << "C" << endl;              break;     case 6:  cout << "D" << endl;              break;    default:  cout << "F" << endl;              break;   } } 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 static const int GRADE_RANGE = 10; if((grade >= 0) && (grade <= 100)) {   grade /= GRADE_RANGE;   switch(grade)   {    case 10:     case 9:  cout << "A" << endl;              break;     case 8:  cout << "B" << endl;              break;     case 7:  cout << "C" << endl;              break;     case 6:  cout << "D" << endl;              break;    default:  cout << "F" << endl;              break;   } } Wouldn't work for uneven ranges but that's a neater way of doing it Oct 4 '06 #6

 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 