if else conditional statement in C programming language:

syntax:

if(condition)
{
true block;
}
else
{
false block;
}

Explanation:

First if will check the conditon if the condition is true then true block will be exectued. Suppose the condition becomes false then false block will executed. There is no such case to execute both if and else part either one case will execute at a time.

When we need to use if else statement:

There are two possible cases are there one is true and another one is false. If it is not true then that will be consider as false. We need to use if else statement to check whether the condition is true or false. If its is true what to do or if it is false what to do like that you need to write the code.

Example:

Write a program to check whether the given year is leap year or not.

 

#include <stdio.h>

int main()
{
int year;
printf(“Enter the year : “);
scanf(“%d”,&year);
if(year%4 == 0)
{
printf(“%d is leap year\n”,year);
}
else
{
printf(“%d is not leap year\n”,year);
}
}

Output:

Enter the year:

2004

2004 is leap year

================================

Try by yourself:

Write a program to check given number is odd or even

Write a program to check the password is correct or not.

Hint: just get the user name and password from the user. Check both username and password is matching or not.

Write a program to find the age calculation using if else statement.

Hint : Get current date, month and year from the user or get it from system date.

Get birth date, month and year from the user. then get the different between both.

First subtract current date and birth date. if the current date is less than birth date add 30 days and reduce one month.