switch Statement in C++

switch Statement in C++

The switch statement in the C++ programming language provides C++ users with a single choice among multiple selections within a control structure. This allows C++ users to execute a single block of program code from multiple available blocks based on the value of a user-defined variable or condition expression. The switch statement is most commonly used in C++ when there are multiple possible value choices for a user-defined program variable, and the user wishes to execute and display individual blocks of program source code based on the value of a variable.

switch Statement in C++

So, let’s explore the switch statement in C++ programming.

Remember, unlike multiple if-else statements in C++, the switch statement is generally more useful for analyzing the value of a single program variable with and against multiple possible choices.

Syntax of the switch statement in C++.

switch (condition/Expression) {

case choice1:

// Here it runs program code to execute if the condition == choice1

break;

case choice2:

// Here it runs program code to execute if the condition == choice2

break;

case choice3:

// Here it runs program code to execute if the condition == choice3

break;

// Similarly here you can have as many other cases as you like create here

Default:

// Here it runs program code to execute if none of the above cases match

break;

}

Explanation of switch statement in C++.

  • condition/expression – Here in the switch statement, expression is the user-defined value or variable that is being analyzed or tested in the current switch statement.
  • case valueN – This is the user-defined potential value in each case statement. The switch can accept user case values ​​or expressions as input. If the switch expression you define matches the valueN, the switch will execute exactly. Then the corresponding block of current program source code is automatically executed.
  • break – A switch statement exits by breaking out of the switch statement after the case statement in the user expression is executed. Without it, the program would continue executing subsequent case statements even if they don’t match.
  • default – A switch statement is an optional choice or default block of code in a program that runs or executes when none of the above case expressions match the value.

Example of a basic C++ switch statement.

#include <iostream>

using namespace std;

int main() {

int laptop = 4;

switch (laptop) {

case 1:

cout << “Macbook Pro” << endl;

break;

case 2:

cout << “Hp Pavilion” << endl;

break;

case 3:

cout << “Dell Vostro” << endl;

break;

case 4:

cout << “Asus Tuf” << endl;

break;

case 5:

cout << “Acer aspire” << endl;

break;

case 6:

cout << “Surface” << endl;

break;

case 7:

cout << “Pixelbook” << endl;

break;

default:

cout << “Wrong selection” << endl;

break;

}

return 0;

}

Explanation of a basic C++ switch statement.

  • In this example, since the value of laptop is user-defined as 4, the corresponding case block in case 4 will be executed in the current program and the statement “Asus Tuf” will be printed.
  • Using the break keyword in this sequence ensures that the program does not execute subsequent case statements after matching a certain number of individual case choices.

Fall-through statement in C++ without the break keyword.

If a C++ user ignores the break keyword in a switch statement, the program will continue executing the next number of switch cases until it encounters a break keyword termination point or reaches the last statement of the switch statement. This is known as fall-through behavior in a switch statement.

Example of a fall-through statement in C++.

#include <iostream>

using namespace std;

int main() {

int course = 1;

switch (course) {

case 1:

cout << “Ai Automation” << endl;

case 2:

cout << “Ai Agent” << endl; // execute this statement here because no break key is defined here

case 3:

cout << “Machine Learning ” << endl; // execute this statement here because no break key is defined here

case 4:

cout << “Robotics” << endl; //here this statement also execute, and break next case statement with break keyword

break;

case 5:

cout << “Dev Ops Development” << endl;

break;

default:

cout << “Wrong course choice” << endl;

}

return 0;

}

Explanation of a fall-through break switch statement.

  • In this example, where the value course1 is user-defined, it executes the block until a break keyword statement is found in case 1, and continues printing statements until case 4. Since there is no break keyword after case 1, it prints statements like “Ai Automation”, “Ai Agent”, “Machine Learnings”, and “Robotics” in the current program, in that order.
  • To avoid fall-through conditions in switch statements, you should always ensure that C++ users use the break keyword after every case.

Using the Default Statement in C++.

The default case statement is an optional choice in a C++ switch, and the default case can be used to handle or manage switch statement situations where none of the case values ​​in the user-defined switch statement exactly or properly match the condition expression. And it behaves like an else block in an if-else chain.

Example of the default statement in C++.

#include <iostream>

using namespace std;

int main() {

int ev = 101;

switch(ev){

case 1:

cout << “Tata Punch EV” << endl;

break;

case 2:

cout << “Mahindra BE 6” << endl;

break;

case 3:

cout << “Tata Nexon” << endl;

break;

default:

cout << “No EV selection from above choice” << endl;

break;

}

return 0;

}

Explanation of the default statement in C++.

  • In this example, the user-defined value ev does not exactly match either Case 1, Case 2, or Case 3. This causes the default block in the current program to execute, printing the statement “No EV selection from above” to the console.

Using Multiple Case Values ​​Concept in C++.

C++ users can group and display multiple values ​​in a single case statement. This allows C++ users to run a single block of program code for multiple individual case values.

Example of using multiple case values ​​in C++.

#include <iostream>

using namespace std;

int main() {

char course = ‘P’;

switch (course) {

case ‘J’:

case ‘M’:

case ‘P’:

cout << “You are eligible for Java, Matlab, Python, Course” << endl;

break;

case ‘C’:

case ‘S’:

cout << “You are not eligible for a C or Swift course” << endl;

break;

default:

cout << “wrong course selection” << endl;

}

return 0;

}

Explanation of the Multiple Case Values ​​in C++.

  • In this example, if the user-defined course selection is ‘J’, ‘M’, or ‘P’, it will print the statement “You are eligible for Java, Matlab, Python, Course.”
  • If the user-defined course selection is ‘C’ or ‘S’, it will print the statement “You are not eligible for a C or Swift course.”

Switch statement concept with enum data type.

The switch statement in C++ programs can also be used with the enum data type, allowing multiple individual case statements to be handled based on the enum data type value.

Switch example with enum data type.

#include <iostream>

using namespace std;

enum Country { INDIA, AMERICA, CHINA, EUROPE };

int main() {

Country country = INDIA;

switch (country) {

case INDIA:

cout << “It represents India” << endl;

break;

case AMERICA:

cout << “It represents America” ​​<< endl;

break;

case CHINA:

cout << “It represents China” << endl;

break;

case EUROPE:

cout << “It represents Europe” << endl;

break;

default:

cout << “Wrong country selection” << endl;

}

return 0;

}

Explanation of the enum data type in C++.

  • Here in this example, as Country country = INDIA is the user defined case value, hence it prints the statement “It represents India” in the current program.

Leave a Reply