Switch Statement in C

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

The Switch Statement in C is another C programming language's decision-making statement. The Switch statement in the C programming language can execute statements from a wide array of possible choices based on a provided condition. You can't perform this with the if-else statement of the C programming language, which lets you choose only between two possibilities based on a given condition. The creators of the C programming developed the Switch statement for the situations where the if-else statement was not that efficient and took a toll on the readability of the code.

Scope of Article

In this article, you will learn about the things listed below:

  • What is the Switch statement in C.
  • Switch statement's syntax.
  • The working of the Switch statement.
  • Rules to be followed while using the Switch statement in C.
  • Important facts about the Switch statement in C.

What is Switch Statement in C?

The Switch statement of the C programming language is one of the decision-making statements of the C programming language. You might already be familiar with the if-else statement of the C programming language, another decision-making statement of the C programming language.

One might raise the question,

  • Why are there two decision-making statements in the C programming language?
  • What is the difference between them?
  • When to use the if-else statement and when to use the switch statement?

The answer is quite simple,

There are two decision-making statements because they both perform well in different situations and are necessary at times. You will understand the difference in this article. Once you clearly understand that, you will be able to say which decision-making statement to use in which situation with absolute certainty.

Let's look at a simple example to understand its necessity from a real-world perspective.

what is switch statement in c

I firmly believe that you know how the prize wheel game works (A picture of the prize wheel is given above for your reference). The wheel will be divided into segments, each holding a prize, and there will also be a pointer. The game master will rotate the wheel, and when it comes to a stop, the pointer will tell you the prize you get to take home. Now, imagine you have been asked to create a program, you will be given the color as the input, and you will have to print the prize the user has won.

If you choose to use the if-else statement, think about how many if-else statements you will have to write. You will have to write a condition to check the given color the user input with all the available colors in the wheel. You will be writing the same statement more than once, i.e., once for every color. It is an awful amount of code to write and hard for anyone to understand.

I hope you understand the trouble with using an if-else statement in a situation like this. The Switch statement of the C Programming language was designed specifically for these situations where the if-else statement would unnecessarily increase the length of the code and affect the readability of the code. The switch statement takes an expression, evaluates it, and, based on the result of the expression, executes a set of instructions from a wide array of possible choices.

Let's understand more about the Switch statement of the C programming language, starting with its syntax.

Syntax of the Switch Statement in C Programming Language.

The expression in the syntax can be Arithmetic, Relational or Logical expression. Still, the condition is that the programmer should provide an expression that always returns a single value as its result, and the result should not be a float or double. The values defined in the case statements should always be constants; you can't use a variable with a case statement. The break and the default statements are both optional. The function that both the statements perform and their need is explained in the later section: Working of the Switch statement in C programming language.

Now that you know the syntax of the switch statement of the C programming language, let's continue with understanding the working of the Switch statement.

How Does the Switch Statement Work?

The working of the Switch statement of the C Programming language is as follows:

The compiler will first evaluate the expression provided with the Switch statement. The result of the expression is then sequentially checked with all the values defined in the case statements until the compiler finds a perfect match for the result. And when it does find a perfect match, The compiler will transfer the control to the following line, i.e., the line after the case statements, which holds the same value as a result. The program control will not just exit the switch statement once it executes all the statements defined under the case statement but will continue to execute them.

But you would want the control to exit from the switch statement after executing all the lines defined within a single case statement. So that is why you have to end every case statement with a break statement. The break statement will transfer the control out of the current code block. You should use the break statement after every case statement, except for the last one, because the control will get out of the switch statement after the execution since there would be no more statements left to execute after that.

You will sometimes need to use a switch statement and would like a default set of lines to execute when none of the case statements have been executed. That is precisely the time when you should turn to the default statement. The default statement will get executed when none of the case statements that have been defined above has been executed. It is important to note that the switch statement compares the result of the given expression with the value provided on the case statements sequentially. So it is necessary to always define the default statement at the end of the switch statement because all the case statements defined under the default statement will never get executed.

Always place the default label at the end of the switch statement. So that at least one of the statements will get executed. The statement under the default label will get executed when no other statements under the case statement have been executed.

Flowchart Representing the Working of the Switch Statement in C:

flowchart of the switch statement in c This flowchart given above explains the working of a definitive Switch statement of the C programming language.

The compiler first evaluates the given expression and compares the result with the value defined within each case statement sequentially, starting from the top to the bottom. If the result does not match the value defined in the case statement, the compiler will check whether the result matches the value defined in the following case statement.

On the other hand, if The Compiler will transfer both the values match, the control to the line next to that case statement. If the case statement is terminated with a break statement, the control will jump out of the switch statement. And, when none of the case statements gets executed, the statements defined within the default statements get executed.

Rules for Switch Statement in C Language

Some essential and critical rules need to be followed when using the switch statement of the C programming language in your Program. The rules are listed below. Try to remember them and apply them when using the Switch statement.

  • The expression provided in the switch statement should always be either an integer value or a char value. You can also provide expressions, but the result of the expression should not return a Float or decimal value. The Switch statement will not accept float, double string, or other data types.

    Eg:

  • You can't use two case labels with the same value. Duplicate case values would throw an error.

    Eg:

  • You can't define ranges within a case statement, nor can you use a single case label for more than one value. A case label can hold only one value.

    Eg:

Important Facts About Switch Case Statement in C

You should be aware of some essential facts before getting your first hands-on experience with the switch statement of the C programming language. They are listed below:

  • The break statement at the end of each case label is optional. However, the control will sequentially pass through all other case labels without the break statement. At the end of the case label, the break statement will get the control out of the switch statement, so The Compiler will not execute the other case statements sequentially.

  • The default label for a switch statement is an optional one. You can use a switch statement without a default label, which would work fine.

  • Just like the if-else statement, you can also nest more than one switch statement inside another. But this would make the code complex and less readable, so it is generally avoided.

    Eg:

Switch Statement in C Example

Let's write a simple C program to mirror the basic functionalities of a simple calculator that takes two integers, performs the requested operation, and displays the output.

The above Program will get the choice from the user as an integer input, then read two numbers also from the user, perform the requested operation and print the output back to the user.

The Program uses a switch statement to perform the decision-making. At the end of each case label, the break statement makes sure the control transfers out of the switch statement and does not execute the other statements sequentially.

The default statement does not have a break statement cause the control will automatically get out of the switch statement after the last line.

Input:

Output:

Conclusion:

  • The switch statement takes an expression and, based on the result, will execute a statement set from a wide array of choices, unlike the if-else statement, which only allows you to choose between two choices.
  • You can use any expression for the switch statement, but the expression should return a single value, and the resulting value should never be of type float or double.
  • Only use constant values in the case statement; never use variables.
  • Terminate each of the case statements with a break, if you want the compiler to get out of the switch statement after the execution of that case statement.
  • Use the default statement when you want to execute a set of the statement when none of the case statements get executed.