switch

Tests a condition against any number of case conditions.

 

Break must be used to terminate the switch, or the next statement will be executed.

 

A default case is utilised for when no condition is met.

 

#include <stdio.h>

	int main()

	{
		char myVar;

		printf("Please enter a value of 1 or 2: \n");
		scanf("%c", &myVar);

		switch (myVar)
		{
			case '1':
			 		printf("You entered a value of 1 \n");
			 		break;
			case '2':
				{
			 		printf("You entered a value of 2 \n");
					break;
				}
			default:
					printf("Invalid choice \n");
					break;
		}
		return 0;
	}

Leave a Reply