How we can use switch statement in c++ code


It is very easy to know how we can use switch condition in c++. First we use switch key word and then use small brackets and give a character in between the brackets. Then use curly brackets and write the cases and the statements which we want to output etc. You can easily understand the use of switch condition in c++ by watching code.

Code:


#include <iostream>
#include <string>
using namespace std;
int main() {
    int num;
    cout<<"Enter a number : ";
    cin>>num;
    switch (num)
    {
                case 1:
                cout<<"How are you.";
                break;
                case 2:
                cout<<"Hello Friends.";
                break;
                case 3:
                cout<<"Hay how are you.";
                break;
                default:
                cout<<"I am fine.";
                break;
                }
}

Output:


In case of 1 the output will be

Enter a number : 1
How are you.

In case of 2 the output will be

Enter a number : 2
Hello Friends.

In case of 3 the output will be

Enter a number : 3
Hay how are you.

In any other case the output will be

Enter a number : 4
I am fine.


Click here for watching the video of that code

avatar
AUTHOR: Muhammad Abbas
I am Software Engineer.

0 Comments