C++ Tutorial: Understanding Switch Statements with Codes and Examples in 2023

Article by: Manish Methani

Last Updated: October 21, 2021 at 8:04am IST
3 min 36 sec read

In C++, a switch statement is a useful tool for controlling program flow based on the value of a single variable. It allows you to evaluate a variable against a list of possible values and execute different code blocks depending on the result. In this tutorial, we'll explore the use of switch statements in C++ and provide examples to help you understand how they work.

Syntax of Switch Statements in C++

The syntax for a switch statement in C++ is as follows:

switch(variable) {
  case value1:
    // code block 1
    break;
  case value2:
    // code block 2
    break;
  case value3:
    // code block 3
    break;
  default:
    // default code block
}

Where "variable" is the variable being evaluated, "value1", "value2", and "value3" are the possible values the variable can take, and the "default" code block is executed if none of the other cases are true. It's important to note that each case must end with a "break" statement to avoid falling through to the next case.

Creating Switch Statements

To create a switch statement, you must first define the variable being evaluated. For example:

int main() {
  int day = 3;
  switch(day) {
    case 1:
      cout << "Monday";
      break;
    case 2:
      cout << "Tuesday";
      break;
    case 3:
      cout << "Wednesday";
      break;
    default:
      cout << "Invalid day";
  }
  return 0;
}

In this example, we create a switch statement that evaluates the value of the variable "day". Depending on the value of "day", the program will output the corresponding day of the week. If the value of "day" is not one of the expected values, the program will output "Invalid day".

Using Switch Statements in C++ Programs

Switch statements can be a powerful tool for controlling program flow in C++ programming. They are particularly useful when evaluating a single variable against a list of possible values. One common use case for switch statements is in creating menu-driven programs, as in the following example:

int main() {
  int choice;
  cout << "Choose an option:
1. Option 1
2. Option 2
3. Option 3
";
  cin >> choice;
  switch(choice) {
    case 1:
      cout << "Option 1 selected";
      break;
    case 2:
      cout << "Option 2 selected";
      break;
    case 3:
      cout << "Option 3 selected";
      break;
    default:
      cout << "Invalid option";
  }
  return 0;
}

In this example, we create a switch statement that evaluates the user's input and executes a different code block depending on the chosen option. If the user enters an invalid option, the program will output "Invalid option".

Conclusion

Switch statements are an important tool for controlling program flow in C++ programming. By understanding how switch statements work and when to use them, you can write more efficient and readable code in your C++ programs. Whether you're a beginner or an experienced programmer, this tutorial is a must-read for mastering switch statements in C++.

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>