C and C++ are consistently listed at the top of lists of popular programming languages. C++ is one of the most powerful programming languages which is suitable for a wide variety of uses. It shares a history with C and as a result many C constructs and code also work in C++. You can generally compile C code with several different C and C++ compilers. One of the most used flow control operators in C++ is the switch()
statement. The switch-case statement can be used in both C language and C++. In this post, we will explain how to use switch statements in C and C++ along with some switch case C++ and C examples too.
Table of Contents
How to use switch statement in C++ and C programming?
When developing an application, another one of the most used constructs in C is the switch()
statement. A switch
statement allows you to check a variable and then branch to different blocks of code (cases) in accordance with the variable’s value. Each case can be defined for different values and the variable being switched on is checked for each case’s condition being fulfilled. It is similar to an if..then
statement only more powerful and easier to read when there are a lot of conditions to check.
When the switch expression is evaluated once, the value of the given expression is compared with the values of each listed case. If it see there is a match, the associated block of code in that case statement is executed till the break. There is also a special type of case called default
. The default
case is optional and can be defined at the end of case list. The default
case is executed if none of the other conditions in the case statement are triggered. The switch()
function is used with a variable (generally it is an integer number) like this below,
1 2 3 4 5 6 |
switch( <switch variable> ) { // list cases here } |
To begin each case
statement block you must start with case <constant expression>:
and end with break;
like so:
1 2 3 4 |
case <constant expression> : break; |
The cases are grouped inside { } brackets, at the end we can use optional default:
case.
What is the syntax of the switch statement in C++ and C programming?
Here is the syntax for the switch statement,
1 2 3 4 5 6 7 8 9 10 11 |
switch ( <switch variable> ) { case <constant expression> : <statement>; break; default : <statement>; } |
How to use switch in C programming?
We can use switch statement in C and C++ programming languages. For example we can switch and case together as a selector function for an integer i value as below,
1 2 3 4 5 6 7 8 9 10 11 12 |
switch(i) { case 1: // do things when i = 1 break; case 2: // do things when i = 2 break; } |
For example, you can indicate condition of items by numbers, in this example below, we have 3 conditions Bad, Normal. Let’s set condition =3 and see how switch() function works in a C example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
int condition = 3; switch(condition) { case 1: printf("Bad"); break; case 2: printf("Normal"); break; case 3: printf("Good"); break; } |
How to use switch in C++ programming?
We can use switch statement in C and C++ programming languages in pretty much the same way. We can also add all other numbers with default:
case statement in C and C++ languages. Here is a C++ example below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
int condition = 3; switch(condition) { case 1: std::cout << "Bad"; break; case 2: std::cout << "Normal"; break; case 3: std::cout << "Good"; break; default: std::cout << "Unknown"; break; } |
This code will output “Unknown” if the value of condition is 0 or below 0 or greater than 3. You can add many code lines between case and break;
Is there a full example about how to use switch in C programming?
Here is a full C example for the switch statement,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <stdio.h> inf main() { int condition = 3; switch(condition) { case 1: printf("Bad"); break; case 2: printf("Normal"); break; case 3: printf("Good"); break; default: printf("Unknown"); break; } return 0; } |
Is there a full example about how to use switch in C++ programming?
Here is a full C++ example for the switch statement,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> int main() { int condition = 3; switch(condition) { case 1: std::cout << "Bad"; break; case 2: std::cout << "Normal"; break; case 3: std::cout << "Good"; break; default: std::cout << "Unknown"; break; } return 0; } |
Although the free C++ Builder Community Edition is extremely powerful it is intended for students, beginners, and startups. If you are a regular business or do not qualify for the free community edition, then you can download a free trial of the very latest full RAD Studio C++ Builder version.
C++ Builder is the easiest and fastest C and C++ IDE for building everything from simple to professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder an
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition