
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain nested switch case in C language
Problem
Write a C program to check the entered password by the user is valid or not based on his/her ID using nested switch case.
Solution
The solution is explained below −
In C language, we can write inner switch which is placed in an outer switch.
The case values of the inner and outer switch can have common values.
Rules
- An expression executes to a result.
- Constants and unique values must be used for case labels.
- Case labels has to be end with a colon ( : ).
- A break keyword has to be included in each case.
- There can be only one default label.
- We can write nested multiple switch statements.
Example
Following is the C program to check the entered password by the user is valid or not based on his/her ID using nested switch case −
#include <stdio.h> int main(){ int userid; int pwd; printf("enter userid:\n"); scanf("%d",&userid); switch (userid){ case 1234: printf("enter password:\n "); scanf("%d", & pwd); switch (pwd){ case 0000: printf("Tutorials Point\n"); break; default: printf("incorrect password"); break; } break; default: printf("incorrect userid"); break; } }
Output
You will see the following output −
Run 1:enter userid: 1234 enter password: 0000 Tutorials Point Run 2: enter userid: 1234 enter password: 234 incorrect password
- Related Questions & Answers
- Explain switch statement in C language
- Explain Nested if-else statement in C language
- Converting digits to word format using switch case in C language
- Switch case statement in C
- Please explain what happens when PHP switch case executes case 0?
- Switch case in Arduino
- Using range in switch case in C/C++
- Switch Case in Python (Replacement)
- Switch case calculator in JavaScript
- String in Switch Case in Java
- How to use case-insensitive switch-case in JavaScript?
- How do we use nested switch statements in C#?
- What are nested structures in C language?
- Explain C tokens in C Language
- The String in Switch Case in Java
Advertisements