Nested if-esle statement - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 5 November 2017

Nested if-esle statement

You can combine multiple if / if-else / if-else-if ladders when a series of decisions are involved. So you can make sure that your program executes certain instructions when a series of conditions are met.
Some use case :
1.
    
        if(condition)
        {
        //block 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 <stdio.h>
    int main()
    {
    int numb1, numb2;
    printf("Enter two integers : ");

    scanf("%d %d",&numb1,&numb2);
    //checking whether two integers are equal.
    if(numb1==numb2)
    {
    printf("\nResult: %d = %d",numb1,numb2);
    }
    else
    {
    //checking whether numb1 is greater than numb2.
    if(numb1>numb2)
    {
    printf("\nResult: %d > %d",numb1,numb2);
    }
    else
    {
    printf("\nResult: %d > %d",numb2,numb1);
    }
    }

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

No comments:

Post a Comment

Post Top Ad