SDLC
Difference between HTML and HTML5
Before learning HTML vs. HTML5, let's learn: What is a Markup Language? A markup language a system...
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.
Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found.
If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.
In this tutorial, you will learn-
A general syntax of how switch-case is implemented in a 'C' program is as follows:
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
Block-1;
Break;
}
Statement-x; Following diagram illustrates how a case is selected in switch case:
Following program illustrates the use of switch:
#include <stdio.h>
int main() {
int num = 8;
switch (num) {
case 7:
printf("Value is 7");
break;
case 8:
printf("Value is 8");
break;
case 9:
printf("Value is 9");
break;
default:
printf("Out of range");
break;
}
return 0;
}Output:
Value is 8
Try changing the value of variable num and notice the change in the output.
For example, we consider the following program which defaults:
#include <stdio.h>
int main() {
int language = 10;
switch (language) {
case 1:
printf("C#\n");
break;
case 2:
printf("C\n");
break;
case 3:
printf("C++\n");
break;
default:
printf("Other programming language\n");}}Output:
Other programming language
When working with switch case in C, you group multiple cases with unique labels. You need to introduce a break statement in each case to branch at the end of a switch statement.
The optional default case runs when no other matches are made.
We consider the following switch statement:
#include <stdio.h>
int main() {
int number=5;
switch (number) {
case 1:
case 2:
case 3:
printf("One, Two, or Three.\n");
break;
case 4:
case 5:
case 6:
printf("Four, Five, or Six.\n");
break;
default:
printf("Greater than Six.\n");}}Output:
Four, Five, or Six.
In C, we can have an inner switch embedded in an outer switch. Also, the case constants of the inner and outer switch may have common values and without any conflicts.
We considere the following program which the user to type his own ID, if the ID is valid it will ask him to enter his password, if the password is correct the program will print the name of the user, otherwise ,the program will print Incorrect Password and if the ID does not exist , the program will print Incorrect ID
#include <stdio.h>
int main() {
int ID = 500;
int password = 000;
printf("Plese Enter Your ID:\n ");
scanf("%d", & ID);
switch (ID) {
case 500:
printf("Enter your password:\n ");
scanf("%d", & password);
switch (password) {
case 000:
printf("Welcome Dear Programmer\n");
break;
default:
printf("incorrect password");
break;
}
break;
default:
printf("incorrect ID");
break;
}
}OUTPUT:
Plese Enter Your ID: 500 Enter your password: 000 Welcome Dear Programmer
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.
Before learning HTML vs. HTML5, let's learn: What is a Markup Language? A markup language a system...
{loadposition top-ads-automation-testing-tools} There are a lot of open source tools and testing...
Here are computer science interview questions for fresher as well as experienced candidates to get...
What is SQOOP in Hadoop? Apache Sqoop ( SQL-to-Hadoop ) is designed to support bulk import of data...
What is Software Engineering? Software engineering is a process of analysing user requirements and then...
Download PDF 1) Explain what is Cassandra? Cassandra is an open source data storage system...