Search code examples
c++switch-statementaverage

How to use IF in Switch statement


How to apply if condition in Switch statement, I want to calculate Average: however I have tried for best to solve the issue but still getting no output from Switch statement. I am beginner to the C++,

#include <iostream>

using namespace std;

int main()
{
    //declaration of variables.
    int sub1,sub2,sub3, sub4,sub5,total,avg;

    //accepting marks from user in each subject
    cout << " Enter Programming in C++ Marks: " << endl;
    cin >> sub1;
    cout << " Enter Software Engineering Marks : " << endl;
    cin >> sub2;
    cout << " Enter Personal Communication Marks : " << endl;
    cin >> sub3;
    cout << " Enter Database Application Marks: " << endl;
    cin >> sub4;
    cout << " Enter Computer Concept Marks: " << endl;
    cin >> sub5;

    //calculatin sum of marks obtained in each subject
    total = (sub1 + sub2 + sub3 + sub4 + sub5);

    //calculating the average marks
    avg = total / 5;

    // starting of if condition for finding out grades of total subjects.

    switch (avg){
      case 1:{
        if ((avg >= 80) && (avg <= 100))
        {
            cout << " Your Average is: " << avg << endl;
            cout << "You Grade is A+ " << endl;
        }
        break; 
      }
      case 2:{
        if ((avg >= 70) && (avg <= 79))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your grade is A " << endl;
        }    
        break;
      } 
      case 3:{
        if ((avg >= 60) && (avg <= 69))
        { 
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is B+  " << endl;
        }   
        break;  
      }
      case 4:{
        if ((avg >= 50) && (avg <= 59))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is C+ " << endl;
        }
        break;
      }
      case 5: {
        if ((avg >= 40) && (avg <= 49))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  C- ! " << endl;
        }
        break;    
      }
      case 6: {
        if ((avg >= 30) && (avg <= 39))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  D ! " << endl;
        }
        break; 
      }

      default:{ 
        if ((avg >= 100) && (avg <= 29))
        {
            cout << " Your Average is: " << avg << endl;
            cout << " Your Grade is  F, So you are Fail in the class ! " << endl;
            break;
        }
      }
    }

    system("pause");
}

Solution

  • The switch statement is used to execute one block of code dependent on a particular value. In a sense, the switch statement can be thought of as a form of an if statement: the code

    switch (avg) {
       case 1 : { /* code block 1 */ } break;
       case 2 : { /* code block 2 */ } break;
       default : { /* code block default */ } break;
    }
    

    can be read as

    if (1 == avg) { /* code block 1 */ }
    else if (2 == avg) { /* code block 2 */ }
    else { /*code block default */ }
    

    Your switch statement can be read as

    if (1 == avg) 
    {
       if ((avg >= 80) && (avg <= 100))
       {
          cout << " Your Average is: " << avg << endl;
          cout <<  "You Grade is A+ " << endl;
       }
    } else if...
    

    and there's no way avg can == 1 and be greater than 80 and less than 100, which is why you don't get any output.

    In C++, the switch statement doesn't lend itself well to testing for ranges; I'd just use an if statement:

    if ( (avg<=100) && (avg >=80))
    {
       // you get an A
    } else if ...
    

    But, if you really really need to use a switch, there are a few ways to go about it:

    switch (avg) {
       case 100:
       case 99:
       case 98:
       ...
       case 80 : { /* you get an A+ */ break; }
       case 79 :
       case 78 : 
       ...
       case 70 : { /* you get a A */ break: }
    etc.
    

    This is ugly, but it is a switch statement. A cleaner way to use the switch statement could be to "force" avg to a discrete value, instead of a range.

    int percentile = avg / 10;  // integer division. 
    

    Now range is going to be between 0 and 10, assuming avg is between 0 and 100. This will make for a slightly cleaner switch:

    switch (percentile) { 
       case 10 :
       case 9:
       case 8: /* You get an A+ */ break;
       case 7: /* You get an A */ break;
       case 6: /* You get a B+ */ break;
    
    etc.
    

    Hope this is helpful.