Switch Case Statement in C Programming



For video tutorial you can see below video:



✔ In this tutorial, you will learn to create the switch statement in C programming with the help of an example.

✔ The switch statement allows us to execute one code block among many alternatives.
✔ You can do the same thing with the if...else..if ladder. However, the syntax of the switch statement is much easier to read and write.


Syntax of switch...case:


switch (expression)
{
         case constatnt1:
               // statements
                break;
         case constant2:
               // statements
                break;
              .
              .
              .
             default:
                   //default statements
 }


✔ The expression can be integer expression or a character expression.
✔ Value-1, 2, n are case labels which are used to identify each case individually.
Remember that case labels should not be same as it may create a problem          while executing a program. Suppose we have two cases with the same label          as '1'. Then while executing the program, the case that appears first will be            executed even though you want the program to execute a second case. This        creates problems in the program and does not provide the desired output.
✔ Case labels always end with a colon ( : ). Each of these cases is associated
with a block.
✔ A block is nothing but multiple statements which are grouped for a particular case.
✔ The break keyword in each case indicates the end of a particular case. If we do
not put the break in each case then even though the specific case is executed,      the switch will continue to execute all the cases until the end is reached. This        should not happen; hence we always have to put break keyword in each case.      Break will terminate the case once it is executed and the control will fall out of        the switch.
✔ The default case is an optional one. Whenever the value of test-expression is not matched with any of the cases inside the switch, then the default will be
executed. Otherwise, it is not necessary to write default in the switch.
✔ Once the switch is executed the control will go to the statement-x, and the execution of a program will continue.

How does the switch statement work?

👉The expression is evaluated once and compared with the values of                         each case label.
·         ðŸ‘‰If there is a match, the corresponding statements after the matching label are        executed. For example, if the value of the expression is equal to constant2,            statements after case constant2: are executed until break is encountered.
·        ðŸ‘‰If there is no match, the default statements are executed.
👉If we do not use break, all statements after the matching label are executed.
👉By the way, the default clause inside the switch statement is optional.


Switch Statement Flowchart:


Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter Your Choice: \n");
scanf("%d",&a);
switch(a)
{
case 1: printf("Today is Sunday");
break;
case 2: printf("Today is Monday");
break;
case 3: printf("Today is Tuesday");
break;
case 4: printf("Today is Wednesday");
break;
case 5: printf("Today is Thursday");
break;
case 6: printf("Today is Friday");
break;
case 7: printf("Today is Saturday");
break;
default: printf("Wrong Choice , Please Select No between 1 to 7");
break;
}
getch();

}

Output:
If user enter 1 then they get output “Today is monday”
Same like they enter number till 7 they get messages but if he/she enter a number is not between 1 to 7 then they get message ("Wrong Choice , Please Select No between 1 to 7");


Why do we need a Switch case?

👉There is one potential problem with the if-else statement which is the                complexity of the program increases whenever the number of alternative path increases. If you use multiple if-else constructs in the program, a program might become difficult to read and comprehend. Sometimes it may even confuse the developer who himself wrote the program.
👉The solution to this problem is the switch statement.

Rules for switch statement:

                      ✔ An expression must always execute to a result.
                      ✔ Case labels must be constants and unique.
                      ✔ Case labels must end with a colon ( : ).
                      ✔ A break keyword must be present in each case.
                      ✔ There can be only one default label.
                      ✔ We can nest multiple switch statements.

Summary:

✔ A switch is a decision making construct in 'C.'
✔ A switch is used in a program where multiple decisions are involved.
✔ A switch must contain an executable test-expression.
✔ Each case must include a break keyword.
✔ Case label must be constants and unique.
✔ The default is optional.
✔ Multiple switch statements can be nested within one another.


Post a Comment

0 Comments