Home / Computer Courses / Switch Structure In C++ Language

Switch Structure In C++ Language

Switch Structure In C++ Language

Switch Structure:

The switch structure is another conditional structure. It is the alternative way of nested ‘if-else’ structure. Through this structure, we can easily select one option if many choices are available. But this situation is difficult to implement in nested ‘if-else’ structure.

switch structure

Syntax:

switch(expression){
   case 1:         statements;            break;

   case 2:        statements;             break;

   case n:        statements;             break;

   default:      statements;

}

Explanation of syntax:

  • switch:                 switch uses to compare an expression with all cases.
  • expression:       expression is a value that must be integer or character. e.g. expression can 7 or ‘7’.
  • case:                      In the switch structure, every case shows one choice.
  • statements:       statements are the block of code that will execute if any case will true.
  • break:                   break is a keyword that used to exit from anybody. Here, it used to exit from the switch. If we cannot use the break at the end of every case functionality than compiler will check all the cases.

Here are the examples for you for better practice.

Example 1:

include <iostream>

using namespace std;

int main()

{

int num;

cout<<”enter a number”;

cin>>num;

switch (num) {

case 1:

printf(“you choose 1”);

break;

case 2:

printf(“you choose 2”);

break;

case 3:

printf(“you choose 3”);

break;

default:

printf(“you choose other than 1, 2 and 3”);

break;

return 0;

}

Example 2:

#include<iostream>

using namespace std;

int main(){

char grade = ‘A’;

switch(grade){

case ‘A’:

case ‘a’:

cout<<“Excellent!”<< endl;

break;

case ‘B’:

case ‘b’:

cout<<“Well done”<<endl;

break;

case ‘C’:

case ‘c’:

cout << “good” << endl;

break;

case ‘D’:

case ‘d’:

cout<<“You passed”<<endl;

break;

case ‘F’:

case ‘f’:

cout<<“oooh you are failed”<<endl;

break;

default:

cout<<“Invalid grade”<<endl;

}

return 0;

}

Example 3:

include <iostream>

using namespace std;

int main()

{

char sign;

float num1, num2;

cout << “Enter values like 3+5:\n”;

cin >> num1 >> sign >> num2;

switch(sign)

{

case ‘+’:

cout << num1+num2;

break;

case ‘-‘:

cout << num1-num2;

break;

case ‘*’:

cout << num1*num2;

break;

case ‘/’:

cout << num1/num2;

break;

default:

cout << “Error! operator is not correct”;

break;

}

return 0;

}

If you face any problem or query then contact us.

We have the collection of other topics of the C++ course and technology updates. Also, like our facebook page.

About Shumaiza Asghar

Shumaiza Asghar

Check Also

Blogger Website

How To Make Blogger Website and Earn Money | Basic Introduction

Contents 1 How To Make Blogger Website and Earn Money | Basic Introduction1.1 What is …

Leave a Reply

Your email address will not be published. Required fields are marked *