C - Switch Case

1. Switch statement in C language is a selection statement . The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression that is included within the switch statement.

2. The general form of the switch statement is:

switch (expression)
	case value-1:block-1
	case value-2:block-2
	default: default-block-break;

3. Here, the expression should result to an integer or character constants value each block is generally a compound statement that specifies one particular course of action.

4. The break statement after each block signals the end of a particular case and causes an exit from the switch statement , thereby transferring the control to statement-x following switch.

5. value-1 , value-2......should be integer constants or character constants and are known as case labels ( also called case prefixes ) . Each case label represents one particular group of statements. The case labels must therefore be unique within a given switch statement.

6. When the switch is executed, the value of the expression is successively compared with values value-1 , value-2.... If a case is found whose value matches the value of the expressions, then the block of statements in that case is executed. After this, break terminates the switch and control is transferred to statement-x.

consider an example:

void main(){
	int i;
	printf("Enter a number between 1 to 3 : ");
		case 1:printf("one");
		case 2:printf("Two");
		case 3:printf("three");
		default:printf("Entered number is invalid");


Enter a number between 1 to 3 : 2

  • Print Page