Switch Case in C Language
Switch Case in Java
The switch case statement is a control flow construct used to perform different actions based on the value of a variable or expression. It is particularly handy when you have a single variable or expression that you want to compare against multiple values, each leading to different actions.
Here's a basic structure of a switch case statement:
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// Additional cases as needed
default:
// Code to execute if none of the cases match
}
Key Components
1. Expression: This is the variable or expression whose value will be compared against the cases.
2. Cases: Each `case` represents a possible value that the expression might have. If the expression matches a case, the code block associated with that case is executed.
3. Break: The `break` statement is crucial because it exits the switch block. Without it, the program would continue executing the code in subsequent cases, even if the condition has already been met.
4. Default: The `default` case is optional and serves as a fallback option. If none of the cases match the expression, the code within the `default` block is executed.
Let's look at a practical example to see how switch case works:
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
// ... other cases for the rest of the week
default:
dayName = "Invalid day";
}
In this example, the value of the `day` variable is compared against various cases to determine the corresponding `dayName`. Since `day` is 3, the `Wednesday` case will match, and `dayName` will be set to "Wednesday."
The switch case statement in Java provides an elegant way to handle multiple conditional scenarios in your code. It makes your code more readable and maintainable, especially when dealing with a single variable or expression that can have multiple values. By understanding the basics of switch case, you can enhance your programming skills and efficiently tackle complex decision-making in Java applications.
हिंदी:
स्विच केस स्टेटमेंट एक नियंत्रण प्रवृत्ति नियंत्रण है, जिसका उपयोग एक चर या अभिव्यक्ति के मूल्य के आधार पर विभिन्न क्रियाएँ करने के लिए किया जाता है। यह खासकर उपयोगी है जब आपके पास एक ही चर या अभिव्यक्ति है जिसे आप मूल्यों के खिलवाड़ करना चाहते हैं, प्रत्येक मूल्य पर विभिन्न क्रियाएँ करने के लिए।
यहां स्विच केस स्टेटमेंट का एक मूल संरचना है:
switch (अभिव्यक्ति) {
case मूल्य1:
// यदि अभिव्यक्ति मूल्य1 के साथ मेल खाती है, तो कोड का निष्पादन करें
break;
case मूल्य2:
// यदि अभिव्यक्ति मैच मूल्य2 के साथ मेल खाती है, तो कोड का निष्पादन करें
break;
// आवश्यकता के अनुसार अन्य मामले
default:
// कोड का निष्पादन यदि कोई भी मामला मेल नहीं खाता है
}
मुख्य घटक
1. अभिव्यक्ति: यह वह चर या अभिव्यक्ति है जिसके मूल्य को मुकाबला किया जाएगा, जिसे केसों के साथ।
2. केस: प्रत्येक `case` किसी संभावित मूल्य का प्रतिनिधित्व करता है जिसका अभिव्यक्ति से मेल खाता है। यदि अभिव्यक्ति किसी केस के साथ मेल खाती है, तो उस केस के साथ जुड़े कोड निष्पादित किया जाता है।
3. ब्रेक: `ब्रेक` स्टेटमेंट महत्वपूर्ण है क्योंकि यह स्विच ब्लॉक से बाहर निकल जाता है। बिना इसके, प्रोग्राम चाहे शर्त पहले ही पूरी हो चुकी हो या नहीं, आगे के केसों के कोड का निष्पादन करेगा।
4. डिफॉल्ट: `डिफॉल्ट` केस वैकल्पिक है और यह एक बैकअप विकल्प के रूप में कार्य करता है। यदि कोई भी केस अभिव्यक्ति से मेल नहीं खाता है, तो `डिफॉल्ट` ब्लॉक के अंदर का कोड निष्पादित किया जाता है।
चलो, स्विच केस कैसे काम करता है देखने के लिए एक व्यावहारिक उदाहरण देखते हैं:
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
// ... आपत्तिक मामलों के लिए अन्य केस
default:
dayName = "अमान्य दिन";
}
इस उदाहरण में, `day` चर की मूल्य को विभिन्न केसों के साथ तुलना किया जाता है ताकि संबंधित `dayName` निर्धारित किया जा सके। क्योंकि `day` 3 है, `Wednesday` केस मेल खाएगा और `dayName` को "Wednesday" सेट किया जाएगा।
जावा में स्विच केस स्टेटमेंट आपके कोड में विभिन्न संविदानी स्थितियों का सरल निर्णय लेने के लिए एक श्रेष्ठ तरीका प्रदान करता है। यह आपके कोड को और पठनीय बनाता है, खासतर जब आपके पास केवल एक चर या अभिव्यक्ति होती है जिसके पास अनेक मूल्य हो सकते हैं। स्विच केस की मूल बातों को समझकर, आप अपनैके प्रोग्रामिंग कौशल को बढ़ा सकते हैं और जावा अनुप्रयोगों में जटिल निर्णय निर्माण को कुशलता से संबोधित कर सकते हैं।
Post a Comment