Switch Statement In C [ Full Information With Examples ]
What is Switch Statement In C
Definition -: Switch Statement is a Decision Making Control Statement that we use when we have many options and we need to execute only one of those options.
Switch Statement is an alternative to a larger if else statement. With the help of Switch Case Statement We can do all the work done by if-else statement
We cannot evaluate the expression as true or False in the parentheses () of Switch Statement. We match that expression with constant values written after the Case keyword in the body of the Switch Statement and if the expression matches any constant value, we execute the code after that case.
Syntax of switch case statement
The syntax of the switch case statement is something like this:
switch (variable or an integer expression)
{
case constant_1 // Code to be executed if expression == constant_1
break;
case constant_2: // Code to be executed if expression == constant_2
break;
case constant_3: // Code to be executed if expression == constant_3
break;
default: // the default case is optional
// Code to be executed if none of the cases match
;
}
To understand the Switch statement in C language, let us see a simple example.
Example of Switch Statement In C
#include <stdio.h>
void main ()
{
int number = 0;
printf ("Enter a number: \n");
scanf ("%d", &number);
switch (number)
{
case 10:
printf ("number is equals to 10");
break;
case 50:
printf ("number is equal to 50");
break;
case 100:
printf ("number is equal to 100");
break;
default:
printf ("number is not equal to 10, 50 or 100");
}
}
Output -:
Enter a number
10
number is equals to 10
In this program, we asked the user to enter a value and the user entered 10. Case 2 is matched with the value entered by the user, so “number is equals to 10” was printed.
If the value entered by the user does not match any case, then the code for the default case would run and in the result “number is not equal to 10, 50 or 100” would be printed.
Below, I am going to tell you some important points related to the switch statement, which you have to fit in your mind properly, because whenever you use the switch statement in C language, then these things will always work for you.
So let’s know.
Important Points about Switch Statement in C:
1. Whatever expression will be in the parentheses of the Switch Statement will eventually provide a constant value. If the result of the expression does not provide a constant value, then the expression will not be valid.
Valid Expression
switch (1+2+23)
switch (1*2+3%4)
Invalid expression
switch (ab + cd)
switch (a + b + c)
Note -: Such an expression (a + b + c) will be valid when it is a variable and has a fixed value already assigned to it.
2. In the parenthesis () of the switch statement, either will be the integer constant or will be a character constant.
Example -: switch (6 + 2) or switch (a)
3. The constant value after the Case keyword inside the switch block cannot be used in more than one case or say that duplicate case value is not allowed.
Invalid case value
case 'A': code;
break;
case 'A': code;
break;
Valid case value
case 'A': code;
break;
case 'B': code;
break;
4. It is not mandatory to write the switch case constant in a single order like 1,2,3,4, you can write it in any order (eg 2,4,3,1).
Example
switch (expression)
{
case 2: code;
break;
case 1: code;
break;
case 3: code;
break;
default: code:
}
5. We can nesting inside the switch statement, but nesting will make the code a bit complex, so I suggest you it is good if you do not nesting inside the switch case.
6. The default case that we write inside the switch block is optional, even if you do not write the default case, it will run. But I suggest that you should write a default case so that the program stays true.
7. We have to put colon “:” after the Constant which is followed by the Case keyword, this colon defines that case.
8. In every case, it is optional to write the break keyword at the end of the code.
However, if you do not use the break keyword, then the codes of all the other cases after that case will also be executed.
So you should use break when you do not have to execute any other case code after executing the code of a case.
Let’s understand this with a program
Example Program_1:
#include <stdio.h>
int main ()
{
int i = 2;
switch (i)
{
case 1:
printf ("Case1");
case 2:
printf ("Case2");
case 3:
printf ("Case3");
case 4:
printf ("Case4");
default:
printf ("Default");
}
return 0;
}
Output -:
Case2 Case3 Case4 Default
Program Explanation -:
Look at the program above once, in that program, we have taken the i variable as an expression in the parenthesis () of the switch. The value of this i variable is 2 that matches case2 of the switch block, so the code of case2 will run.
But did you notice? In this program, along with the code of case2, the code of all the cases after Case2 (Case3 Case4 Default) is also being executed. Due to which the output came Case2 Case3 Case4 Default.
Think about why this happened?
This happened because we did not use the break keyword at the end of Case2’s code. The break keyword tells the compiler that now we have to get out of this switch block.
If we do not use the break keyword, the compiler will not know where the code will end and the compiler will execute the code of the switch block until the default case runs.
Do you understand what I am trying to tell you?
If yes then it is a good thing and if not, then you just know that the code you will write after the Case keyword in the Switch block, you will also have to write the break keyword at the end of that code. So after that case is run, the code of any other case will not be executed.
Read More -:
- Control Statements in C
- If else Statement in C
- Loop in C
- For Loop in C
- While and Do while Loop in C
- Break Statement in C
- Continue statement in C
- Conditional Operator in C
- Download C Language Notes Pdf
- C Language Tutorial For Beginners
- C Programming Examples With Output
- 250+ C Programs for Practice PDF Free Download
Conclusion
Friends, I hope that after reading this post today, you have learned what is the Switch Statement in C language. And how to use it in C language?
Friends, I hope you have found the answer of your question and you will not have to search about the Switch statement in C language.
However, if you want any information related to this post or related to programming language, and computer science, then comment below I will clear your all doubts.
If you want a complete tutorial of C language, then see here C Language Tutorial. Here you will get all the topics of C Programming Tutorial step by step.
Friends, if you liked this post, then definitely share this post with your friends so that they can get information about the Switch statement in C Language.
To get the information related to Programming Language, Coding, C, C ++, subscribe to our website newsletter. So that you will get information about our upcoming new posts soon.