YouTip LogoYouTip

C Switch

C switch Statement

C switch Statement

-- Learning is not just about technology, but also about dreams!

C Tutorial

C Language Tutorial C Introduction C Environment Setup C VScode 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 enum (Enumeration) C Pointers C Function Pointers and Callbacks 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 Undefined Behavior C Command Line Arguments C Safe Functions C Sorting Algorithms C Project Structure C Language Examples C Classic 100 Examples C Quiz

C Standard Library

C Standard Library - Reference Manual <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library - ">C Standard Library - <a href="#" title="C Standard Library -

C Operators

C Loops

Deep Dive

Scripting Languages

Software

Web Services

Web Design and Development

Development Tools

Scripts

Computer Science

Programming

Web Services

Programming Languages

C switch Statement

C Decision Making C Decision Making

The switch statement is a restricted control flow statement that is used to execute different code blocks based on the value of an expression.

A switch statement allows testing a variable for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax

The syntax for a switch statement in C programming language is as follows:

switch(expression){
    case constant-expression :
       statement(s);
       break; /* optional */
    case constant-expression :
       statement(s);
       break; /* optional */
    
    /* you can have any number of case statements */
    default : /* Optional */
       statement(s);
}

Explanation of switch statement:

  • The value of the expression following the switch is compared with the constant value after each case until a match is found or execution reaches the default (if it exists).
  • If a match is found, the code block following the corresponding case is executed, and then the switch statement is exited.
  • If there is no match and a default exists, the code block following the default is executed.
  • If there is no match and no default, the entire switch statement is skipped until the end.

The switch statement must follow the following rules:

  • Type of switch expression: The expression in a switch statement must be an integer type (char, short, int, or an enumeration), or an expression that can be implicitly converted to an integer type.
  • Uniqueness of case labels: In a switch statement, each case label must be unique; there cannot be duplicate values.
  • Optionality of the default case: The default label in a switch statement is optional. If no case label matches, the code block under the default label is executed (if it exists).
  • Constant value in case labels: The value following the case label must be a constant expression, meaning its value can be determined at compile time.
  • Order of case labels: The order of case labels in a switch statement is not important; they can be written in any order. The program will match them in the order they appear.
  • Use of the break statement: Usually, a break statement is needed at the end of the code block for each case label to terminate the execution of the switch statement. Without a break statement, the program will continue executing the code in the next case label until it encounters a break statement or the switch statement ends.
  • Nesting of switch statements: Switch statements can be nested within other switch statements, but attention should be paid to code readability and complexity.
  • Scope of case labels and expressions: The case labels of a switch statement can be integer constant expressions, but they cannot be floating-point numbers or strings.

Flow Diagram

switch statement in C

Example

#include <stdio.h>

int main() {
   /* local variable definition */
   char grade = 'B';

   switch(grade) {
      case 'A' :
         printf("Excellent!n" );
         break;
      case 'B' :
      case 'C' :
         printf("Well donen" );
         break;
      case 'D' :
         printf("You passedn" );
         break;
      case 'F' :
         printf("Better try againn" );
         break;
      default :
         printf("Invalid graden" );
   }
   
   printf("Your grade is  %cn", grade );

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Well done
Your grade is  B

C Decision Making C Decision Making

C Operators

C Loops

3 Notes

  1. #0 Helen

    QQ9***13813@163.com

    Reference Link

    Use the switch statement to write a program that determines if a given year is a leap year based on the input year, and determines the number of days in that month based on the input month.

    Definition of a leap year:

    • An ordinary year (a year not divisible by 100) is a leap year if it is divisible by 4. (e.g., 2004 is a leap year, 1999 is not).
    • A century year (a year divisible by 100) is a leap year if it is divisible by 400. (e.g., 2000 is a leap year, 1900 is not).
    #include <stdio.h>
    int main() {
       int year, month, flag = 0;
       printf("Please enter the year and month, separated by a space:!n");
       scanf("%d %d", &year, &month);
       if( ( year % 4 ==0 && year % 100 != 0 ) || year % 400 ==0) {
          flag = 1;
          printf("The year you entered is a leap yearn");
       } else {
          flag = 0;
          printf("The year you entered is a common year!!n");
       }
       switch(month) {
          case 1: printf("The month you entered is January with 31 daysn"); break;
          case 2:
             if(flag == 1)
                printf("The month you entered is February with 29 daysn");
             else
                printf("The month you entered is February with 28 daysn");
             break;
          case 3: printf("The month you entered is March with 31 daysn"); break;
          case 4: printf("The month you entered is April with 30 daysn"); break;
          case 5: printf("The month you entered is May with 31 daysn"); break;
          case 6: printf("The month you entered is June with 30 daysn"); break;
          case 7: printf("The month you entered is July with 31 daysn"); break;
          case 8: printf("The month you entered is August with 31 daysn"); break;
          case 9: printf("The month you entered is September with 30 daysn"); break;
          case 10: printf("The month you entered is October with 31 daysn"); break;
          case 11: printf("The month you entered is November with 30 daysn"); break;
          case 12: printf("The month you entered is December with 31 daysn"); break;
       }
       return 0;
    }
    

    Helen

    8 years ago (2018-04-22)

  2. #0 Shadow Eagle

    323***0161@qq.com

    Use switch statement

← Php Removed ExtensionsPhp Deprecated Features β†’