switch statement in C
The switch statement in C is very powerful decision making statement. It reduces the complexity of the program. Hence increases the readability of the program. Switch statement accepts single input from the user and based on that input executes a particular block of statements.
Syntax of switch statement in C:
switch(expression){ case constant 1: perform this; case constant 2: perform this; case constant 3: perform this; case constant 4: perform this; . . . default: perform this; }
Control passes to the statement whose case constant-expression matches the value of switch ( expression ). The switch statement can include any number of case instances, but no two case constants within the same switch statement can have the same value. Execution of the statement body begins at the selected statement and proceeds until the end of the body or until a break statement transfers control out of the body. Please make a note that using default case is optional.
Example 1:
int a = 2; switch(a){ case 1: printf(“1”); case 2: printf(“2”); }
Output:
2
Example 2:
int a = 2; switch(a){ case 1: printf(“1\n”); break; case 2: printf(“2\n”); break; default: printf(“No match”); }
Output:
2
Example 3:
char a = ‘A’; switch(a){ case ‘B’: printf(“B\n”); break; case ‘A’: printf(“A\n”); break; default: printf(“No match”); }
Output:
A
in example 1 the output should be 12
No.
The value “2” is assigned to variable a.
Hence control directly goes to case 2 and displays 2 as the output.
Hope this clears your doubt.
what is the use of default: there
default is a one of the switch case. This case gets executed only if none of the case condition is satisfied. Consider the below example:
int a = 10;
switch(a){
case 1:
printf(
I the general syntax section and in example 1 you do not use “break;”, but in examples 2 and 3 you do. What does “break;” do? When is it needed?
‘break’ is a statement. It is most commonly used with the switch statement. ‘break’ statement stops the execution within the switch statement and exits from the switch statement. In the example 1, if value of a had been 1 the output would have been
1
2
since there was no break statement in case 1.
but in example 2, there is a break statement in each of the case. Let us suppose value of a = 1. then within the switch statement, case 1 would be executed. After printing value 1, execution of break statement will cause program to exit from the switch statement skipping case 2 and default case.
For more info. on break statement visit: https://www.learnconline.com/2010/03/break-statement.html
Hope your doubt got cleared.
If you like this site then please don’t forget to click on ‘Like: Facebook’ button on the top right of this page.
Thanks,
LearnCOnline.com
sir,if we use break ;then the next switch cases are cheked
no… once the break is executed. system moves the control out of switch statement
sir,there is requirement to use break; in default case
i have windows 7 64bit my c language is not installing on this version..which version i install plz help me ….
in example all cases are printed because of no break
can 2 switch statements have the same case?
sir suppose i am using if statment in switch what happenig please tell me sir
switch itself is a keyword which contains combinations of if statements
if i will put a semicolon at the end of switch what will happenigng
int x=2;
switch(x,x+1)
{
case 1:
printf(“1”);
break;
case 2:
printf(“2”);
break;
case 3:
printf(“3”);
break;
}
it will work perfectly eventhough i am using commaoperator in switch please replay me eplanation
int x=2;
switch(x=(x=2,x=3,x))
{
case 1:
printf(“1”);
break;
case 2:
printf(“2”);
break;
case 3:
printf(“3”);
break;
default:printf(“Not Match “);
break;
}
it will also work perfect because comma operator execute left to right.
o/p : 3
Really nice explaination sir,thanks
is there any use,using break in default case
if we do not use break in any of the cases. But if the case expression matches switch expression in the first case itself, will the output comprise of results of all cases even though its expression does not match with switch expression.
please explain as soon as possible.
sir kindly let me know,
what can the expression in switch comprise of?
why we use break ??
break is used to get out of the conditions ,if you use break the switch statement will stop looking for further matches .
Is it necessary to use the break in every programme whenever Switch condition used?
suppose that if one of the case statement value matches with the switch value then along with the execution of all the following case statement will the default program also get executed?
if the default program is placed in between the cases statements then will the program execute the default statement if none of the case statement ONLY before it wont satisfy or all the case statements wont satisfy
hi this is very great for beginners. sir i have doubt that switch expression how compiler read for example switch(x=(x=1,x=2,x=3,x)). if int x=2 then case 3 and op=3 it become how.
sir if in example 1 if a=1 then why the putput would be:
1
2
it will execute case 1 because a=1 but why case 2?
switch(a,b,option)
{case ‘1’ : printf (“%d the sum is” ,a+b);
break;
case ‘2’: printf(%d the difference is “,a-b);
break;
}
wat wll be d output in dis case:
int main()
{
char suite =3;
switch (suite)
{
case 1:
printf (“diamond\n”);
case 2:
printf (“spade\n”);
default:
printf (“heart\n”);
}
printf(“suite\n”);
return 0;
}
THIS PAGE HELPS ALOT
Thank you everyone for the wonderful comments. I hope this website is helping every one out there.
If you like this website, kindly share it with everyone.
Thanks,
LearnCOnline Team
why it said ‘declaration terminated incorrectly’ ?…
char ‘a’=’A’;
switch(a)
{
LETTER “‘a’ && ‘z’ || ‘A’ && ‘Z'” : printf (“NOT A LETTER”) ;
break ;
LETTER “‘a’|| ‘A'” : printf (“Vowel A”) ;
break ;
LETTER “‘e’ || ‘E'” : printf (“Vowel E”) ;
break ;
LETTER “‘o’ || ‘O'” : printf (“Vowel O”) ;
break ;
default : printf (“NOT NOT A LETTER, Vowel A, Vowel E, Vowel O”) ;
break ;
}
can we put a switch statement inside a switch statement
yes you can put a switch statement inside a switch tatement
can we use > to compare the value of variable inside a switch statement?
sir,i want too program my line follower robot for goin on a ‘y’ road and i want it to follow the straight line then to move right way and the to stop at the end of the line i have made this program tell mi is it right or not and also tell me the right one if wrong.
int r,l;
int temp=0;
void setup()
{
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
}
void loop()
{
r=digitalRead(3);
l=digitalRead(4);
if(l==HIGH && r==HIGH)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
}
else if(l==HIGH && r==LOW)
{
digitalWrite(10,LOW);
digitalWrite(11,HIGH);
digitalWrite(12,HIGH);
digitalWrite(13,LOW);
}
else if(l==LOW && r==HIGH)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
}
else if(l==LOW && r==LOW)
{
temp++;
if(temp==1)
{
digitalWrite(10,HIGH);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
}
else if(temp==2)
{
digitalWrite(10,LOW);
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
}
}
sir tell me how to differentiate two same loops in a program
if we have multiple declaration and we want to use switch how we use of switch in case of multiple declaration.e.g switch(expression)