Header Ads Widget

switch Statement in C

The switch statement is very useful while writing menu driven programs. Sometimes there is need in program to make choice among number of alternatives. For this purpose we use switch statement.

switch( expression )
{
    case constant1: statement;
                              ...............
                              ...............
    case constant2: statement;
                              ...............
                              ...............
    .
    .
    .
     case constantN: statement;
                              ...............
                              ...............
    default : default statement;
}

Here switch, case and default are keywords. The expression following switch keyword is any C expression that yields an integer value. The keyword case is followed by an integer or character constant. Each constant must be different from other.
Let us see how the switch statement works. First, the expression following the the keyword switch is evaluated. The value of this expression is then compared one by one with constant values that follow the case keyword. When a match is found, then all statements under that particular case are executed, and all subsequent case and default statement as well. If no match is found, then only statements following the keyword default are executed.

int main()
{
int i=3;
switch(i)
  {
    case 1: printf("One \n");
    case 2: printf("Two \n");
    case 3: printf("Three \n");
    case 4: printf("Four \n");
    default: printf("Wrong Choice \n");
  }
return 0;
}

The output of above program:
Three
Four
Wrong Choice

But here we want only case 3.  For this we have to use break statement. So  the above program is written as :

int main()
{
int i=3;
switch(i)
  {
    case 1: printf("One \n");
                 break;
    case 2: printf("Two \n");
                 break;
    case 3: printf("Three \n");
                 break;
    case 4: printf("Four \n");
                 break;
    default: printf("Wrong Choice \n");
  }
return 0;
}

The output of this program would be:
Three

There is no need for a break statement after default. Here multiple statements are executed in each case, but there is no need to enclose them within pair of braces.

Find output of following program ...


Post a Comment

4 Comments

  1. Hi Tej,

    My name is Anuj Agarwal. I'm Founder of Feedspot.

    I would like to personally congratulate you as your blog C Programming Basics has been selected by our panelist as one of the Top 25 C Programming Blogs on the web.

    https://blog.feedspot.com/c_programming_blogs/

    I personally give you a high-five and want to thank you for your contribution to this world. This is the most comprehensive list of Top 25 C Programming Blogs on the internet and I’m honored to have you as part of this!

    Also, you have the honor of displaying the badge on your blog.

    Best,
    Anuj

    ReplyDelete
  2. Nice blog, Thanks for sharing C programming basics knowledge.

    ReplyDelete
  3. provides useful information

    ReplyDelete