What is if-else statement
- The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test (ie, whether the outcome is true or false).
- In C if else control statement, group of statements are executed when condition is true. If condition is false, then else part statements are executed.
- If the condition specified in the if statement evaluates to true, the statements inside the if-block are executed and then the control gets transferred to the statement immediately after the if-block.
- Even if the condition is false and no else-block is present, control gets transferred to the statement immediately after the if-block.
- The else part is required only if a certain sequence of instructions needs to be executed if the condition evaluates to false.
Syntax:
Flowchart of if-else statement in C
Example program for if else statement in C:
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf("n Enter a number:");
scanf("%d", &a);
if(a>0)
{ printf( "n The number %d is positive.",a);
}
else
{
printf("n The number %d is negative.",a);
}getch();
}
#include<conio.h>
void main( )
{
int a;
printf("n Enter a number:");
scanf("%d", &a);
if(a>0)
{ printf( "n The number %d is positive.",a);
}
else
{
printf("n The number %d is negative.",a);
}getch();
}
/*The following program compares two strings to check whether they are equal or not.*/
#include <stdio.h>
#include <string.h>
#include <conio.h>
void main( )
{char a[20] , b[20];
printf("n Enter the first string:");
scanf("%s",a);
printf("\n Enter the second string:");
scanf("%s",b);
if((strcmp(a,b)==0))
{
printf("\nStrings are the same");
}
else
{
printf("\n Strings are different");
}
getch();
}
#include <string.h>
#include <conio.h>
void main( )
{char a[20] , b[20];
printf("n Enter the first string:");
scanf("%s",a);
printf("\n Enter the second string:");
scanf("%s",b);
if((strcmp(a,b)==0))
{
printf("\nStrings are the same");
}
else
{
printf("\n Strings are different");
}
getch();
}
/*The following program checks whether the entered number is positive or negative.*/
#include <stdio.h>
int main()
{
if (m == n)
int m=40,n=20;
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
int main()
{
if (m == n)
int m=40,n=20;
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
Output:
m and n are not equal
Let's see the simple example of even and odd number using if-else statement in C language.
#include<stdio.h>
#include<conio.h>
void main()
{
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}getch();
}
#include<conio.h>
void main()
{
int number=0;
clrscr();
printf("enter a number:");
scanf("%d",&number);
if(number%2==0)
{
printf("%d is even number",number);
}
else
{
printf("%d is odd number",number);
}getch();
}
Output
enter a number:4
4 is even number
enter a number:5
5 is odd number
4 is even number
enter a number:5
5 is odd number