Switch Statements in C

Switch statements is used to create the multiple option. If there is multiple option in our program then we use switch statement.

Rules of switch statements

Syntax of switch statements:

	
    switch(expression)
    {
    case 1: 
    statement 1;
    break;
    
    case 2:
    statement 2;
    break;
    
    case n:
    statement n;
    
    default:
    statement 3;
    }
	

Switch statement first take expression then go for cases then match the case. If case not matched then move to next case, again not match go up to n(case), then go for default.

Example of switch statement program:

	
    #include<stdio.h>
    #include<conio.h>

    void main()
    {
	int option;
	int n;
	clrscr();
	printf("Enter Your Choice Between 1 to 3.");
	scanf("%d",&n);
	switch(option)
	{
	case 1:
	printf("Today is Monday.");
	break;
	case 2:
	printf("Today is Sunday.");
	break;
	case 3:
	printf("Today is Friday.");
	break;

	default:
	printf("You selected incorrect option.");
	}
	getch();
    }

Post Your Comment