The switch Statement in C.

By Shakib Ansari | Date: Sat, Apr 5, 2025

Abhi tak aapne conditions ko check karne ke liye if-else conditions ka use kara hoga lekin kabhi jab bahut sari conditions ko check karna hota hai to aise mein if-else conditions program ki complexity ko increase kar deti hai aur program ko cumbersome bana deti hai jisse program ko understand karna hard ho jata hai. Fortunately, C has a built-in mutliway decision statement known as a switch. The switch statement check karti hai value of a given variable (or expression) against a list of case values, aur jab bhi koi case match hota hai us value se to us block ki sabhi statements execute ho jaati hai. The general form of the switch statement is as shown below:

switch(expression)
{
      case value_1: 
                            block_1
                            break;
      case value_2: 
                            block_2
                            break;
      ......
      ......
      default: 
                           default_block
                           break;
}

Ye expression ek integer expression or characters hai. value_1, value_2 .... ye constants or constant expression hai jise case labels kehte hai. Switch statement mein har ek value unique honi chahiye. block_1, block_2 ... ye statements list hai inme kuch statements hongi. Blocks ke around {} braces put karne ki need nhi hoti hai. case labels hamesha colon (:) se end hote hai.

Jab switch execute hogi to expression ki value compare hogi to value_1, value_2, .... agar koi aisa case found hua jiski value match hoti hai, to us block ki statements executes hongi.

The break; statement har ek block ke end mein hai, iska matlab hai ki jab koi case value, expression se match ho jaati hai aur us code block ki statements execute hokar end ho jaati hai tab program ka flow switch se exit ho jayga.

The default ek optional case hai. Ye isliye use hota hai agar koi case match nhi hota hai to at the end default_block execute ho jayga without checking any condition like else block.(ANSI C hame 257 case labels use karna permit deti hai matlab ham 257 cases check kar sakte hai switch statement mein).

Calculator Example Using Switch Statement :

#include <stdio.h>
int main() {
    double num1, num2;
    char operator;

    printf("Enter first number: ");
    scanf("%lf", &num1);

    printf("Enter an operator (+, -, *, /): ");
    scanf(" %c", &operator);  // notice space before %c to consume any newline

    printf("Enter second number: ");
    scanf("%lf", &num2);

    switch (operator) {
        case '+':
            printf("Result: %.2lf", num1 + num2);
            break;
        case '-':
            printf("Result: %.2lf", num1 - num2);
            break;
        case '*':
            printf("Result: %.2lf
", num1 * num2);
            break;
        case '/':
            if (num2 != 0)
                printf("Result: %.2lf", num1 / num2);
            else
                printf("Error: Division by zero is not allowed.
");
            break;
        default:
            printf("Error: Invalid operator.
");
    }

    return 0;
}
Output: Enter first number: 30
Enter an operator : +
Enter second number: 40
Result: 50.0

Code Explaination:

  1. Sabse pehle 1 input liya as double (num1), 1 operator as char (operator), and 1 more input as double(num2).
  2. Phir switch statement mein hamne operator pass variable pass kar diya.
  3. Phir four case define kare aur ek default case(optional).
  4. Ab operator ki value har ek case ki value se compare hogi aur jo bhi value match hoti hai us value ke code block ki statement execute ho jaygi.
  5. Agar koi case value nhi match hoti hai to default case execute ho jayga.
  6. Har case mein ek break; statement bhi hai jo us case ki statements ko execute karke switch se exit kara dega.
About the Author

Hi, I'm Shakib Ansari, Founder and CEO of BeyondMan. I'm a highly adaptive developer who quickly learns new programming languages and delivers innovative solutions with passion and precision.

Shakib Ansari
Programming

Comments