example of switch case in c++

"C++ Switch Case Example"

By
AD

-switch case is a control structure in C++ that allows the program to branch based on a given expression. It's often used to test for a condition by trying out different expressions. Here's a simple example:

Consider a program that prints the day of the week based on a given number. The numbers from 1 to 7 can represent the days: 1 for Sunday, 2 for Monday, and so on. A switch statement can neatly handle this:

Basic Example of Switch Case in C++

The following C++ code snippet demonstrates the use of a switch case:

AD
switch case statement in C language - Codeforcoding
switch case statement in C language - Codeforcoding
Javascript switch case example
Javascript switch case example
Switch Statement in Java
Switch Statement in Java

int dayNumber = 5;
switch (dayNumber) {
case 1:
cout << "Sunday"
; break;
case 2:
cout << "Monday"
; break;
// ... and so on for the rest of the week
}

Day Representation in Switch Case

The switch statement in this example `switch (dayNumber)` tests the value of `dayNumber`. If `dayNumber` is 1, it executes the associated code `cout << "Sunday"`. If it's 2, it executes `cout << "Monday"`, and so on.

Arduino - switch case statement
Arduino - switch case statement
Switch Cases Template (Flowchart)
Switch Cases Template (Flowchart)
an image of a programming program with the words, 5 switch case and instructions to use it
an image of a programming program with the words, 5 switch case and instructions to use it

Each `case` label specifies a value the expression may have. If any of these values match, the associated block of code is executed. When a match is found, the statement `break;` is used to exit the switch statement, preventing the `default` case from being executed if present.

Using Switch Case with a String

In C++17 and later, switch cases can also use strings, which increases their versatility. Here's an example:

AD
string dayName = "Monday"; switch (dayName) { case "Monday": cout << "Start of the week"
; break; case "Friday": cout << "End of the week"
; break; default: cout << "Some other day"<
; }

The `default` case is optional and executes when no case matches the switch's expression. In this example, it catches all days except Monday and Friday.

a diagram showing the different types of switches and connections in an internet network, with blue lights
a diagram showing the different types of switches and connections in an internet network, with blue lights
Automatic Changeover Switch
Automatic Changeover Switch
Types of Switches Module in Indian Market
Types of Switches Module in Indian Market
an open wall mounted switch box with two switches and one light on the inside side
an open wall mounted switch box with two switches and one light on the inside side
a diagram showing how to understand the value of investment
a diagram showing how to understand the value of investment
Switch
Switch

Switch Case with Ranged Values

Beginning with C++20, switch statements have gained support for the `ranges` feature, enabling more compact and readable code. Here's an example:

int grade = 84;
switch (grade / 10) {
case 9: case 10:
cout << "Excellent"
; break;
case 8:
cout << "Good"
; break;
default:
cout << "Needs improvement"
; break;
}

AD

In this code, `case 9` and `case 10` are combined using the fallthrough behavior, so if `grade` is between 85 and 100, it will print "Excellent". This example showcases how `switch` can efficiently handle multi-range values.

Bear Picnic Switch Case Set for Nintendo Switch 2/Switch OLED/Switch, Green Meadow Protective Shell and Travel Case
Bear Picnic Switch Case Set for Nintendo Switch 2/Switch OLED/Switch, Green Meadow Protective Shell and Travel Case
the managed network switch functions are displayed in this graphic above it is an image of several different types of switches
the managed network switch functions are displayed in this graphic above it is an image of several different types of switches
the basic switch configuration diagram for an internet device, with instructions to install and configur
the basic switch configuration diagram for an internet device, with instructions to install and configur
an info sheet describing how to use switching
an info sheet describing how to use switching
a diagram showing the different types of switching and how they are used to connect
a diagram showing the different types of switching and how they are used to connect
\
\
the different types of switches are shown in this diagram, with instructions on how to use
the different types of switches are shown in this diagram, with instructions on how to use
Kawaii Lucky Cat Switch OLED Case, Clear Protective Cover with Pink Bow Design
Kawaii Lucky Cat Switch OLED Case, Clear Protective Cover with Pink Bow Design
GZCOM PoE Switch
GZCOM PoE Switch
an image of a switch box with the words cisco switches above it
an image of a switch box with the words cisco switches above it
the types of switches in networked devices are shown on a piece of lined paper
the types of switches in networked devices are shown on a piece of lined paper
cara kerja switch setelah broadcast
cara kerja switch setelah broadcast

Nested Switch Case

Switch statements can also be nested inside other switch statements. This can lead to complex but manageable code,especially when dealing with multiple, intertwined conditions. Here's a simple example:

string month = "February"; int daysInMonth = (month == "February") ? (leapYear ? 29 : 28) : // Some other month switch (daysInMonth) { case 30: case 31: cout << "This month has 30 or 31 days."
; break; default: cout << "This month has less than 30 days."
; }

The outer `switch` uses the number of days in a month to decide the output, while the inner `if`-`else` statement with another `switch` handles the special case of February, considering leap years.

AD

In conclusion, switch case is a powerful control structure in C++ that can significantly simplify your code, making it easier to write and maintain. Experiment with different scenarios to fully grasp its capabilities. Happy coding!

AD