How to Write Switch Statement in C++

Switch statement is use instead of if else condition in programming. If statement is well organized statement which reduce the complexity of code and increase the readability of your program and code. Just assign the condition input in the switch variable and put their related data in specific case.

Source Code 


#include<iostream>
using namespace std;
int main ()
{
 int num=0;
 cout<<" Enter Number Between 0-9 \n ";
 cin>>num;
 switch (num)
 {
 
  case 0:
  cout<<" You press : " << num;
  break;
  case 1:
  cout<<" You press : " << num;
  break;
  case 2:
  cout<<" You press : "<< num;
  break;
  case 3:
  cout<<" You press : "<< num;
  break;
  case 4:
  cout<<" You press : "<< num;
  break;
  case 5:
  cout<<" You press : "<< num;
  break;
  case 6:
  cout<<" You press : "<< num;
  break;
  case 7:
  cout<<" You press : "<< num;
  break;
  case 8:
  cout<<" You press : "<< num;
  break;
  case 9:
  cout<<" You press : "<< num;
  break;
  default :
  cout<<" You Press Something Else !";
}
}

Output of the Program



Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments:

  1. The example is shitty programming. All case statements execute the same line of code. Change it to something like:

    case 0:
    cout << "I see you like 0" << endl;
    break;
    case 1:
    cout << "Ah, you're a 1 type of guy" << endl;
    break;

    Something like that. Basically, different statements.

    ReplyDelete