Home » C programming language

Switch case statement in C language

In C Programming Language, ladder/multiple if can be replaced by the switch case statement, if value to be tested is integral type.

Switch statement is used to check a conditional expression or variable with the given multiple choices of integral types. These integral values are called case values.

In switch statement these is one condition (variable), which is checked with its multiple case values. This statement is very useful in programming language.

Remember following points:

  • There may be multiple values to be checked.
  • Program’s control moves to the matched case value, if there is no case value found it moves to the default case.
  • case value must be integral type literal, variable can not be use as a case value.
  • default case statement is an option, but it should be use.
  • default case can be placed anywhere in the switch body, but should be use at the end of the switch body.
  • Multiple case value can have single body i.e. you can check more than one case values for single set of statements.
  • Do not forget to put break after the case body.
  • switch statement has fall-down property, if break is not found, program’s execution moves to the next case body whether it is matched or not.
switch statement in c

Syntax

switch(test-condition/variable)
{
	case case-value:
		block1;
	[break];
		
	case case-value:
		block2;
	[break];
		
	case case-value:
		block3;
	[break];
		
	case case-value:
		block4;
	[break];

	default:
		block-default;
}

Here, break is an optional may be ignored if you are checking multiple case values for single block (set of statements).

Consider the examples:
/* program to print number from 0 to 5 using switch*/
#include <stdio.h>
int main()
{
	int num;
	printf("Enter an integer number: ");
	scanf("%d",&num);
	switch(num){
		case 0:
			printf("Zero");
			break;
		case 1:
			printf("One");
			break;
		case 2:
			printf("Two");
			break;
		case 3:
			printf("Three");
			break;
		case 4:
			printf("Four");
			break;
		case 5:
			printf("Five");
			break;
		default:
			printf("Invalid Number");
	}
	return 0;
}

Output

First run:
Enter an integer number: 0
Zero

Second run:
Enter an integer number: 4
Four

Third run:
Enter an integer number: 8
Invalid Number

Consider the example to check whether entered character is VOWEL or CONSONANT using switch statement having multiple case values without using break, because here we are checking multiple case values for single block.

/*program to chech entered character is VOWEL or CONSONANT*/

#include < stdio.h >
int main()
{

	char ch;
	printf("Enter a character: ");
	scanf("%c",&ch);
	
	if( (ch>='A' && ch<='Z') || (ch>='a' && ch<='z') )
	{
		switch(ch)
		{
			case 'A':
			case 'a':
			case 'E':
			case 'e':
			case 'I':
			case 'i':
			case 'O':
			case 'o':
			case 'U':
			case 'u':
				printf("%c is a VOWEL.",ch);
				break;
			default:
				printf("%c is a CONSONANT.",ch);
		}
	}
	else
		printf("Entered character is INVALID.");
	return 0;
}

Output

First run:
Enter a character: I
I is a VOWEL.

Second run:
Enter a character: t
I is a CONSONANT.

Third run:
Enter a character: 9
Entered character is INVALID.



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.