Switch Statement
- Till now we have Use if else statement to choose one of the many alternatives.
- But when number of alternative increases the , complexity of the program increases and then if else statements becomes very complex.
- To Make such problems easier C language provides a multi-way decision statement Switch statement
Switch Statement
- Switch statement is multi-way decision statement that takes the decision from the various cases
- A case is block of code which get's executed when certain case Expression get's matched.
- A case expression can be repeatedly used in a switch statement.
- Case Expression allows several alternates of action and choose one of them which will get executed during run time.
- When you want to solve multiple option type problems, for example: Menu like program, where one value is associated with each option and you need to choose only one at a time, then, switch statement is used.
- Switch statement is a control statement that allows us to choose only one choice among the many given choices.
- It executes that block of code which matches the case value. If there is no match, then default block is executed(if present).
switch(expression)
{
case value1:
//body
break;
case value2:
//body
break;
case value3:
//body
break;
default:
//optional case
break;
}
switch(expression) { case value1: //body break; case value2: //body break; case value3: //body break; default: //optional case break; }
- You Have seen that at end of the every case we have use break statement that is because to come out from switch statement after a particular case get's matched.
- If break statement is not used after each case , then every case gets executed after the matched case.
- We will see what happens when we dont use break statement.
- default case: Default case is optional case to add , that case gets executed when none of the expression get's matched.
Simple Program of switch case:
void main()
{
int choice;
printf("\nEnter 1 to display A");
printf("\nEnter 2 to display B");
printf("\nEnter 3 to display C");
printf("\nEnter the choice: ");
scanf("%d",&choice);
switch(choice) //switching the choice variable.
{
case 1: //if choice is 1 ,
//then this case will get execute.
printf("%c",'A');
break;
case 2://if choice is 2 ,
//then this case will get execute.
printf("%c",'B');
break;
case 3://if choice is 3 ,
//then this case will get execute.
printf("%c",'C');
break;
default://if no choice is matched
//than this case will execute.
printf("Invalid choice");
}
}
void main() { int choice; printf("\nEnter 1 to display A"); printf("\nEnter 2 to display B"); printf("\nEnter 3 to display C"); printf("\nEnter the choice: "); scanf("%d",&choice); switch(choice) //switching the choice variable. { case 1: //if choice is 1 , //then this case will get execute. printf("%c",'A'); break; case 2://if choice is 2 , //then this case will get execute. printf("%c",'B'); break; case 3://if choice is 3 , //then this case will get execute. printf("%c",'C'); break; default://if no choice is matched //than this case will execute. printf("Invalid choice"); } }
Output1
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 1
A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 1
A
Output2
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 2
B
Enter 2 to display B
Enter 3 to display C
Enter the choice: 2
B
Output3
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 3
C
Enter 2 to display B
Enter 3 to display C
Enter the choice: 3
C
Now we will see Same program but we will remove break statement from every case:
void main()
{
int choice;
printf("\nEnter 1 to display A");
printf("\nEnter 2 to display B");
printf("\nEnter 3 to display C");
printf("\nEnter the choice: ");
scanf("%d",&choice);
switch(choice) //switching the choice variable.
{
case 1: //if choice is 1 ,
//then this case will get execute.
printf("%c",'A');
case 2://if choice is 2 ,
//then this case will get execute.
printf("%c",'B');
case 3://if choice is 3 ,
//then this case will get execute.
printf("%c",'C');
default://if no choice is matched
//than this case will execute.
printf("Invalid choice");
}
}
void main() { int choice; printf("\nEnter 1 to display A"); printf("\nEnter 2 to display B"); printf("\nEnter 3 to display C"); printf("\nEnter the choice: "); scanf("%d",&choice); switch(choice) //switching the choice variable. { case 1: //if choice is 1 , //then this case will get execute. printf("%c",'A'); case 2://if choice is 2 , //then this case will get execute. printf("%c",'B'); case 3://if choice is 3 , //then this case will get execute. printf("%c",'C'); default://if no choice is matched //than this case will execute. printf("Invalid choice"); } }
Output1
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 1
ABC
Enter 2 to display B
Enter 3 to display C
Enter the choice: 1
ABC
Output2
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 2
BC
Enter 2 to display B
Enter 3 to display C
Enter the choice: 2
BC
Output3
Enter 1 to display A
Enter 2 to display B
Enter 3 to display C
Enter the choice: 3
C
Enter 2 to display B
Enter 3 to display C
Enter the choice: 3
C
- In Output1 Even if we did't choose 2 AND 3 , it displayed A , B and C,this is happen because we did not use break statements
- As well as in Output2 even if we did not choose 3 it display B AND C
- This is how break statement plays an important role in Switch statements
- So , break statements are important for executing the tasks properly.
- Switch statement are very easy and efficient to use.
- This how switch statements works.
Practice Programs
- Programs Regarding If-else Statements
- Program Regarding Nested If - statements
- Programs Regarding loops
Further Topics
- What are Array?
- Why Array?
- Types of Array
- How to declare array ?
- how to Initialize an array?
- What is One Dimensional Array
- Concept of One Dimensional Array
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- What is Two Dimensional Array ?
- Concept of Two Dimensional Array
- How to initialize Two Dimensional Array?
- How to take Elements of Two dimensional array from the User?
- How to display 2D Array As an Table?
- Practice Program Regarding Arrays
People Also Searched:
- If-else statements
- What is while loops
- What is do-while loops?
- what is for loop?
- break keywords?
- continue keywords?
- What are Control Statements
- Ternary Operators
- What are keywords?
- What are Identifiers?
- What are data types ?
- What are Variables?
- Constants In C
- Escape Sequences
- different types of Declaring constants
- Keyword 'const'
- Practice Programs
0 Comments
Post a Comment