C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Syntax References

switch case statement syntax in C/C++ language

Syntax of switch case statement in C/C++ programming language, this article contains syntax, examples and explanation about switch case statement in C language.

Here, is the syntax of switch case statement in C or C++ programming language:

switch (variable)
{
	case case_value1:
		block1;
		[break];
	case case_value2:
		block2;
		[break];
	.
	.
	.
	default:
		block_default;
}

Program will check the value of variable with the given case values, and jumps to the particular case block if case value matched.

For example: if the value of variable matches with the case_value2, it will execute the statement(s) written in block2.

break is optional here, we must use if we want to break the execution of switch statement, if break is not found after the block (statements written in that particular block), it will execute the statement of next case.

If any of the case values does not match with the variable, default block will be executed.

Consider the given example:

#include <stdio.h>

int main()
{
	int a=2;
	
	switch(a)
	{
		case 1:
			printf("One\n");
			break;

		case 2:
			printf("Two\n");
			break;			
		
		case 3:
			printf("Three\n");
			break;		

		default:
			printf("Default\n");
	}
	return 0;
}

Output

Two



COMMENTS