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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.