Nested If….Else Structure

Nested If….Else Structure

Example nested structure

Nested if else structure is an extension of if else structure. If else structure comes under selection looping structure. When a programmer wants compiler to select one of the many alternatives available, programmer can use nested if else structure or ladder. Nested if else structure reduces the length of the code. We can have max 32 nested if statements. It is an alternative to switch case. Let’s see an example of nested if else structure in C/C++.

Example : Find the greatest number of the three numbers entered by the user.

1. Algorithm

1. START
2. PRINT "ENTER THREE NUMBERS"
3. READ a,b,c
4. IF a == b == c THEN
        PRINT "ALL THREE NUMBERS ARE EQUAL"
   ELSE IF a>b AND a>c THEN
        PRINT "FIRST NUMBER IS GREATER"
    ELSE IF b>a AND b>c THEN
        PRINT "SECOND NUMBER IS GREATER"
      ELSE 
            PRINT "THIRD NUMBER IS GREATER"
      [END OF IF]
    [END OF IF]
   [END OF IF]
5. EXIT

2. Code

C Program

#include<stdio.h>

int main()
{
    int a,b,c;
    printf("\nEnter three nos:");
    scanf("%d%d%d",&a,&b,&c);

    if(a == b && b == c)
    {
        printf("All the numbers are equal");
    }
    else if (a>b && a>c)
    {
        printf("First number is greater");
    }
    else if (b>a && b>c)
    {
        printf("Second number is greater");
    }
    else
    {
        printf("Third number is greater");
    }

    return 0;
}

C++ Program

#include<iostream>

using namespace std;

int main()
{
    int a,b,c;
    cout<<"\nEnter three nos:";
    cin>>a>>b>>c;

    if(a == b && b == c)
    {
        cout<<"All the numbers are equal";
    }
    else if (a>b && a>c)
    {
        cout<<"First number is greater";
    }
    else if (b>a && b>c)
    {
        cout<<"Second number is greater";
    }
    else
    {
        cout<<"Third number is greater";
    }

    return 0;
}

3. Output

Example of Nested If Else Structure
Example of Nested If Else Structure

4. Program Parts

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. printf/cout is the keyword use to print a message.
7. scanf/cin is the keyword use to store values in variables.
8. return statement is use to return a value. If any error occurs the program will return 0.

5. Explanation

First the compiler will check the necessary header files. If required header files are present compiler will enter main function. Now three variables are declared (a,b,c) of integer data type. User is asked to enter three variables. Now first if statement will check if the three numbers entered by the user are equal. If the condition is true then compiler will print “Three numbers are equal” and it will come out of nested if else structure. Else it will check rest of the conditions.

Conclusion

So this was the example of nested if else structure in C/C++. There are many other possible ways to find greatest of three numbers. If you know any of them do tell us in the comment section below. Hope you liked it. If yes then please do share it with your friends and programming buddies. Also tell us what other programs you would like us to post.

For more such content follow us on instagram. Visit again!

ErrorFreeProgram.

3 COMMENTS

comments user
sheru

a1 awesome good work👏👏

    comments user
    ErrorFreeProgram

    thank you so much 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *