Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8256539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:53:18+00:00 2026-06-08T01:53:18+00:00

How to apply if condition in Switch statement, I want to calculate Average: however

  • 0

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");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T01:53:20+00:00Added an answer on June 8, 2026 at 1:53 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List<MediaRef> and i want to apply Take and Where condition on
I want to apply texture to the transparent png(bitmap), but I don't want to
I want to apply a rewrite condition, so that only *.php files are affected:
So, I want to apply a WHERE condition to a field assigned by a
I have a list and I want to apply a logical test to each
I want to apply a condition, after I passed another condition like this: List<Entity>
i am trying to apply some condition for an Auto generated textfield, the script
The apply phase of save may fail and/or is still being done asynchronously before
I need to apply tint effect on image in Android . But the tint
I wanted to apply style only to the myPage id. But I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.