The switch Statement in C Language

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:

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:

Output:
2

Example 2:

Output:
2

Example 3:

Output:
A

You may also like...

39 Responses

  1. Anonymous says:

    in example 1 the output should be 12

  2. LearnCOnline says:

    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.

  3. Anonymous says:

    what is the use of default: there

  4. LearnCOnline says:

    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(

  5. Anonymous says:

    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?

  6. LearnCOnline says:

    ‘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: http://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

  7. Anonymous says:

    sir,if we use break ;then the next switch cases are cheked

  8. LearnCOnline says:

    no… once the break is executed. system moves the control out of switch statement

  9. sir,there is requirement to use break; in default case

  10. anuj pal says:

    i have windows 7 64bit my c language is not installing on this version..which version i install plz help me ….

  11. Anonymous says:

    in example all cases are printed because of no break

  12. Anonymous says:

    can 2 switch statements have the same case?

  13. sir suppose i am using if statment in switch what happenig please tell me sir

  14. if i will put a semicolon at the end of switch what will happenigng

  15. 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

  16. 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

  17. Anonymous says:

    Really nice explaination sir,thanks

  18. Anonymous says:

    is there any use,using break in default case

  19. Anonymous says:

    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.

  20. Anonymous says:

    sir kindly let me know,
    what can the expression in switch comprise of?

  21. Anonymous says:

    break is used to get out of the conditions ,if you use break the switch statement will stop looking for further matches .

  22. Anonymous says:

    Is it necessary to use the break in every programme whenever Switch condition used?

  23. 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?

  24. 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

  25. Anonymous says:

    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.

  26. Anonymous says:

    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?

  27. Unknown says:

    switch(a,b,option)
    {case ‘1’ : printf (“%d the sum is” ,a+b);
    break;
    case ‘2’: printf(%d the difference is “,a-b);
    break;
    }

  28. 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;
    }

  29. LearnCOnline says:

    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

  30. Anonymous says:

    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 ;

    }

  31. can we put a switch statement inside a switch statement

  32. Md Sumon says:

    yes you can put a switch statement inside a switch tatement

  33. Kairung Heng says:

    can we use > to compare the value of variable inside a switch statement?

  34. devendra says:

    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);
    }
    }
    }

  35. dev says:

    sir tell me how to differentiate two same loops in a program

  36. king man says:

    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)

Leave a Reply

Your email address will not be published. Required fields are marked *