Nested if-else


nested if-else

When we make a program where one condition is dependent on another condition we use a nested loop and when the condition is satisfied it will execute the corresponding code block. In simple words Nested if statement in C is the nesting of if statement within another if statement and nesting of if statement with an else statement. 

 

Syntax   

if(condition 1) 

{ 

    if (condition n1) // nested if else 

    { 

        printf("execute block 1"); 

    } 

    else  

    { 

        printf("execute block 2"); 

    } 

} 

else 

{ 

    if (condition n2){ 

        printf("execute block 3");     

    } 

    else  

    { 

        printf ("execute block 4"); 

    } 

} 

 

 

Flowchart  

  

 

QUESTION:- a print smallest number between 3 user input. 


#include <stdio.h>
#include <conio.h>
void main()
{
    int abc;
    printf("enter a value of a: ");
    scanf("%d", &a);
    printf("enter a value of b: ");
    scanf("%d", &b);
    printf("enter a value of c: ");
    scanf("%d", &c);
    if (a < b)
    {
        if (a < c)
        {
            printf("%d is smallest"a);
        }
        else
        {
            printf("%d is smallest"c); // default value of nested if-else.
        }
    }
    else
    {
        printf("%d is smallest"b);
    }
    getch();
}

output  


enter a value of a: 10 

enter a value of b: 12 

enter a value of c: 4 

4 is the smallest. 


Follow our page for regular updates.

0 Comments