1. C Programming for Beginners: Writing Your First Hello World Program in C in 2023 2. Mastering Datatypes in C Programming: A Comprehensive Guide for Beginners in 2023 3. Complete Guide to % Format Specifiers in C for Beginners in 2023 4. A Comprehensive Guide to Operators in C Programming: Learn Everything About Operators in C in 2023 5. Master C Operator Precedence and Associativity in 2023: Definition, Examples, and Code 6. Learn Increment and Decrement Operators in C: A Beginners Guide with Examples - Codzify 7. Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify 8. Ternary Operators in C Programming: A Beginners Guide (2023) 9. Switch Statements in C 2023: Complete Guide for Beginners 10. Learn Functions in C Programming - A Complete Tutorial for Beginners in 2023 11. Call by Value and Call by Reference in C Programming - A Complete Tutorial for Beginners in 2023 12. Storage Classes in C Programming: A Comprehensive Guide for 2023 13. Arrays in C Programming: A Comprehensive Guide for 2023 14. Basics of 2D Array in C Programming in 2023: Definition, Initialization, and Accessing Elements 15. Beginners Guide to Strings in C Programming with Codes and Examples 16. Strings Functions in C Language - strlen, strcpy,strcmp,strcat functions 17. A Comprehensive Guide to Structures in C Programming in 2023 18. Learn Unions in C Programming with Codes, Examples, and Explanations in 2023 19. Dynamic Memory Allocation in C: Understanding malloc, calloc, realloc, and free Functions 20. Introduction to C Programming 21. Hello world program in C 22. String functions in C - strncat, strcpy, strncpy, strchr, strrchr, strstr functions 23. Program for Fibonacci number in C - Codzify.com 24. What is palindrome number in C? 25. Write a C program to find a factorial of a given number 26. Write a C program to check whether a given number is prime or not 27. Write a C program to check whether a given number is an Armstrong number or not. 28. Write a C Program to transpose a matrix 29. C Programming Tutorial: Understanding Comments and Identifiers with Code Examples 30. Master String Handling Functions in C in 2023 - Codzify Topics

Switch Statements in C 2023: Complete Guide for Beginners

Article by: Manish Methani

Last Updated: November 4, 2021 at 2:04pm IST
4 min 30 sec read

The Switch statement is a selection statement in C that allows you to execute different statements depending on the value of an expression. It's similar to an if-else statement but it's more concise and easier to read when you have to compare a large number of values.

In this tutorial, we'll go through everything you need to know about the switch statement in C, including its syntax, usage, and some examples.

Syntax of a Switch statement in C:

switch(expression) {
  case constant-expression:
    statement(s);
    break;
  case constant-expression:
    statement(s);
    break;
  .
  .
  .
  default:
    statement(s);
}
  • The expression is evaluated once and compared with each constant-expression in the case statements.
  • If the expression matches a constant-expression, the statements following that case will be executed until a break statement is encountered.
  • If no case matches the expression, the default statements will be executed.

Now let's take a look at some examples to understand the usage of the switch statement.

Example 1: Simple switch statement

#include 

int main() {
  int day = 3;

  switch (day) {
    case 1:
      printf("Monday");
      break;
    case 2:
      printf("Tuesday");
      break;
    case 3:
      printf("Wednesday");
      break;
    case 4:
      printf("Thursday");
      break;
    case 5:
      printf("Friday");
      break;
    case 6:
      printf("Saturday");
      break;
    case 7:
      printf("Sunday");
      break;
    default:
      printf("Invalid day");
  }
  
  return 0;
}

In this example, we have a simple switch statement that takes an integer variable day and prints the corresponding day of the week. If the value of day is not between 1 and 7, it prints "Invalid day". The output of this program will be "Wednesday".

Example 2: Switch statement with multiple cases

#include 

int main() {
  int score = 85;
  
  switch (score) {
    case 80:
    case 81:
    case 82:
    case 83:
    case 84:
      printf("Grade B");
      break;
    case 85:
    case 86:
    case 87:
    case 88:
    case 89:
      printf("Grade A-");
      break;
    case 90:
    case 91:
    case 92:
    case 93:
    case 94:
      printf("Grade A");
      break;
    default:
      printf("Invalid score");
  }

  return 0;
}

In this example, we have a switch statement that takes an integer variable score and prints the corresponding grade. If the value of score is between 80 and 84, it prints "Grade B". If it's between 85 and 89, it prints "Grade A-". If it's between 90 and 94, it prints "Grade A". If the value of score is not between 80 and 94, it prints "Invalid score". The output of this program will be "Grade A-".

That's it for this tutorial. We have covered the basics of the switch statement in C with some examples. I hope this tutorial has been helpful for you to understand the switch statement in C.

 

Learn Flutter, FlutterFlow,
Firebase & Angular.

Looking to learn how to create the production-ready apps
from scratch? Well, you are at the right place.

₹1299 or $15.56

Learn to create the Dating App using No-Code Tool FlutterFlow

Level: Advanced

Explore Curriculum
₹1299 or $15.56

Flutter Mobile App Development Course

Explore Curriculum
₹1299 or $15.56

The Complete Angular Course

Explore Curriculum
₹1299 or $15.56

Dart Programming for Absolute Beginners

Coming Soon
FREE

Next.js course

Start Watching
FREE

Fundamentals of Computer Programming Languages

Start Watching
FREE

Learn HTML, CSS & Bootstrap

Start Watching

Latest Web Stories

1

Learn how to open WhatsApp using FlutterFlow | Step by Step Guide

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test

Download the Codzify
Mobile App


Learn Anytime, Anywhere at your own pace. Scan the QR Code with your Mobile Camera to Download the Codzify Mobile App.

Codzify Mobile App Codzify Mobile App
Codzify Logo

Terms and Conditions    Cookie Policy   Refund Policy   Adsense Disclaimer