Breaking News

How to use switch case statement in C++

Switch Case has the same logic as an If Statement, but this is going to be more specific to test a single variable against a list of values.
So if we were together on If Statement this will be a simple lesson for you.

The Syntax for Switch Case.

switch (expression)
{
    case constant-expression:
         statement(s);
         break;
    case constant-expression:
         statement(s);
         break;
    default:
         statement(s);
}

This syntax is a full structure of switch case in C++. Here we have two cases but you may have many cases it depend on the type of question or problem that you solve.

Description of the syntax.

switch (expression). Here the word switch is a keyword and the expression can be a variable or any value that is going to be tested with other values. This expression must not be a type of double or float data type.

case constant-expressionHere the word case is keyword and the constant-expression is a value that will be tested or compared with the above expression. If these two expressions match, it means that, the above expression is equal to the constant-expression, so the answer will be true and it will execute the statements that depends on it until it reach the break statement.

statement(s). This statement can be anything that you want to be executed once the result of case will be true.

break. This is a keyword that is used to terminate the flow of execution when the case  statement return true. This is optional, mean it can be written or not, but if you don't put it on your code, the flow of execution will continue until it reach to the other break statement. This mean that, the below cases statements will not tested anymore instead they will execute their statements until the end of program.

default. This is keyword that is used to execute statement(s) if those above case statements has return false. As we know, it may happen during the comparison of expression the result to be false for those case statements, so here the result of false is like converted to be true and the statement that depend on default statement will be executed. This is like else statement on if statement.
Note: When you write this case statement, the data type of the expression must be equal to the data type of constant-expression.

Now let us have an example that will apply this concept.

Write a program that allow a user to enter his/her grade and then the program should display the result depend on the following criteria. Use switch.
Grade = A – ‘Very Good’
Grade = B – ‘Good’
Grade = C – ‘Bad’

The answer of this question will be written in Dev-C++ Compiler as follow, but you may use any Compiler.

#include <iostream>
using namespace std;
int main()
{
     char grade;
     cout<<"Enter your grade: ";

     cin>>grade;
     switch (grade)
     {
         case 'A':
              cout<<"Very Good";
              break;
         case 'B':
              cout<<"Good";
              break;
         case 'C':
              cout<<"Bad";
              break;    
         default:
              cout<<"The grade is unknown";
     }
}



My input was B as my grade and it will display the following output (see picture below).



But what if someone type a grade out of A,B or C? May be someone type grade M, what is going to happen?

The answer of this question will be "The grade is unknown" because grade M is not same as A or B or C. So according to our code, the result will be "The grade is unknown".


And what if someone type c instead of C? The answer also will be "The grade is unknown". Why, because here the program try to match these to expression, if they match or they are equal, the statement will executed otherwise, it will not executed.

So how can I write a program that will take both small and capitals letters? The answer is to add another case below of each case and put a small letter as a constant-expression. See the following picture.




We are done...!!!

I hope now you understand how to use Switch Case in C++. Feel free to ask any question if you didn't understand then I can reply as faster as I can. 

Please comment and share with your friends. Don't miss the following post where we will continue to learn different things and application of new concepts.

Thank you for reading, be blessed....!!!

3 comments:

  1. Nice explanation! Thank you!

    ReplyDelete
  2. You are welcome! Feel free to ask anything concerning with topic!

    ReplyDelete