Switch in C

The switch statement tests the value of a given variable against a list of case values and when a match is found.a block of statement associated with that case is executed the following syntax of switch statement.

Syntax


switch(expression)
{
	case1:
		statement1;
		statement2;
		break;
	case2:
		statement1;
		statement2;
		break;
	case3:
		statement1;
		statement2;
		break;
	default:
		statement;
}							
							

Example


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

void main()
{
	int a;
	clrscr();
	
	printf("Enter any Number From 1 to 3:");
	scanf("%d",&a);
	
	switch(a);
	{
		case1:
			printf("One");
			break;
		case2:
			printf("Two");
			break;
		case3:
			printf("Three");
			break;
		default:
			printf("Please Enter 1 to 3 Number:");
			break;
	}
	getch();
}							
							
Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit