Translate

Decision Making in C++ | C++ में निर्णय नियंत्रण (If, Switch, Loops)

Decision Making in C++: C++ में निर्णय लेना कैसे काम करता है?

क्या आपने कभी सोचा है कि एक program कैसे decide करता है कि उसे क्या करना है? जैसे हम real life में situations देखकर decision लेते हैं—वैसे ही programming में भी conditions के आधार पर decisions लिए जाते हैं। यही concept Decision Making in C++ कहलाता है, जिसे हिंदी में निर्णय नियंत्रण कहा जाता है।

Decision Making in C++ | C++ में निर्णय नियंत्रण

इस ब्लॉग में हम आसान भाषा में समझेंगे कि C++ में if-else, switch, और logical conditions कैसे काम करते हैं—और कैसे आप इन्हें real-world problems में use कर सकते हैं।

📌 Table of Contents

C++ में Decision Making (निर्णय नियंत्रण) क्या है?

Decision Making in C++ का मतलब है कि program condition check करके अलग-अलग actions perform करे।

उदाहरण:

  • अगर marks > 50 → Pass
  • अगर marks < 50 → Fail

यहां program एक condition के आधार पर decision ले रहा है—यही C++ decision making है।

If-Else Statement in C++

If-Else सबसे basic और powerful decision-making tool है।

Syntax:

if(condition) {
    // code
} else {
    // code
}

Example:

int marks = 60;

if(marks >= 50) {
    cout << "Pass";
} else {
    cout << "Fail";
}
यह example दिखाता है कि program कैसे condition-based decision लेता है।

Nested If (Multiple Decisions)

जब multiple conditions होती हैं, तब हम nested if का use करते हैं।

Example:

int marks = 85;

if(marks >= 50) {
    if(marks >= 80) {
        cout << "Distinction";
    } else {
        cout << "Pass";
    }
} else {
    cout << "Fail";
}

यह real-life grading system जैसा है।

Switch Statement in C++

Switch statement तब use होता है जब एक variable के कई possible values हों।

Syntax:

switch(expression) {
    case 1:
        // code
        break;
    case 2:
        // code
        break;
    default:
        // code
}

Example:

int day = 2;

switch(day) {
    case 1: cout << "Monday"; break;
    case 2: cout << "Tuesday"; break;
    default: cout << "Other Day";
}
Switch program को clean और readable बनाता है।

Logical Operators (AND, OR, NOT)

Logical operators decision making को और powerful बनाते हैं।

  • && (AND)
  • || (OR)
  • ! (NOT)

Example:

if(marks >= 50 && attendance >= 75) {
    cout << "Eligible";
}

यहां दो conditions मिलकर एक decision बना रही हैं।

Real-Life Use Cases 

Decision Making in C++ सिर्फ theory नहीं है—यह real world में हर जगह use होता है:

  • ATM machine (balance check)
  • Login system (username/password validation)
  • Online shopping (discount rules)

Example (Discount):

if(amount > 1000) {
    cout << "10% Discount";
}

Advanced Decision Making in C++ 

अब जब आपने basic Decision Making in C++ समझ लिया है, तो next level पर चलते हैं—जहाँ आप smart, optimized और professional-level decisions बना सकते हैं।

1. Ternary Operator (?:) – Short Decision Making

Ternary Operator एक shortcut है if-else statement का। यह code को छोटा और clean बनाता है।

Syntax:

condition ? expression1 : expression2;

Example:

int marks = 55;

string result = (marks >= 50) ? "Pass" : "Fail";
cout << result;

यहाँ एक ही line में C++ decision making किया गया है।

कब use करें?

  • ✔ Simple conditions में
  • ✔ जब readability maintain रहे

कब avoid करें?

  • ❌ Complex nested conditions में

2. Decision Trees Concept (निर्णय वृक्ष)

Decision Tree एक logical structure है जिसमें multiple conditions step-by-step evaluate होती हैं—जैसे एक tree structure।

Real-Life Analogy:

Imagine करें कि आप job apply कर रहे हैं:

  • अगर graduation complete है → next step
  • अगर experience है → shortlist
  • वरना → reject

Example Code:

int marks = 75;
int experience = 2;

if(marks >= 60) {
    if(experience >= 1) {
        cout << "Selected";
    } else {
        cout << "Need Experience";
    }
} else {
    cout << "Not Eligible";
}

यह एक structured decision making in C++ approach है।

3. Optimization Strategies (कोड को बेहतर बनाना)

Professional developers सिर्फ code नहीं लिखते—वे उसे optimize भी करते हैं।

Strategy 1: Reduce Nested If

Deep nested conditions code को confusing बना देती हैं।

Bad Practice:

if(a > 0) {
    if(b > 0) {
        if(c > 0) {
            cout << "Valid";
        }
    }
}

Optimized Version:

if(a > 0 && b > 0 && c > 0) {
    cout << "Valid";
}

Logical operators से decision making simplify करें।

Strategy 2: Use Early Return

if(marks < 50) {
    return "Fail";
}
return "Pass";

इससे unnecessary nesting avoid होती है।

Strategy 3: Switch vs If-Else

  • ✔ Fixed values → Switch
  • ✔ Range conditions → If-Else

सही structure चुनना ही smart C++ decision making है।

4. Common Mistakes (आम गलतियाँ)

  • ❌ = और == में confusion
  • ❌ Too many nested if statements
  • ❌ Missing break in switch
  • ❌ Unclear conditions

Example Mistake:

if(a = 5)  // गलत

Correct:

if(a == 5)

5. Practical Checklist (Quick Implementation Guide)

  • ✔ क्या condition clear है?
  • ✔ क्या logic simple किया जा सकता है?
  • ✔ क्या सही structure (if/switch) use हुआ है?
  • ✔ क्या readability maintain है?
  • ✔ क्या test cases run किए हैं?

6. Real-World Mini Project Idea

Student Result System बनाएं:

  • Input: marks
  • Output:
    • 90+ → Excellent
    • 70-89 → Good
    • 50-69 → Pass
    • <50 → Fail

इसमें आप if-else ladder, logical operators और decision making in C++ का पूरा use कर सकते हैं।

Actionable Tips (प्रैक्टिकल सुझाव)

  • ✔ हमेशा simple condition से शुरुआत करें
  • ✔ Nested if को limit करें (code readable रखें)
  • ✔ Switch का use करें जब multiple fixed options हों
  • ✔ Logical operators का smart use करें
  • ✔ Debugging के लिए conditions print करें

Pro Tip: अगर आपका code confusing लग रहा है, तो decision logic simplify करें—यही एक अच्छे developer की पहचान है।

FAQ (Frequently Asked Questions)

1. C++ में decision making क्या है?

यह process है जिसमें program conditions के आधार पर decisions लेता है।

2. If-else और switch में क्या अंतर है?

If-else complex conditions के लिए होता है, जबकि switch fixed values के लिए।

3. Nested if कब use करना चाहिए?

जब multiple conditions check करनी हों।

4. Logical operators क्यों जरूरी हैं?

ये multiple conditions को combine करने में मदद करते हैं।

5. क्या switch if-else को replace कर सकता है?

नहीं, दोनों अलग scenarios में use होते हैं।

📌 Further reading

Next
This is the most recent post.
Previous
Older Post

Post a Comment

Blogger

Your Comment Will be Show after Approval , Thanks

Support Our Content

Pay via UPI
Works with: GPay | PhonePe | Paytm | BHIM
anurajk.com@ptyes


More Payment Options / Scan QR →

Ads

 
↑ Top