Download Our Mobile App
The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.
The syntax of switch statement in c language is given below:
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
1) The switch expression must be of an integer or character type.
2) The case value must be an integer or character constant.
3) The case value can be used only inside the switch statement.
4) The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known as fall through the state of C switch statement.
Let's try to understand it by the examples. We are assuming that there are following variables.
int x,y,z;
char a,b;
float f;
First, the integer expression specified in the switch statement is evaluated. This value is then matched one by one with the constant values given in the different cases. If a match is found, then all the statements specified in that case are executed along with the all the cases present after that case including the default statement. No two cases can have similar values. If the matched case contains a break statement, then all the cases present after that will be skipped, and the control comes out of the switch. Otherwise, all the cases following the matched case will be executed.
Let's see a simple example of c language switch statement.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
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");
}
return 0;
}
Output
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50
#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x>y && x+y>0)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}
}
Output
hi
C Switch statement is fall-through
In C language, the switch statement is fall through; it means if you don't use a break statement in the switch case, all the cases after the matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given below.
#include<stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
switch(number){
case 10:
printf("number is equal to 10\n");
case 50:
printf("number is equal to 50\n");
case 100:
printf("number is equal to 100\n");
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Output
enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
Output
enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100
We can use as many switch statement as we want inside a switch statement. Such type of statements is called nested switch case statements. Consider the following example.
#include <stdio.h>
int main () {
int i = 10;
int j = 20;
switch(i) {
case 10:
printf("the value of i evaluated in outer switch: %d\n",i);
case 20:
switch(j) {
case 20:
printf("The value of j evaluated in nested switch: %d\n",j);
}
}
printf("Exact value of i is : %d\n", i );
printf("Exact value of j is : %d\n", j );
return 0;
}
Output
the value of i evaluated in outer switch: 10
The value of j evaluated in nested switch: 20
Exact value of i is : 10
Exact value of j is : 20
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
This is how it works:
The switch expression is evaluated once
The value of the expression is compared with the values of each case
If there is a match, the associated block of code is executed
The break statement breaks out of the switch block and stops the execution
The default statement is optional, and specifies some code to run if there is no case match
The example below uses the weekday number to calculate the weekday name:
यो कसरी काम गर्दछ:
स्विच अभिव्यक्ति एक पटक मूल्याङ्कन गरिन्छ
अभिव्यक्तिको मान प्रत्येक केसको मानहरूसँग तुलना गरिन्छ
यदि त्यहाँ मिल्दो छ भने, कोडको सम्बन्धित ब्लक कार्यान्वयन हुन्छ
ब्रेक स्टेटमेन्ट स्विच ब्लकबाट बाहिर निस्कन्छ र कार्यान्वयन रोक्छ
पूर्वनिर्धारित कथन ऐच्छिक छ, र यदि कुनै केस मिल्दैन भने चलाउनको लागि केही कोड निर्दिष्ट गर्दछ
तलको उदाहरणले हप्ताको दिनको नाम गणना गर्न हप्ताको दिन संख्या प्रयोग गर्दछ:
When C reaches a break keyword, it breaks out of the switch block.
This will stop the execution of more code and case testing inside the block.
When a match is found, and the job is done, it's time for a break. There is no need for more testing.
A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block.
The default keyword specifies some code to run if there is no case match:
ब्रेक कीवर्ड
जब C ब्रेक किवर्डमा पुग्छ, यो स्विच ब्लकबाट बाहिर निस्कन्छ।
यसले ब्लक भित्र थप कोड र केस परीक्षणको कार्यान्वयन रोक्नेछ।
जब एक मिल्दो भेटिन्छ, र काम सकियो, यो ब्रेक को लागी समय हो। थप परीक्षणको आवश्यकता छैन।
ब्रेकले धेरै कार्यान्वयन समय बचत गर्न सक्छ किनभने यसले स्विच ब्लकमा बाँकी सबै कोडको कार्यान्वयनलाई "बेवास्ता" गर्छ।
पूर्वनिर्धारित किवर्ड
पूर्वनिर्धारित कुञ्जी शब्दले चलाउनको लागि केही कोड निर्दिष्ट गर्दछ यदि त्यहाँ कुनै केस मिलान छैन: