- In c language, if else statement provides two way decision only.
- In some case, we need to check multiple choice.
- At that time, we can use if...else if ladder(multiple if else statement).
- if...else if ladder provides multi-way decision. There may be more than one condition in Program at that time use if..else if ladder.
syntax:
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else if(condition-3)
statement-3;
.
.
.
else if(condition-n)
statement-n;
else
statement;
- In if..else if ladder, the condition-1 is true than it will execute the statement-1.
- If the condition-2 is true than it will execute the statement-2. Same way it will check
All the condition.
Flow Chart:
Example: A c program to check the given year is leap year or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("Enter a year:");
scanf("%d", &year);
if(year%4 == 0)
printf("\n This is a leap year.", year);
else if (year%400 == 0)
printf("\n This is a leap year.", year);
else if(year%100 == 0)
printf("\n This is not a leap year.", year);
else
printf("\n This is not a leap year.", year);
getch();
}
OUTPUT:
Enter a year: 2011
This is not a leap year.
No comments:
Post a Comment