Notes in Hindi

Difference between if-else and else if Ladder in Hindi

/ BCA / Programming with C and CPP

Difference between if-else and else if Ladder in Hindi

Difference between if-else and else if Ladder in Hindi

Introduction

Programming में जब हमें किसी condition को check करना होता है, तब हम Decision Making Statements का उपयोग करते हैं। C Language में इसके लिए if-else और else if Ladder का बहुत उपयोग होता है। ये दोनों statements दिखने में मिलते-जुलते हैं लेकिन इनके उपयोग और कार्यप्रणाली में अंतर होता है। इस लेख में हम दोनों के बीच का अंतर, उनके वास्तविक उपयोग और उन्हें debug करने के तरीके को विस्तार से समझेंगे।

What is if-else Statement?

if-else एक दो-तरफा निर्णय (two-way decision) लेने वाला statement है। यदि कोई condition true होती है, तो if block का code चलेगा अन्यथा else block का code चलेगा। इसका उपयोग तब किया जाता है जब सिर्फ दो ही स्थितियों में से एक को चुनना होता है।

int age = 20;
if(age >= 18) {
  printf("Eligible to vote");
} else {
  printf("Not eligible to vote");
}

What is else if Ladder?

जब एक से अधिक conditions को check करना हो, तब else if Ladder का उपयोग किया जाता है। यह एक क्रमिक जांच (sequential checking) की प्रक्रिया होती है। पहले if condition को check किया जाता है, फिर else if conditions को ऊपर से नीचे तक जांचा जाता है और यदि कोई condition true मिल जाती है तो उसका block run होता है, बाकी को skip कर दिया जाता है।

int marks = 75;
if(marks >= 90) {
  printf("Grade A");
} else if(marks >= 75) {
  printf("Grade B");
} else if(marks >= 60) {
  printf("Grade C");
} else {
  printf("Grade D");
}

Key Differences between if-else and else if Ladder

  • if-else केवल दो conditions को संभालता है जबकि else if Ladder कई conditions को संभाल सकता है।
  • if-else में true या false के आधार पर दो ही रास्ते होते हैं जबकि else if में multiple options हो सकते हैं।
  • else if Ladder एक structured way देता है multiple conditions को check करने का।
  • if-else logic छोटे decisions के लिए उपयोगी होता है जबकि else if बड़े और complex decisions के लिए।

Performance Note

else if Ladder sequentially ऊपर से नीचे चलता है, इसलिए यदि top condition हमेशा true होती है, तो performance बेहतर होती है। अधिक conditions होने पर performance पर थोड़ा असर पड़ सकता है।

Real-Time Use of else if Ladder in C in Hindi

1. Grading System

छात्रों को उनके अंकों के आधार पर Grade देना:

int marks;
printf("Enter marks: ");
scanf("%d", &marks);

if(marks >= 90) {
  printf("Grade: A");
} else if(marks >= 80) {
  printf("Grade: B");
} else if(marks >= 70) {
  printf("Grade: C");
} else if(marks >= 60) {
  printf("Grade: D");
} else {
  printf("Grade: F");
}

2. Menu Driven Program

किसी User से input लेकर operation करना:

int choice;
printf("1. Add\n2. Subtract\n3. Multiply\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if(choice == 1) {
  printf("Addition function executed");
} else if(choice == 2) {
  printf("Subtraction function executed");
} else if(choice == 3) {
  printf("Multiplication function executed");
} else {
  printf("Invalid choice");
}

3. Salary Calculation

Employee की performance के आधार पर bonus देना:

char grade;
printf("Enter grade (A/B/C): ");
scanf(" %c", &grade);

if(grade == 'A') {
  printf("Bonus: ₹10000");
} else if(grade == 'B') {
  printf("Bonus: ₹7000");
} else if(grade == 'C') {
  printf("Bonus: ₹5000");
} else {
  printf("No bonus");
}

Debugging Tips for else if Ladder Statement in Hindi

1. Condition Properly Check करें

  • हर if और else if condition को ध्यान से पढ़ें, कहीं गलत comparison operator (= vs ==) तो नहीं है।
  • जैसे: if(x = 5) गलती से assignment हो जाएगा, जबकि if(x == 5) comparison है।

2. Logical Sequence बनाएं

  • else if में conditions को descending या logical order में रखें ताकि सही block सबसे पहले execute हो।
  • जैसे: if(x > 90) पहले हो फिर if(x > 80) उसके बाद।

3. Last में else Block जरूर रखें

  • else block को अंत में रखने से unexpected inputs को handle किया जा सकता है।
  • यह default case के रूप में काम करता है।

4. Debug Print Statement Use करें

  • हर block में printf() या log जोड़ें ताकि runtime में आपको पता चले कि कौन सा block run हुआ।
if(score > 90) {
  printf("Inside A block\n");
} else if(score > 80) {
  printf("Inside B block\n");
} else {
  printf("Inside default block\n");
}

5. Dry Run करें

  • Code लिखने के बाद Paper पर या manually input डालकर step-by-step dry run करें।

6. Comments Use करें

  • हर block के ऊपर comment लिखें कि वो किस condition के लिए काम कर रहा है।
// Check if user is Admin
if(role == 1) {
  printf("Admin Panel");
}

7. Extra Semicolon से बचें

  • if(condition); के बाद गलती से semicolon न लगाएं, इससे condition काम नहीं करेगी।

8. Brackets Always Use करें

  • एक ही line का code हो तो भी { } का उपयोग करें जिससे गलती न हो।

Please Give Us Feedback