Nested if-else Statement in C Language with Examples

We can use one if-else statement inside another if or else block. This type of statement is called a Nested if-else Statement. It allows programmers to check multiple conditions and execute different blocks of code depending on the results.

C language में एक if-else Statement के अंदर दूसरे if या else Statement को लिखा जा सकता है। इस प्रकार के Statement को Nested if-else Statement कहा जाता है। इसकी सहायता से प्रोग्रामर एक से अधिक Conditions की जाँच करके उनके परिणाम के अनुसार अलग-अलग Code Execute कर सकता है।

Syntax of Nested if-else Statement

Nested if-else statements can be arranged in different ways. The following are three commonly used structures.

Nested if-else Statements को अलग-अलग तरीकों से व्यवस्थित किया जा सकता है। नीचे इनके तीन सामान्य Structures दिए गए हैं।

1. Nested if-else Inside the if Block

In this structure, another if-else statement is written inside the body of the outer if block. The inner condition is checked only when the outer condition is true.

इस Structure में Outer if ब्लॉक के अंदर एक अन्य if-else Statement लिखा जाता है। Inner Condition की जाँच तभी की जाती है जब Outer Condition सत्य होती है।

Syntax:

if (condition1)
{
if (condition2)
{
statements;
}
else
{
statements;
}
statements;
}
else
{
statements;
}

2. if-else Ladder

In an if-else ladder, multiple conditions are checked one by one. When a condition becomes true, its corresponding block is executed and the remaining conditions are skipped. If none of the conditions is true, the final else block is executed, if provided.

if-else Ladder में एक से अधिक Conditions की जाँच क्रमशः की जाती है। जैसे ही कोई Condition सत्य होती है, उससे संबंधित Block Execute होता है और शेष Conditions को Skip कर दिया जाता है। यदि कोई भी Condition सत्य नहीं होती है, तो अंतिम else Block Execute होता है, यदि दिया गया हो।

Syntax:

if (condition1)
{
statements;
}
else if (condition2)
{
statements;
}
else if (condition3)
{
statements;
}
else
{
statements;
}

Note: An if-else ladder is a chain of conditional statements. It is commonly studied with nested if-else, but it is not the same as placing an entire if-else statement inside another block.

नोट: if-else Ladder Conditional Statements की एक श्रृंखला होती है। इसे सामान्यतः Nested if-else के साथ पढ़ाया जाता है, लेकिन यह किसी दूसरे Block के अंदर पूरा if-else Statement लिखने के समान नहीं है।

3. Nested if-else Inside Both if and else Blocks

In this structure, an inner if-else statement is written inside both the outer if block and the outer else block. This structure is useful when different conditions need to be checked in both cases of the outer condition.

इस Structure में Outer if तथा Outer else दोनों Blocks के अंदर अलग-अलग Inner if-else Statements लिखे जाते हैं। यह Structure तब उपयोगी होता है जब Outer Condition के True और False दोनों मामलों में अलग-अलग Conditions की जाँच करनी हो।

Syntax:

if (condition1)
{
if (condition2)
{
statements;
}
else
{
statements;
}
}
else
{
if (condition3)
{
statements;
}
else
{
statements;
}
}

Example 1: Find the Greatest Number Among Three Numbers

The following program uses nested if-else statements to find the greatest among three numbers entered by the user.

निम्नलिखित प्रोग्राम Nested if-else Statements का प्रयोग करके यूजर द्वारा इनपुट की गई तीन संख्याओं में से सबसे बड़ी संख्या ज्ञात करता है।

Program:

#include <stdio.h>
int main(void)
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b)
{
if (a > c)
{
printf("A is greatest.");
}
else
{
printf("C is greatest.");
}
}
else
{
if (b > c)
{
printf("B is greatest.");
}
else
{
printf("C is greatest.");
}
}
return 0;
}

Output:

Enter three numbers: 25 40 15
B is greatest.

Explanation:

  1. Three integer values are accepted from the user.

  2. First, the condition a > b is checked.

  3. If it is true, the inner condition a > c is checked.

  4. If a > b is false, the inner condition b > c is checked.

  5. According to the conditions, the greatest number is displayed.

व्याख्या:

  1. यूजर से तीन Integer वैल्यू इनपुट की जाती हैं।

  2. सबसे पहले a > b Condition की जाँच की जाती है।

  3. यदि यह सत्य होती है, तो Inner Condition a > c की जाँच की जाती है।

  4. यदि a > b असत्य होती है, तो Inner Condition b > c की जाँच की जाती है।

  5. Conditions के परिणाम के अनुसार सबसे बड़ी संख्या प्रदर्शित की जाती है।

Note: This example displays one of the greatest values when numbers are equal; it does not separately report ties.

नोट: यदि दो या अधिक संख्याएँ समान और सबसे बड़ी हों, तो यह उदाहरण उनमें से केवल एक वैल्यू प्रदर्शित करता है। यह समानता की अलग से जानकारी नहीं देता।

Example 2: Check Positive, Negative or Zero Using if-else Ladder

The following program uses an if-else ladder to determine whether a number is positive, negative or zero.

निम्नलिखित प्रोग्राम if-else Ladder का प्रयोग करके यह निर्धारित करता है कि कोई संख्या Positive, Negative या Zero है।

Program:

