CC++C++17Introduction to C++Learn C++Syntax

How To Use Switch In C++ And C Programming?

How To Use Switch In C++ And C Programming

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.

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,

To begin each case statement block you must start with case <constant expression>: and end with break; like so:

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,

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,

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.

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.

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,

Is there a full example about how to use switch in C++ programming?

Here is a full C++ example for the switch statement,

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?