Nested if-else Statement in C++ (C++ में Nested if-else स्टेटमेंट)

We can define one if-else statement inside another if or else block. This type of statement is called a Nested if-else Statement. It allows the programmer to check multiple test conditions and execute different statements according to the conditions.

हम एक if-else स्टेटमेंट के अंदर दूसरे if-else स्टेटमेंट को define कर सकते हैं। इस प्रकार के स्टेटमेंट को Nested if-else Statement कहा जाता है। यह प्रोग्रामर को दो या दो से अधिक conditions को check करने तथा उनके अनुसार अलग-अलग statements execute करने की सुविधा प्रदान करता है।

Types of Nested if-else Statements (Nested if-else के प्रकार)

Nested if-else statements can be written in different forms. Some common structures are given below.

Nested if-else statements को अलग-अलग structures में लिखा जा सकता है। कुछ सामान्य structures निम्नलिखित हैं।

1. Nested if-else inside an if block

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

    statements;
}
else
{
    statements;
}

2. if-else Ladder

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

3. Nested if-else inside both if and else blocks

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

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

    statements;
}

Example 1: Calculate Percentage and Division of a Student

Write a C++ program to calculate the division of a student based on the percentage obtained.

एक C++ प्रोग्राम लिखिए जो विद्यार्थी के प्रतिशत के आधार पर उसकी division प्रदर्शित करे।

Program:

#include <iostream>
using namespace std;

int main()
{
    float per;

    cout << "Enter Percentage of student: ";
    cin >> per;

    if (per >= 60)
    {
        cout << "I Division " << per << "%" << endl;
    }
    else
    {
        if (per >= 45)
        {
            cout << "II Division " << per << "%" << endl;
        }
        else
        {
            if (per >= 33)
            {
                cout << "III Division " << per << "%" << endl;
            }
            else
            {
                cout << "Fail!! " << per << "%" << endl;
            }
        }
    }

    return 0;
}

Output 1:

Enter Percentage of student: 75
I Division 75%

Output 2:

Enter Percentage of student: 50
II Division 50%

Output 3:

Enter Percentage of student: 40
III Division 40%

Output 4:

Enter Percentage of student: 25
Fail!! 25%

Explanation (व्याख्या)

  • If per >= 60, the student gets First Division.

  • Otherwise, the program checks whether per >= 45.

  • If true, the student gets Second Division.

  • Otherwise, it checks whether per >= 33.

  • If true, the student gets Third Division.

  • Otherwise, the student is declared failed.

यहाँ सबसे पहले per >= 60 condition check होती है। यदि यह false होती है, तो अगली condition per >= 45 check होती है। इसके बाद per >= 33 check होती है। यदि सभी conditions false होती हैं, तो विद्यार्थी को fail घोषित किया जाता है।

Example 2: Find the Greatest Among Three Numbers

Write a C++ program to find the greatest among three numbers.

एक C++ प्रोग्राम लिखिए जो तीन संख्याओं में से सबसे बड़ी संख्या ज्ञात करे।

Program:

#include <iostream>
using namespace std;

int main()
{
    float a, b, c;

    cout << "Enter Three Numbers: ";
    cin >> a >> b >> c;

    if (a > b)
    {
        if (a > c)
        {
            cout << a << " is greatest" << endl;
        }
        else
        {
            cout << c << " is greatest" << endl;
        }
    }
    else
    {
        if (b > c)
        {
            cout << b << " is greatest" << endl;
        }
        else
        {
            cout << c << " is greatest" << endl;
        }
    }

    return 0;
}

Output 1:

Enter Three Numbers: 10 25 15
25 is greatest

Output 2:

Enter Three Numbers: 50 20 80
80 is greatest

Explanation (व्याख्या)

First, the program compares a and b.

  • If a > b, it compares a with c.

  • Otherwise, it compares b with c.

  • The number that is greater than the other two is displayed as the greatest number.

सबसे पहले प्रोग्राम a और b की तुलना करता है।

  • यदि a > b है, तो a और c की तुलना होती है।

  • अन्यथा b और c की तुलना होती है।

  • जो संख्या बाकी दोनों संख्याओं से बड़ी होती है, उसे greatest number के रूप में प्रदर्शित किया जाता है।

Example 3: Check Whether a Year is a Leap Year

Write a C++ program to check whether a given year is a leap year or not.

एक C++ प्रोग्राम लिखिए जो यह check करे कि दिया गया वर्ष leap year है या नहीं।

Program:

#include <iostream>
using namespace std;

int main()
{
    int year;

    cout << "Enter Year: ";
    cin >> year;

    if (year % 400 == 0)
    {
        cout << year << " is leap year" << endl;
    }
    else
    {
        if (year % 100 == 0)
        {
            cout << year << " is not a leap year" << endl;
        }
        else
        {
            if (year % 4 == 0)
            {
                cout << year << " is leap year" << endl;
            }
            else
            {
                cout << year << " is not a leap year" << endl;
            }
        }
    }

    return 0;
}

Output 1:

Enter Year: 2024
2024 is leap year

Output 2:

Enter Year: 1900
1900 is not a leap year

Output 3:

Enter Year: 2000
2000 is leap year

Explanation (व्याख्या)

A year is a leap year if:

  • It is divisible by 400, or

  • It is divisible by 4 but not divisible by 100.

The program checks these conditions using nested if-else statements.

कोई वर्ष leap year होता है यदि:

  • वह 400 से पूरी तरह divisible हो, या

  • वह 4 से divisible हो लेकिन 100 से divisible न हो।

प्रोग्राम इन conditions को nested if-else statements की सहायता से check करता है।

Nested if-else is a selection control structure in C++. It is useful when a program needs to check multiple conditions and execute different statements based on the results.

C++ में Nested if-else एक selection control structure है। इसका प्रयोग तब किया जाता है जब प्रोग्राम में multiple conditions check करनी हों तथा उनके परिणाम के आधार पर अलग-अलग statements execute करने हों।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Programming Methodologies and Data structures using C++ Language with OOP's Concepts

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)