#include <stdio.h>
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("Number is positive.");
}
else if (num < 0)
{
printf("Number is negative.");
}
else
{
printf("Number is zero.");
}
return 0;
}

Output:

Enter a number: -10
Number is negative.

Explanation:

  1. The program accepts an integer from the user.

  2. If num > 0, the number is positive.

  3. Otherwise, if num < 0, the number is negative.

  4. If both conditions are false, the number is zero.

व्याख्या:

  1. प्रोग्राम यूजर से एक Integer इनपुट करता है।

  2. यदि num > 0 सत्य होता है, तो संख्या Positive होती है।

  3. अन्यथा यदि num < 0 सत्य होता है, तो संख्या Negative होती है।

  4. यदि दोनों Conditions असत्य होती हैं, तो संख्या Zero होती है।

Example 3: Check Eligibility for Admission

A student is eligible for admission if their marks are at least 50. If the student meets this condition, another condition checks whether the marks are at least 75 to determine whether the student qualifies for a scholarship.

यदि किसी विद्यार्थी के अंक कम से कम 50 हैं, तो वह Admission के लिए पात्र है। यदि विद्यार्थी इस Condition को पूरा करता है, तो दूसरी Condition के माध्यम से जाँचा जाता है कि उसके अंक कम से कम 75 हैं या नहीं, ताकि Scholarship की पात्रता निर्धारित की जा सके।

Program:

#include <stdio.h>
int main(void)
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 50)
{
printf("You are eligible for admission.\n");
if (marks >= 75)
{
printf("You are eligible for scholarship.");
}
else
{
printf("You are not eligible for scholarship.");
}
}
else
{
printf("You are not eligible for admission.");
}
return 0;
}

Output 1:

Enter your marks: 85
You are eligible for admission.
You are eligible for scholarship.

Output 2:

Enter your marks: 60
You are eligible for admission.
You are not eligible for scholarship.

Output 3:

Enter your marks: 40
You are not eligible for admission.

Explanation:

  1. The program accepts the student's marks.

  2. The outer condition checks whether marks are at least 50.

  3. If true, the inner if-else checks whether marks are at least 75.

  4. If the outer condition is false, the student is declared ineligible for admission.

व्याख्या:

  1. प्रोग्राम विद्यार्थी के अंक इनपुट करता है।

  2. Outer Condition यह जाँचती है कि अंक कम से कम 50 हैं या नहीं।

  3. यदि यह सत्य होती है, तो Inner if-else यह जाँचता है कि अंक कम से कम 75 हैं या नहीं।

  4. यदि Outer Condition असत्य होती है, तो विद्यार्थी को Admission के लिए अपात्र घोषित किया जाता है।

Difference Between Nested if-else and if-else Ladder

Nested if-elseif-else Ladder
An if-else statement is placed inside another block.Multiple conditions are arranged in a sequence.
Inner conditions may depend on the result of outer conditions.Conditions are checked one by one until one is true.
Useful for hierarchical or dependent decisions.Useful for selecting one option from several alternatives.

हिन्दी में अंतर:

Nested if-elseif-else Ladder
एक if-else Statement को दूसरे Block के अंदर लिखा जाता है।अनेक Conditions को क्रमबद्ध तरीके से लिखा जाता है।
Inner Conditions का निष्पादन Outer Conditions के परिणाम पर निर्भर हो सकता है।Conditions की जाँच क्रमशः तब तक होती है जब तक कोई एक सत्य न हो जाए।
Hierarchical या एक-दूसरे पर निर्भर Decisions के लिए उपयोगी है।कई विकल्पों में से एक विकल्प चुनने के लिए उपयोगी है।

Important Points About Nested if-else

  1. An if or else block can contain another if-else statement.

  2. Nested if-else can be used to check multiple conditions.

  3. The inner condition is checked only when program control reaches the inner statement.

  4. Curly braces {} help identify the beginning and ending of each block.

  5. An else is associated with the nearest unmatched if in the same block.

  6. An if-else ladder is useful when several alternative conditions are required.

  7. Proper indentation makes nested conditions easier to understand.

  8. किसी if या else Block के अंदर दूसरा if-else Statement लिखा जा सकता है।

  9. Nested if-else का प्रयोग एक से अधिक Conditions की जाँच के लिए किया जा सकता है।

  10. Inner Condition की जाँच तभी होती है जब Program Control उस Inner Statement तक पहुँचता है।

  11. Curly Braces {} प्रत्येक Block की शुरुआत और समाप्ति को पहचानने में सहायता करती हैं।

  12. else उसी Block में अपने सबसे नजदीकी ऐसे if से जुड़ता है जिसका else पहले से नहीं है।

  13. कई वैकल्पिक Conditions की जाँच के लिए if-else Ladder उपयोगी होती है।

  14. सही Indentation से Nested Conditions को समझना आसान हो जाता है।

Nested if-else statements are useful for making complex decisions in C programming. They allow programmers to check conditions at different levels and execute appropriate blocks of code. The if-else ladder provides another convenient way to handle multiple alternative conditions.

Nested if-else Statements C Programming में जटिल निर्णय लेने के लिए उपयोगी होते हैं। इनकी सहायता से प्रोग्रामर अलग-अलग स्तरों पर Conditions की जाँच करके उपयुक्त Code Execute कर सकते हैं। Multiple Alternative Conditions को Handle करने के लिए if-else Ladder भी एक उपयोगी तरीका है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025