EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Switch Statement in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Switch Statement in C

Switch-Statement-in-C

Introduction to Switch Statement in C

Before we learn what is Switch statement in C, let us first understand what is C. C is a procedure-oriented programming language developed by Dennis Ritchie. The basic purpose behind developing the C language was to use it as a programming language of the system i.e to program an operating system. Many languages borrow their syntax from this C language. C++, for instance, is an extension or can be considered as an upgraded version of C programming language.

What is Switch Statement in C?

Consider a case where you have been given a bunch of keys of different sizes. Now you are asked to open a door using one of the keys from this bunch. So what will be your approach towards this problem? It is simple, and you guessed it right, you will pick one key and try to unlock the door using it. If this key does not open the door, you try with another key. The process continues until you’ve finally found the key which unlocks the door. After the key is found and the door is unlocked, you stop. In case if you are able to find the key in the first attempt you will not keep on trying with the other keys after that, correct?

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Similar is the case with the switch statement. This example can help you easily understand the basic definition and flow of the switch statement. The basic flow and functionality of the switch statement remain the same in all the programming languages. The difference can be seen only in the general syntax based on the programming language being used.

In a very basic term, a switch statement evaluates an expression, tests it and compares it against the several cases written in the code. As soon as the match with any case is found, the control enters into this case and start executing the statements written within this case until a break statement is encountered. As soon as a break statement appears, the switch statement terminates and the program control exits switch.

It might sometimes happen that no case matches up with the value of the expression. For such cases, we mention a default case that will always execute in case if no match is found.

The cases in a block of the switch statement are represented by different numbers or string, which is known as an identifier. The value of the expression or the value provided by the user is compared with these cases until the match is found.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

The syntax for switch statement in C programming language is given below-

syntax :

switch( expression )
{
case value1:
//Block of code;
break;
case value2:
//Block of code;
break;
case valueN:
//Block of code
break;
default:
//Block of code
break;

Example:

This example will give more clarity about the use of switch statement
#include <stdio.h>
int main () {
char grade_report = 'D';
printf("Your performance is : ");
switch(grade_report) {
case 'A' :
printf("Outstanding Result!\n" );
break;
case 'B' :
printf("Excellent Result!\n" );
break;
case 'C' :
printf("Good Result\n" );
break;
case 'D' :
printf("Satisfying Result\n" );
break;
case 'F' :
printf("Poor Result\n" );
break;
default :
printf("You did not appear for exam\n" );
}
return 0;
}

Output:

Switch Statement in C-1

Flowchart of Switch Statement

Switch Statement of C

How Switch Statement Works in C?

Let us understand the flow of control depicted in the flowchart above in order to gain a better understanding of the flow of execution.

An expression is passed with the switch statement which is equal to one of the values of the cases. In case the value is not equal, the default case is executed. The value of this expression is then compared with the case identifier or the first case. If the first case matches then the block of code associated with the first case is executed. Once the break is encountered, the execution stops and you will exit the switch statement. However, if the case does not match, the execution flows to the next case. If this case matches, then the second code block executes otherwise, the flow checks the next case in a similar way. Finally, if no case matches then the default code block is executed.

Examples for Switch Statement in C

Let us have a look at few more examples –

Example #1

This example depicts the use of break statement in switch. If the break statement is not specified after the case, the execution flow will continue until it encounters the break statement.

Code:

#include <stdio.h>
int main() {
int range_of_number=50;
switch (range_of_number) {
case 10:
case 20:
case 30:
printf("The number is 10 or 20 or 30 ");
break;
case 50:
case 55:printf("This case also executes because there is no break ");
printf("\n");
case 60:
printf("The number is either 40 or 50 or 60");
break;
default:
printf("The number is greater than 60");}}

Output:

Switch Statement in C-2

Example #2

Code:

#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x==y && x+y<10)
{
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
}  }

Output :

Switch Statement in C-3

Example #3

Nested Switch Statement.

Code:

#include <stdio.h>
int main() {
int ID = 300;
int password = 1000;
printf("Enter Your ID:\n ");
scanf("%d", & ID);
switch (ID) {
case 300:
printf("Enter your password:\n ");
scanf("%d", & password);
switch (password) {
case 1000:
printf("Welcome to the portal\n");
break;
default:
printf("Enter correct password");
break;
}
break;
default:
printf("Enter correct ID");
break;
}
}

Output :

This will depend upon the values entered by the user.

Output 1 : 

Switch Statement in C-4

Output 2:

Switch Statement in C-5

Output 3:

Switch Statement in C-6

Conclusion

Switch case statements are a controlled statement that is regarded as a substitute for if-else statements. It is a multiway branch statement that provides a way to organize the flow of execution to parts of code based on the value of the expression.

Recommended Articles

This has been a guide to the Switch Statement in C. Here we also discuss the basic concept and How Switch Statement Works in C respectively. You may also have a look at the following articles to learn more –

  1. Switch Statement in C++
  2. PHP Switch Statement
  3. Python Switch Statement
  4. Switch Case in PowerShell

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More