6/28/21

C Switch Statement in C Programming Language.

In this tutorial, we are going to learn about Switch Statement in the C programming language. We have already seen if, if-else, nested if-else, condition, etc. If you haven’t read those tutorials then first go and read those tutorials then read them. 

Topics that cover today.

  1. C switch statement
  2. C Switch Syntax
  3. Flow chart of C Switch Statement
  4. Example of C switch statements

So let’s go with our today’s topic. 

C Switch Statement

The C switch statement is used to execute a code block among many alternatives statements. The Switch statement allows the variable to be tested with the statement( Values) also known as Case written in the switch statement and if the statement is verified(true) with a statement then the code written between that statement will execute and then the rest of the code will start executing.

The same thing is also done by the if-else condition. But the syntax of the switch condition is easier to read and understandable for the programmers.

C Switch Statements Syntax.

switch (expression)

{
    case constant-expression1 :

    statements;
    break;                      /* The break function is optional  */
 
    case constant-expression1 :

    statements;
    break

 
    .
    .
    .
    .
     default:

     default statements;

}

Note: - The Break statement in the switch is not compulsory (optional) to add to the program. If the programmer wanted to execute the default statement then the break statement is not added to the program.

If none of the statements matched with the expression then by default statement is automatically displayed on the output screen.

Flow chart of the Switch Statement

Example of Switch Statement

Program to find whether a number is even, odd or negative.

#include<stdio.h>
int main()

{
  int number;

 
  printf("Enter a number: ");

  scanf("%d",&number);

 
  switch( number%2 )

  {
    case 0:

      printf("%d is even.", number);

      break;

    case 1:

      printf("%d is odd.", number);

      break;

    default:

        printf("Sorry...Number is Negative");

  }
 
  return 0;

}

Output:-




People Also Read : Nested IF-Else in C Programming

People Also Read : C Instruction With Example in C Language.

"+y+""}else{if(A==5){c+='
  • '+w+""+y+"
  • "}else{if(A==6){c+='
  • "+w+'
    '+u+""+y+"
  • "}else{c+='
  • "+w+"
  • "}}}}}s.innerHTML=c+=""+y;d.callBack()};randomRelatedIndex=h;showRelatedPost=g;j(d.homePage.replace(/\/$/,"")+"/feeds/posts/summary"+e+"?alt=json-in-script&orderby=updated&max-results=0&callback=randomRelatedIndex")})(window,document,document.getElementsByTagName("head")[0]); //]]>

    1 comment:

    1. It’s amazing and helpful article for all sort of readers. please share more article. you must read if you are live in Australia I really appreciate to you. I also rerecommended to you lean too...
      learn more
      learn more
      learn more
      learn more

      ReplyDelete

    Follow on Twitter

    Linkedin

    Categories

    Mad About Computer. Powered by Blogger.
    This site is in no way affiliated with or endorsed by specified business. It exists as a compendium of supporting information intended for informational purposes only. If you want to buy this website, please don't hesitate to contact us via e-mail: "d e n a c c 9 7 7 (at) g m a i l (dot) c o m" (delete spaces) or you can find and buy it on Afternic domain auctions.