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.

2 comments:

  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
  2. I hope you will share such type of impressive contents again with us so that we can utilize it and get more advantage.
    interaction design firm

    ReplyDelete

Like us on facebook

blogger templatesblogger widgets

Follow on Twitter

Linkedin

Categories

Mad About Computer. Powered by Blogger.