c programming switch case statement

C Programming Switch Case Statement

c programming switch case statement

C Programming Switch Case Statement

Table of Contents

By the end of this chapter, you will learn how to write switch statements in C programming.

C programming switch statement

By using nested if…else statements you can execute a specific block of code against many values. But if you want to check the value of single variable then it is better to use nested…else statement instead of nested if…else statement. switch

switch statement are often if…else faster than nested statements and the syntax of switch statements is relatively simple and transparent.

C Switch Syntax

				
					switch (expression)
 {
 case value1:
 //This code will be executed if the value of expression is equal to value1;
 break;

 case value2:
 //This code will be executed if the value of expression is equal to value2;
 break;
 .
 .
 .
 default:
 //This code will be executed if the value of the expression is not equal to any value;
}
				
			

How does the switch...case statement work?


Example 1: Switch Statement

				
					#include<stdio.h>
#include<conio.h>

int main()
    {
    int num;
    clrscr();
    printf("Enter any number (1 to 7) \n: ");
    scanf("%d",&num);
    switch(num)
    {
        case  1:
            printf("Today is Saturday");
            break;
        case  2:
            printf("Today is Sunday");
            break;
        case  3:
            printf("Today is Monday");
            break;
        case  4:
            printf("Today is Tuesday");
            break;
        case  5:
            printf("Today is  Wednesday");
            break;
        case  6:
            printf("Today is Thursday");
            break;
        case  7:
            printf("Today is Friday");
            break;
        default:
            printf("Only enter value 1 to 7");
    }
    getch();
}

				
			

output
				
					Enter any number (1 to 7): 4 
Today is Tuesday
				
			
c programming switch case statement

In the above program user input 4, the value of the expression matches case 4 : and Tuesday is printed.


Example 2: Switch Statement

				
					// Program for creating simple calculators
// Add, subtract, multiply, or divide based on user input.

# include <stdio.h>

int main() {

 char operator;
 double firstNumber, secondNumber;

 printf("Enter an operator (+, -, *,): ");
 scanf("%c", &operator);

 printf("Enter two operands: ");
 scanf("%lf %lf",&firstNumber, &secondNumber);

 switch(operator)
 {
 case '+':
 printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber);
 break;

 case '-':
 printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber);
 break;

 case '*':
 printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber);
 break;

 case '/':
 printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
 break;

 This statement will be executed if the // operator does not match any case values ​​(+, -, *, /).
 default:
 printf("Error! operator is not correct");
 }

 return 0;
}
				
			

output
				
					Enter an operator (+, -, *,): -
Enter two operands: 50.27
20.25
32.5 - 12.4 = 30.02
				
			

User – operator input is stored in the operator variable. And given the two operands 32.5 and 12.4 as inputs, they are stored in the firstNumber and secondNumber variable respectively.

Then program control jumps to the following block.

				
					printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-firstNumber);

				
			

Finally the break statement terminates the switch statement.

If the break statement was not used, all case after the correct case would be executed.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *