Switch-case statement

switch-case statements

switch-case control statement is another and fast way to deal with the conditional statement.

Syntax of switch/case statement

switch(switch variable)
{
	case constant-1:
    	statement-1;
        statement-2;
        statement-3;
        ...........;
        ...........;
    case constant-2:
    	statement-1;
        statement-2;
        statement-3;
        ...........;
        ...........;
    case constant-3:
    	statement-1;
        statement-2;
        statement-3;
        ...........;
        ...........;
        
    ...............
    ...............
    ...............
    
    default:
    	statement-1;
        statement-2;
        statement-3;
        ...........;
        ...........;

}

Syntax Explained

The switch variable is matched with each and every case-constant and if switch variable value is matched, with any case constant then all the statements written after it, are executed.

One more thing is that in the place of switch variable any Integral expression can also be written.

In the above-given syntax note that case contains multiple statements but no need to enclosed in brackets.One more thing you should note that after case constant there is no semicolon(;), there is a colon(:). Don't try to change.

Program to print the days in the given month

#include<stdio.h>
int main() {
	int m,y;
	
	printf("Enter any month, say(1-12): ");
	scanf("%d",&m);
	
	switch(m) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			printf("31 days");
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			printf("30 days");
			break;
		case 2:
			printf("Enter the year: ");
			scanf("%d",&y);
			if(y%400 == 0 || y%100 != 0 && y%4 == 0) {
				printf("29 days");
			} else {
				printf("28 days");
			}
		break;
		default:
			printf("Invalid input");
	}
	
}

Output

Enter any month,say(1-12): 2
Enter the year: 1998
28 days

Enter any month,say(1-12): 7
31 days

Program to create a simple calculator using switch

/*create a calculator*/
#include<stdio.h>
#include<math.h>

int main() {
 	int a,b;
 	char ch;
 	
	printf("Enter any choice,say(+,-,*,/,%%,^): ");
	scanf("%c",&ch);	
 
 	printf("Enter any two numbers: ");
 	scanf("%d%d",&a,&b);	
 	
	switch(ch)
 	{
 		case '+':
			printf("Total= %d",a+b);
			break;
 		case '-':
 			printf("Sub= %d",a-b);
 			break;
 		case '*':
 			printf("Multiplication= %d",a*b);	
 			break;
 		case '/':
 			if(b!=0)
 				printf("Division= %.2f",(float)a/b);
 			else
				printf("Error! Divide by zero");
 			break;
 		case '%':
 			if(b!=0)
 				printf("Remainder= %d",a%b);
 			else
 				printf("Error! Div by zero");
 			break;
 		case '^':
 			printf("%d raised to power %d=%d",a,b,(int)(pow(a,b)));
 			break;
 		default:
 			printf("Invalid Input.");
 		}
 	return 0;
}

Output

Enter any choice,say(+,-,*,/,%,^): *
Enter any two numbers: 4 8
Multiplication= 32

Program Explained

  1. In the printf("Enter any choice,say(+,-,*,/,%%,^): ");, look at the double % sign. % sign can't be printed on screen because the compiler thinks it the start of the format specifier. To print % on output screen use the double % sign.
  2. To calculate the power caret(^) sign is used for the notation. It is not an operator in c but many other languages used it for power.
  3. The next thing that you should observe, printf("%d raised to power %d=%d",a,b,(int)(pow(a,b))); statement. In this statement pow() function is used to calculate the power. This function returns the output in double. To convert the output into an int. The typecasting is done.

Switch case control statement | Video Tutorial in Hindi