Best uses of switch statement in C language and its examples

Best uses of  switch statement in C  language and its examples
Techiio-author
Written by Shuvhojit DebFebruary 16, 2022
6 min read
C
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Techiio-author
Shuvhojit Deb

Full Stack Developer

It is our goal in this blog post to introduce you to the switch statement in C,in which we will learn how it works,its flowchart,and examples of how it can be used.

Introduction of switch statement in C:

The switch statement in C is an alternative to the if-else-if ladder statement which allows us to execute multiple operations for the different possible values of a single variable called the switch variable. Here, we can define various statements in multiple cases for the different values of a single variable.

Syntax:-

switch(expression){    
case value1:    
 //code to be executed;    
 break;  //optional  
case value2:    
 //code to be executed;    
 break;  //optional  
......    
    
default:     
 code to be executed if all cases are not matched;    
}
Flowchart of switch statement:

blogpost
Working of switch statement:

Allow us to comprehend the progression of control portrayed in the flowchart above to acquire a superior comprehension of the progression of execution.

An articulation is passed with the switch statement, which is equivalent to one of the upsides of the cases. On the off chance that the worth isn't equivalent, the default case is executed. The worth of this articulation is then contrasted and the case identifier or the main case. If the principal case matches, then, at that point, the square of code related to the main case is executed. When the break is experienced, the execution stops, and you will leave the switch statement. In any case, if the case doesn't coordinate, the execution streams to the following case. If this case matches, then, at that point, the subsequent code block executes; in any case, the stream looks at the following case along these lines. At last, if no case matches, then, at that point, the default code block is executed.

Examples for switch statement in C:

Example-1:-

This model portrays the utilization of the break statement in the switch. If the break statement isn't specified after the situation, the execution stream will go on until it experiences the break statement.

Code:-

#include <stdio.h>
int main() {
int range_of_number=50;
switch (range_of_number) {
case 10:
case 20:
case 30:
printf("The number is 10 or 20 or 30 ");
break;
case 50:
case 55:printf("This case also executes because there is no break ");
printf("\n");
case 60:
printf("The number is either 40 or 50 or 60");
break;
default:
printf("The number is greater than 60");}}

Output:-

blogpost

Example-2:-

Code:-

#include <stdio.h>
int main()
{
int x = 10, y = 5;
switch(x==y && x+y<10)
  {
case 1:
printf("hi");
break;
case 0:
printf("bye");
break;
default:
printf(" Hello bye ");
  }  
}

Output:-

blogpost

Example-3:-

Nested Switch Statement.

Code:-

#include <stdio.h>
int main() 
{
int ID = 300;
int password = 1000;
printf("Enter Your ID:\n ");
scanf("%d", & ID);
switch (ID) {
case 300:
printf("Enter your password:\n ");
scanf("%d", & password);
switch (password) {
case 1000:
printf("Welcome to the portal\n");
break;
default:
printf("Enter correct password");
break;
  }
break;
default:
printf("Enter correct ID");
break;
  }
}

Output:-

This will depend upon the values entered by the user.

Output-1:-

blogpost

Output-2:-

blogpost

Output-3:-

blogpost
Conclusion:

Switch case statements are controlled statement that is viewed as a substitute for if-else statements. It is a multiway branch statement that gives a method for getting sorted out the progression of execution to parts of code because of the worth of the articulation.

C Language
Programming
Control Statement
Switch Statement
2 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 2 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance