switch

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shireesh
    New Member
    • Oct 2006
    • 2

    switch

    can any one help me
    to print students grade using switch in 'c'
    if(marks>80)the n print very good
    if(60<marks<80) good ......
    how can i write the code using switch,where can i specify the condition
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    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.

    Comment

    • sanketbarot
      New Member
      • Sep 2006
      • 30

      #3
      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)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by sanketbarot
        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

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          Where are the divisions? Are they all equally spaced?

          Suppose A = 90-100, B = 80-89, C = 70-79, D=60-69,F < 60
          Code:
          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;
            }
          }

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by 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
            Code:
            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

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Personally in this case I would favour an

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

              construct

              Comment

              • shireesh
                New Member
                • Oct 2006
                • 2

                #8
                Originally posted by Banfa
                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.

                Comment

                Working...