
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Explain switch statement in C language
It is used to select one among multiple decisions. ‘switch’ successively tests a value against a list of integers (or) character constant. When a match is found, the statement (or) statements associated with that value are executed.
Syntax
The syntax is given below −
switch (expression){ case value1 : stmt1; break; case value2 : stmt2; break; - - - - - - default : stmt – x; }
Algorithm
Refer the algorithm given below −
Step 1: Declare variables. Step 2: Read expression variable. Step 3: Switch(expression) If value 1 is select : stmt 1 executes break (exists from switch) If value 2 is select : stmt 2 executes ;break If value 3 is select : stmt 3 executes; break …………………………………………… Default : stmt-x executes;
Example
The following C program demonstrates the usage of switch statement −
#include<stdio.h> main ( ){ int n; printf ("enter a number"); scanf ("%d", &n); switch (n){ case 0 : printf ("zero"); break; case 1 : printf ("one"); break; default : printf ("wrong choice"); } }
Output
You will see the following output −
enter a number 1 One
Consider another program on switch case as mentioned below −
Example
#include<stdio.h> int main(){ char grade; printf("Enter the grade of a student:
"); scanf("%c",&grade); switch(grade){ case 'A': printf("Distiction
"); break; case 'B': printf("First class
"); break; case 'C': printf("second class
"); break; case 'D': printf("third class
"); break; default : printf("Fail"); } printf("Student grade=%c",grade); return 0; }
Output
You will see the following output −
Run 1:Enter the grade of a student:A Distiction Student grade=A Run 2: Enter the grade of a student:C Second class Student grade=C
- Related Articles
- Explain nested switch case in C language
- Explain ‘simple if’ statement in C language
- Explain if-else statement in C language
- Explain Nested if-else statement in C language
- Explain else-if ladder statement in C language
- Explain Strict Comparison in JavaScript switch statement?
- Explain common code blocks in JavaScript switch statement?
- Switch case statement in C
- Explain the scope rules related to the statement blocks in C language
- Switch Statement in Java
- PHP switch Statement
- Java switch statement
- How to use strings in switch statement in C#
- Java switch statement example
- Explain C tokens in C Language

Advertisements