Home >>C Tutorial >C If-else statements

If-else statements in C

If-else conditional statements in C

In order to perform the operations based on various specific condition, the if-else statement is used in the C programming. The point to be noted is that if and only if the given condition is true then only the operations that are specified in ‘if’ block are executed.

Here are the variants of if statement in C language:

  • If statement
  • If-else statement
  • If else-if ladder
  • Nested if

If Statement

The if statement in C programming is generally used to check various given condition and depending upon the correctness of that condition, performs some operations. Where there is a need to perform the different operations for the different conditions, the if statement is mostly used.
Here is the syntax of the if statement:

if(expression)
{  
//code to be executed  
}  

Here is an example of the if statement in C:

#include<stdio.h>    
int main()
{    
int number=0;    
printf("Enter a number:");    
scanf("%d",&number);    
if(number%2==0){    
printf("%d is even number",number);    
}    
return 0;  
}    
Output :
Enter a number:4
4 is even number

Find the Largetst Number

Here is a program depicting to find the largest number of the three provided number:

#include <stdio.h>  
int main()  
{  
    int a, b, c;   
     printf("Enter three numbers?");  
    scanf("%d %d %d",&a,&b,&c);  
    if(a>b && a>c)  
    {  
        printf("%d is largest",a);  
    }  
    if(b>a  && b > c)  
    {  
        printf("%d is largest",b);  
    }  
    if(c>a && c>b)  
    {  
        printf("%d is largest",c);  
    }  
    if(a == b && a == c)   
    {  
        printf("All are equal");   
    }  
} 
Output :
Enter three numbers?
12 23 34
34 is largest

If-else statement in C

The if-else statement in C is generally used to perform two operations for a single condition. The if-else statement is basically an extension to the if statement by which users can perform two different operations, i.e., one to determine the correct value and other to determine the incorrect value. Please note that if and else block cannot be executed simultaneously.
Here is the syntax of the if-else statement:

if(expression)
{  
//code to be executed if condition is true  
}else{  
//code to be executed if condition is false  
}  

Here is an example of the If-else statement:

#include<stdio.h>    
int main()
{    
int number=0;    
printf("enter a number:");    
scanf("%d",&number);     
if(number%2==0)
{    
printf("%d is even number",number);    
}    
else
{    
printf("%d is odd number",number);    
}     
return 0;  
}    
Output :
enter a number:4
4 is even number
enter a number:5
5 is odd number

Here is another example to find out whether a person is eligible to vote or not:

#include <stdio.h>  
int main()  
{  
    int age;   
    printf("Enter your age?");   
    scanf("%d",&age);  
    if(age>=18)  
    {  
        printf("You are eligible to vote...");   
    }  
    else   
    {  
        printf("Sorry ... you can't vote");   
    }  
}  
Output :
Enter your age?18
You are eligible to vote...
Enter your age?13
Sorry ... you can't vote

If else-if ladder statement in C

The if-else-if ladder statement in C programming is basically an extension to the if-else statement. In the scenario where there are multiple cases to be performed for different conditions, this condition is used. In if-else-if ladder statement, the statements defined in the if block will be executed if the given condition is true, or else, the statements defined in the else-if block will be executed, if the other condition is true, at the end if none of the given condition is found to be true then the statements defined in the else block will be executed.
Please note that there are multiple else-if blocks possible.

Syntax

if(condition1)
{  
//code to be executed if condition1 is true  
}
else if(condition2)
{  
//code to be executed if condition2 is true  
}  
else if(condition3)
{  
//code to be executed if condition3 is true  
}  
...  
else
{  
//code to be executed if all the conditions are false  
}  

Here is an example of if-else if statement in C:

#include<stdio.h>    
int main()
{    
int number=0;    
printf("enter a number:");    
scanf("%d",&number);     
if(number==10)
{    
printf("number is equals to 10");    
}    
else if(number==50)
{    
printf("number is equal to 50");    
}    
else if(number==100)
{    
printf("number is equal to 100");    
}    
else
{    
printf("number is not equal to 10, 50 or 100");    
}    
return 0;  
}   
Output :
enter a number:4
number is not equal to 10, 50 or 100
enter a number:50
number is equal to 50

Print Grade Card

Here is another example of if else-if statement depicting a program to calculate the grade of the student according to the specified marks:

#include <stdio.h>  
int main()  
{  
    int marks;   
    printf("Enter your marks?");  
    scanf("%d",&marks);   
    if(marks > 85 && marks <= 100)  
    {  
        printf("Congrats ! you scored grade A ...");   
    }  
    else if (marks > 60 && marks <= 85)   
    {  
        printf("You scored grade B + ...");  
    }  
    else if (marks > 40 && marks <= 60)   
    {  
        printf("You scored grade B ...");  
    }  
    else if (marks > 30 && marks <= 40)   
    {  
        printf("You scored grade C ...");   
    }  
    else   
    {  
        printf("Sorry you are fail ...");   
    }  
}  
Output :
Enter your marks?10
Sorry you are fail ...
Enter your marks?40
You scored grade C ...
Enter your marks?90
Congrats ! you scored grade A ...

Popular Tutorials