It is a statement in C that allows to execute a specific code block. It is one of the many alternative ways.
Note: switch case follows a top-down approach.
Syntax :
switch (expression)
{
case constant_1:
// statements within the block
break;
case constant_2:
// statement within the block
break;
.
.
.
default:
// default statements within the default block
}
Example : 1
//code to understand Switch case statement
//codewindow.in
#include <stdio.h>
int main(void) {
int val;
printf("\nEnter a value: ");
scanf("%d",&val);
switch(val)
{
case 1: printf("\nSunday");
break;
case 2: printf("\nMonday");
break;
case 3: printf("\nTuesday");
break;
case 4: printf("\nWednesday");
break;
default: printf("\nOff-Day");
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
For val= 1, Output will be Sunday
For val= 2, Output will be Monday
For val= 3, Output will be Tuesday
For val= 4, Output will be Wednesday
For any other value, Output will be Off-Day
*/
Explanation:
For value 1, the expression is matched with all the case values, the case value which matches with the expression i.e. case (1) and executes its statement and then breaks from the switch-case block.
For value 2, the expression is matched with all the case values, the case value which matches with the expression i.e. case (2) and executes its statement and then breaks from the switch-case block.
The same goes for other values too.
Example : 2
//code to understand Switch case Statement
//codewindow.in
#include <stdio.h>
int main(void) {
int val;
printf("\nEnter a value: ");
scanf("%d",&val);
switch(val)
{
case 1: printf("\nSunday");
case 2: printf("\nMonday");
case 3: printf("\nTuesday");
case 4: printf("\nWednesday");
default: printf("\nOff-Day");
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
For val= 1, Output will be --
Sunday
Monday
Tuesday
Wednesday
Off-Day
For val= 2, Output will be --
Monday
Tuesday
Wednesday
Off-Day
For val= 3, Output will be --
Tuesday
Wednesday
Off-Day
For val= 4, Output will be --
Wednesday
Off-Day
For any value, Output will be
Off-Day
*/
Explanation:
Here,
Since the break statement is not used in each case, it follows the next case after execution of the preferred block
When the value is 1, it executes the block with case value 1 and then follows the next case block (value 2) and continues, as there is no break statement used to break the execution process including the default block.
When the value is 2, it executes the block with case value 2 (the second one) and then follows the next case block (value 3) and continues, as there is no break statement used to break the execution process including the default block.
When the value is 4, it executes the block with case value 4 (the second one) and then follows the next case block (default case block).
Example : 3
//code
//codewindow.in
#include <stdio.h>
int main(void) {
int val;
printf("\nEnter a value: ");
scanf("%d",&val);
switch(val)
{
case 2: printf("\nSunday");
case 1: printf("\nMonday");
break;
case 4: printf("\nTuesday");
case 3: printf("\nWednesday");
break;
default: printf("\nOff-Day");
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
For val= 1, Output will be --
Monday
For val= 2, Output will be --
Sunday
Monday
For val= 3, Output will be --
Wednesday
For val= 4, Output will be --
Tuesday
Wednesday
For any value, Output will be
Off-Day
*/
Explanation:
Here,
For value 1 it searches the case with the same value and executes it, hence executes case 1 (which is after 2, in this case) and then breaks out of the process as they break statement is mentioned.
For value 2 it searches the case with the same value and executes it, hence executes case 2 (which is before 1, in this case) since there is no break statement and follows a top-down approach it executes the next immediate case block i.e. 1 and after executing its statement it finally breaks off the process.
*Here it doesn’t follow any numbering system such as 1 then 2 then 3, it follows a top-down approach.
For value 4 it searches the case with the same value and executes it, hence executes case with value 4 (which is before 3, in this case) since there is no break statement and follows a top-down approach it executes the next immediate case block i.e. case (3) and after executing its statement it finally breaks off the process.
Example: 4
//code to understand switch case Statement
//codewindow.in
#include <stdio.h>
int main(void) {
int val;
printf("\nEnter a value: ");
scanf("%d",&val);
switch(val)
{
default: printf("\nOff-Day");
case 2: printf("\nSunday");
case 1: printf("\nMonday");
break;
case 4: printf("\nTuesday");
case 3: printf("\nWednesday");
break;
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
For val= 1, Output will be --
Monday
For val= 2, Output will be --
Sunday
Monday
For val= 3, Output will be --
Wednesday
For val= 4, Output will be --
Tuesday
Wednesday
For any value, Output will be
Off-Day
Sunday
Monday
*/
Explanation:
Here,
For value 1 it executes the case block with value 1 and breaks the process.
For value 2 it executes the case block with value 2 i.e. case (2) since there is no break statement it immediately executes the next case block i.e. case (1) and then breaks off the process.
For value 4 it executes the case block with value 4 i.e. case (4) since there is no break statement it immediately executes the next case block i.e. case (3) and then breaks off the process.
For any other value it starts from the default value and execution of the process continues (case (2), case (1)) till it reaches a break statement which is after case (1), and the execution breaks.
Example : 5
//code to understand switch case statement
//codewindow.in
#include <stdio.h>
int main(void) {
char ch;
printf("\nEnter a character: ");
scanf("%c",&ch);
switch(ch)
{
case 'S': printf("\nSunday");
break;
case 'S': printf("\nMonday");
break;
case 'T': printf("\nTuesday");
break;
case 'W': printf("\nWednesday");
break;
default: printf("\nOff-Day");
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
Duplicate case value is not allowed in switch case
*/
Explanation:
Cases with the same value are not permitted in C. Hence it throws an error saying “Duplicate case value is not allowed in switch case”
Example : 6
//code to understand switch case Statement
//codewindow.in
#include <stdio.h>
int main(void) {
char ch;
printf("\nEnter a character: ");
scanf("%c",&ch);
switch(ch)
{
case 'S': printf("\nSunday");
break;
case 49: printf("\nMonday");
break;
case 'T': printf("\nTuesday");
break;
case 'W': printf("\nWednesday");
break;
default: printf("\nOff-Day");
/* It is used for the other cases except 1,2,3,4 */
}
return 0;
}
/*
Input: 12
Output: Off-Day
ch='1'
Ascii of '1' = 49
So, we can write as case '1' or case 49
*/
Explanation:
Here the value input is 12, thus none of the case value matches with it, hence it executes the default block and then breaks out of the process.
Also checkout
- Unlocking Innovation and Diversity: Accenture HackDiva Empowers Women in Tech with Cutting-Edge Solutions – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- QA Engineer Opportunities at Siemens Company: Apply Now – codewindow.in
- Software Engineer Positions at Siemens Company: Apply Now – codewindow.in
- Cloud Engineer II Opportunities at Insight Company: Apply Now – codewindow.in
- Shape Your Career: Assistant Engineer Opportunities at Jindal Company – codewindow.in
- Shape Your Future: Executive Opportunities at Jindal Company – cdewindow.in
- Associate Engineer, Software Development at Ingram: Apply Now – codewindow.in
- Jade Company’s UI/UX Development Engineer Opportunities – Apply Now – codewindow.in
- Transform Your Career with S&P Global: Apply for the Software Development Engineer Role and Lead the Future of Financial Technology Innovation – codewindow.in
- Unlock Your Potential at Accenture as an Associate Software Engineer – Elevate Your Career with Innovation and Excellence – codewindow.in
- Accelerate Your Career: Join NVIDIA’s Elite Software Engineering Internship Program and Shape the Future of Technology – codewindow.in
- C Programming Interview Questions – codewindow.in
- Lead the Way in Analytics: Specialist Position at Razorpay – codewindow.in
- Innovate with Cyient: Junior Software Engineer Wanted – codewindow.in
- Innovate with Volvo: Associate Software Engineer Wanted – codewindow.in
- Lead the Tech Revolution: Full Stack Developer at Unisys – codewindow.in
- Software Engineer at ABB: Unlock Innovation and Shape the Future – codewindow.in
- IBM Associate Systems Engineer Job: Boost Your Career with a Leading Technology Giant – codewindow.in
- Make Your Mark in Android Development: Join Concentrix – codewindow.in
- Infosys is Growing: Field Services Developer Role Now Open – codewindow.in
- Start Your IT Career Journey with Amazon: IT Services Support Associate I Opportunity – codewindow.in
- Shape the Future of Web: Front-End Software Engineer Opportunity at Google Cloud – codewindow.in
- Barclays QA Team Expands: QA Analyst Role Now Open- codewindow.in
- Eurofins QA Team Grows: Test Engineer Role Now Open – codewindow.in
- Exciting Opportunity: Java Spring Boot Senior Developer Role at Infosys – codewindow.in
- Unlock Your Potential at Nokia: Software Engineer Opportunities Await – codewindow.in
- Join Microsoft’s World-Class Team as a Software Engineer and Shape the Future of Technology – codewindow.in
- Virtusa is Seeking Talented React JS Developers to Drive Digital Excellence – codewindow.in
- Join IBM Dynamic Team as a Full Stack Developer and Shape the Future – codewindow.in
- EY Welcomes Aspiring AI/ML Interns to Unlock the Future of -codewindow.in
- Exciting Opportunity: Project Engineer at Rockwell Automation- codewindow.in
- Be at the Nexus of Technology & Finance: Associate Software Engineer at Goldman Sachs – codewindow.in
- Wipro is Hiring Test Engineers to Elevate Quality Assurance – codewindow.in
- Deloitte Is Hiring: Analysts and Senior Analysts Wanted to Drive Innovation – codewindow.in
- Exciting Software Development Opportunity At Oracle – codewindow.in
- BlackBerry Hiring System Software Developer – off campus – codewindow.in
- Lenskart hiring IT Support – off campus – codewindow.in
- Bold Hiring Software Engineer-UI – codewindow.in
- Myntra Hiring Software Engineer – off campus drive – codewindow.in
- PayPal Hiring Data Analyst 1 – codewindow.in
- SIEMENS HEALTHINEERS Hiring Data Scientist ( ML & AI) – codewindow.in
- RELX Digital hiring Software Engineer I – codewindow.in
- KPMG hiring Full Stack Developer Analyst – codewindow.in
- KPMG Hiring for Software Engineer – off Campus – codewindow.in
- Previous Year Coding Questions Suggestion Paper – codewindow.in
- Programming in C++ – codewindow.in
- Programming in C++ – codewindow.in
- Unstop Hiring Challenges Internships and Hackathons – codewindow.in
- Programming in Python – codewindow.in
- January 2024
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- June 2021
- May 2021
- April 2021
- March 2021
- January 2021
- December 2020
- November 2020
- October 2020
- September 2020