C++ nested if else stetnent

You can combine multiple if / if-else / if-else-if ladders when a series of decisions are involved.

Some use case :
1.
    
    if(condition)
    {
        //black of statement

        if(condition)
        {
            //block of statement
        }
    }
    

2.
    
    if(condition)
    {
        //black of statement
    }
    else
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }
    

3.
    
    if(condition)
    {
        //black of statement
    }
    else if(condition)
    {

        if(condition)
        {
            //block of statement
        }
        else
        {
            //block of statement
        }

    }
    

Example :

#include <iostream>
using namespace std;
int main()
{
    int numb1, numb2;
    cout<<"Enter two integers : ";

    cin>>numb1>>numb2;
    //checking whether two integers are equal.
    if(numb1==numb2)
    {
        cout<<"\nResult: "<<numb1<<" = "<<numb2;
    }
    else
    {
        //checking whether numb1 is greater than numb2.
        if(numb1>numb2)
        {
            cout<<"\nResult: "<<numb1<<" > "<<numb2;
        }
        else
        {
            cout<<"\nResult: "<<numb2<<" > "<<numb1;
        }
    }

    return 0;
}
Output :
 Enter two integers : 7 9
 Result: 9 > 7

0 Comments