Switch Statement in C: Syntax and Examples

Switch Statement in C: Syntax and Examples

The switch statement is one of the most basic decision-making concepts in the C programming language. Switch statement is a control flow component that facilitates multi-way branching according to an expression's value. In C, the switch statement is composed of the switch keyword, an expression enclosed in parenthesis, and several case labels that specify potential values. C++ (CPP) also makes use of this statement for comparable reasons. You can study examples showing how switch statements are used in real-world C applications to gain a better understanding.

In this C tutorial, we'll explore what makes switch statements so versatile and practical, from how they're set up to how they can be used effectively.

What is the Switch Statement in C?

In the previous conditional statements tutorial we saw the if else-if ladder to check multiple conditions if one or the other fails. The switch statement serves as an alternative to the if else-if ladder. It checks multiple cases for different values of a single variable.

switch statement in c programming

Syntax

The syntax of the switch statement in C is as follows:

Rules for the switch statement in C

  1. The data type of the switch expression must be of an integer or character .
  2. All the case labels must be followed by a colon “ : ”.
  3. The case the value must be an integer or character constant.
  4. The break keyword is optional, but it can decrease the execution time of your program. It will stop the unnecessary execution of other cases after a match is found.

Note: We will learn break statement in detail in the section, Break and Continue Statements in C

Example

The above code in the C Compiler checks if the number is 10 or is it 50 or is it 100. If none of the given numbers exist, the default the statement gets printed.

Output

number is equal to 

There can be several switch statements inside a switch statement. These are called nested switch statements.

Syntax

Example

    < ,i); ,j); > > , i ); , j );

Output

the value of i evaluated in the outer In conclusion, switch statements in C provide a useful alternative to the traditional “ if ” statement for making decisions within your code. Not only are they more efficient and precise, but they also provide many advantages. As you continue learning about the intricacies of switch statements in C, remember the tremendous power it gives you not only to control what is inside your program’s box of instructions but also how best to apply those instructions for maximum results. C Online Course Certification will help you more in your understanding of switch statements and other essential concepts.

FAQs

Q1. What are the data types of switch statement in C?

The switch expression must be of an integer or character type. The case value must be an integer or character constant. The case value can be used only inside the switch statement. The break statement in switch case is not a must.

Q2. What is allowed in a switch case?

The value of the expressions in a switch-case statement must be an ordinal type i.e. integer, char, short, long, etc.

Q3. Can we return in a switch case?

Switch statements can only contain return statements if they are within an object that accepts a callback. T