- The
if-else statement provides two way decisions only.
- Sometimes,
decisions must be made between more than two possibilities.
- At that
time, we can use nested-if else statement.
- C
language provides nested-if else. Nested if else means loop within loop.
- In, nested
if else statement, if the condition is true than statement(s)-1 (or statement(s)-2)
is executed.
- If the
condition is false then statement(s)-3 (or statement(s)-4) is executed.
- Syntax:
if(condition)
{
if(condition)
{
Statement(s)-1;
}
else
{
Statement(s)-2;
}
else
{
if(condition)
{
Statement(s)-3;
}
else
{
Statement(s)-4;
}
}
- Example: A C program to find out the largest number among three
numbers.
#include<stdio.h>
#include<conio.h>
void
main()
{
int a,b,c;
clrscr();
printf("Enter the values of a: ");
scanf("%d",&a);
printf("Enter the values of b: ");
scanf("%d",&b);
printf("Enter the values of c: ");
scanf("%d",&c);
if(a>b)
{
if(a>c)
{
printf(" a is largest number.");
}
else
{
printf(" c is largest number.");
}
}
else
{
if(b>c)
{
printf(" b is largest number.");
}
else
{
printf(" c is largest number.");
}
}
getch();
}
OUTPUT:
Enter the values of a:15
Enter the values of b:25
Enter the values of c:45