Switch statement in c programming language

 Switch statement in C programming language


In todays blog post we will look at the use of control statements in C Programming language. These control statements basically execute one block of code from multiple options or conditions depending on the value of single expression. And the control statement that we are going to discuss today is switch statement. And it can be used as an alternate to if else if ladder statement. It is not just an alternate to the if else if ladder but it is considered as more clean, readable, and more efficient alternate as compared to if else if ladder statement.

The switch statement is a conditional statement that we use to run different code sections based on some conditions. The switch statement also works like the if else-if else ladder statement. As we know in the else-if ladder statement we implement multiple conditions, it is exactly how the switch statement works. In the switch statement, we used the term case: that tells us about any specific condition within the switch expression area switch(expression).

 


Before diving deep into switch statement let us look at the essential components of switch statement and its basic working in C programming language. So that we can handle multiple conditions and statements more efficiently. For this we will look at the basic syntax of switch statement in C programming language.


What is the syntax of a switch statement in C programming


switch
(expression) { case constant1: { // code to be written here. break; } case constant2: { // code to be written here. break; } case constant3: { // code to be written here. break; } default: { // code to be written here. break; } }


If we look at the above coding syntax of switch statement in C programming language. It starts with a keyword switch followed by the opening and closing round brackets with some expression written between them. In switch statement there are some keywords that we should  keep in our mind while using the switch statement which are,

1) switch

2) expression (not a keyword)

3) case

4) break

5) default

Now with in the switch statement body we use the values that are generated from the expression that was written between the round brackets after the switch keyword. 

The most important part is the case keyword part with some value given to it like this,

case constant1:

and with in these case statements we will use the break keyword that will simply terminate the switch statement or simply comes out of the body of the switch statement whenever some case value is matched.

And at last if no case value matches with expression value then the default case will run automatically. So this is the basic working and syntax of switch statement in C programming language.


Rules and regulations of switch statement

Always remember that the expressions that are used within the switch statement i.e. switch(expression), this expression must be a single character or an integer value. As discussed earlier case: statements are used in switch statements, these cases must be an integer or a single character. At the end of each case coding block, we use a break statement.

It is not mandatory to write a break statement but it Is a good practice to use a break statement, because if there is no break statement used between different cases then all cases will run or be executed one by one.


What is the default keyword in the switch statement

We use this default keyword at the end of the switch statement. This default keyword works in the same way as the else statement works in the if else-if else ladder statement. In switch when no case is true then at-last the default case runs and executes some piece of code.


Simple coding example of switch statement in C programming language


#include
<stdio.h> void main() { int number = 2; switch (number) { case 1: { printf("Value is 1."); break; } case 2: { printf("Value is 2."); break; } case 3: { printf("Value is 3."); break; } default: { printf("Value is not 1, 2, or 3."); break; } } _getch(); }

In the above given coding example of switch statement in C programming language, at the start of the code we have declared and initialized a variable number of type int and assigned it a value of 2 like this, 

int number = 2;

Now let us move to the implementation of switch statement in C programming language. In our switch statement we have used number variable as switch expression, now with in our switch statement we have used multiple case statements with different values like this,

case 1:
case 2:
case 3:

In these case statements these values of 1, 2, and 3 are the values that our number variable might be holding and as per the value of the number variable any specific case will run.

Suppose if we assign the value of 2 to the variable number like this, 

int number = 2;

Then case 2: will be executed and the code written with in case 2: block will be executed. Now keep one thing in mind that at the end of  case 2: coding part we have used the break key word which will terminate and simply ends the switch statement and comes out of the body of switch statement.

And suppose if none of the case values matches then the default case will run and after that our switch statement will be terminated. So this is the basic working of switch statement in C programming language.

Congratulations! like always we have successfully implemented the switch statement in C programming language.

Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

Hello World Program In C Programming Language

How to add two numbers in C programming language