Control Structures in Java if, if...else, nested if...else and the ternary operator (?:) जावा में कंट्रोल स्ट्रक्चर एवं उसके प्रकार

Control structures are essential parts of Java programming that control the flow of execution. They help a program make decisions, execute specific blocks of code, or repeat instructions. Java offers several types of control structures like if, if...else, nested if...else, and the ternary operator. These tools make logic implementation easier and organized.

कंट्रोल स्ट्रक्चर जावा प्रोग्रामिंग का एक आवश्यक हिस्सा होते हैं जो प्रोग्राम की निष्पादन प्रक्रिया को नियंत्रित करते हैं। ये प्रोग्राम को निर्णय लेने, विशेष कोड ब्लॉक चलाने या निर्देशों को दोहराने में मदद करते हैं। जावा में if, if...else, nested if...else, और टर्नरी ऑपरेटर जैसे कंट्रोल स्ट्रक्चर होते हैं, जो लॉजिक को आसान और व्यवस्थित बनाते हैं।


✅ 1. if Statement

The if statement checks a condition. If the condition is true, the code inside the if block is executed.

if स्टेटमेंट एक शर्त की जांच करता है। यदि शर्त सत्य होती है, तो if ब्लॉक के अंदर का कोड निष्पादित होता है।

Example:

public class TestIf {
public static void main(String[] args) {
int age = 20;
if(age >= 18) {
System.out.println("You are eligible to vote.");
}
}
}

✅ 2. if...else Statement

The if...else statement allows the program to execute one block of code if a condition is true, and another block if it is false.

if...else स्टेटमेंट प्रोग्राम को एक कोड ब्लॉक चलाने देता है यदि शर्त सत्य हो, और दूसरा ब्लॉक यदि शर्त असत्य हो।

Example:

public class TestIfElse {
public static void main(String[] args) {
int number = 10;
if(number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
}
}

✅ 3. Nested if...else Statement

Nested if...else statements are used when multiple conditions need to be checked. One if...else is written inside another.

जब एक से अधिक शर्तों की जांच करनी हो तो नेस्टेड if...else का उपयोग किया जाता है। इसमें एक if...else स्टेटमेंट के भीतर दूसरा if...else होता है।

Example:

public class TestNestedIf {
public static void main(String[] args) {
int marks = 85;
if(marks >= 60) {
if(marks >= 80) {
System.out.println("Distinction");
} else {
System.out.println("First Class");
}
} else {
System.out.println("Needs Improvement");
}
}
}

✅ 4. Ternary Operator (?:)

The ternary operator provides a shorthand way to write simple if...else conditions. It returns one of two values based on the condition.

टर्नरी ऑपरेटर एक संक्षिप्त तरीका है जिससे सरल if...else स्थिति को लिखा जा सकता है। यह एक शर्त के आधार पर दो में से कोई एक मान लौटाता है।

Syntax: (condition) ? value_if_true : value_if_false;

Example:

public class TestTernary {
public static void main(String[] args) {
int a = 10, b = 5;
String result = (a > b) ? "a is greater" : "b is greater";
System.out.println(result);
}
}

✅ Conclusion / निष्कर्ष

Control structures are fundamental in building logical decision-making in Java. By using if, if...else, nested conditions, and the ternary operator, developers can guide the program flow effectively. These structures not only simplify the logic but also enhance the readability and functionality of the code.

कंट्रोल स्ट्रक्चर जावा में लॉजिकल निर्णय लेने के लिए अत्यंत आवश्यक हैं। if, if...else, nested कंडीशन और टर्नरी ऑपरेटर का उपयोग करके प्रोग्राम की दिशा को प्रभावी ढंग से नियंत्रित किया जा सकता है। ये स्ट्रक्चर कोड को न केवल सरल बनाते हैं बल्कि उसकी पठनीयता और कार्यक्षमता भी बढ़ाते हैं।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध