nested if….else statement


nested if….else statement

When a series of decisions involved, We may need to use more than one if..else statements in the nested form. The if statement may itself contain another if statement is known as nested if statement. The if statement may be nested as deeply as you need to nest it. One block of code will only be executed if two conditions are true. Condition 1 is tested first and then condition 2 is tested. The second if condition is nested in the first. The second if condition is tested only when the first condition is true else the program flow will skip to the corresponding else statement and executes statement-3.

if (condition 1)
    if (condition 2)
       statement-1;
                else
                   statement-2;
else 
              statement-3;
Next-statement;



































Sample Program to print large number among three numbers.

#include<stdio.h>
#include<conio.h>
void main ()
{
            int a,b,c;
            printf(“Enter a,b,c: “);
            scanf(“%d%d%d”,&a,&b,&c);

            if(a>b)
{
            if(a>c)
            {
                        printf(“a is large”);
            }
            else
            {
                        printf(“c is large”);
            }
}
else
{
                        if(b>c)
                        {
                        printf(“b is large”);
            }
            else
            {
                        printf(“c is large”);
            }

}
}

Output:-

Enter a,b,c: 4
23
11
b is large

No comments:

Post a Comment