# Difference between If Else and Nested If Else in C

## What is the If Else statement in C?

In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the specified conditions in the if block are true.

There are some variants of if statements in the C programming language.

• If statement
• If else statement
• Nested if else

### If Statement

If statements in C program are used to check the condition given by the user and perform some operation depending on the accuracy of the condition.  We use the if statement when we need to perform different operations in different scenarios.

Syntax:

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

Let us discuss how if condition works through the following flowchart: Example 1:

``````//program to check even numbers using only if statement
#include<stdio.h>
#include<conio.h>
int main(){
int number=0;
printf("Enter a number to check even:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
getch();
return 0;
}
``````

Output:

``````Enter the number to check even: 4
4 is even number
``````

Example 2:

``````//program to check the largest number using only if statement in C program
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers to check largest number\n");
scanf("%d %d %d",&a,&b,&c);
if(a>b && a>c)
{
printf("%d is largest number",a);
}
if(b>a  && b > c)
{
printf("%d is largest number",b);
}
if(c>a && c>b)
{
printf("%d is largest number",c);
}
if(a == b && a == c)
{
printf("All number are equal");
}
}
``````

Output:

``````Enter three numbers to check largest number
10
20
30
30 is largest number
``````

### If Else Statement

In C language, the if else statements are used to perform two operations for a single condition in one program. We can also say that the if else condition is an extension of the If condition.

In other words, with the help of the If else condition, we can perform two operations; one for the accuracy or rightness of that condition, and other for the wrongness of the condition in the C program. We can not execute the block of if and else statement simultaneously.

Syntax:

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

Let’s discuss how if else condition works through the following flowchart: ### Working of If else statements in C

With the help of the if and else statements, we can easily decide the result according to the given conditions by the user in the C program. If the given condition is true, we execute the logical body of the If statement, and the inside body of the else statement will not be executed.

Similarly, if the given condition inside the If statement is false, it will ignore the If statement, and execute the else statement of the C program.

Let's take an example to understand more clearly the use of the If else statement in C:

Suppose, we have the expression ABC in the program:

If the ABC condition is true then

• Statement inside the If statement will be executed, and the else statement will be ignored.

If the ABC condition is not true then

• Statement insdie the if will be ignored by the complier, and the else statement will be executed.

Example 1:

``````//program to check number is even or odd using if else condition
#include<stdio.h>
int main(){
int number=0;
printf("Enter a number to check even or odd:\n");
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 to check even or odd
3
3 is odd number
``````

Example 2:

``````// program to check number Armstrong using if else condition in C

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("Enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("%d is an armstrong  number.");
else
printf("%d is not an armstrong number.");
return 0;
}
``````

Output:

``````Enter the number=121
121 is not an armstrong number.
``````

Example 3:

``````// program to check leap year or not using if else ladder statements in C program

#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}
``````

Output:

``````Enter a year: 1999
1999 is not a leap year
``````

### Advantages of the If Else Condition in C

• With the help of the if-else condition, we can make a good decision in programming and also we can execute the right program or code.
• It also helps in the debugging of code.

### Disadvantages of If Else Condition in C

• The use of If else condition can increase the number of code that we have to test.
• It becomes unreadable and complex if there are a lot of If else statements in the program.

### What is a Nested If Else Statement in C?

Nested if else statements are used to check more than one condition that is already satisfied. We can say that nested if-else statements are an advanced version of the if-else statement in the C programming language.

### Syntax of the nested if-else statement:

``````//check if the first condition holds
if (condition 1) {
//if the second condition holds
if (condition 2) {
do something
}
//if the second condition does not hold
else {
do something else
}
}
// if the first condition does not hold
else{
//if the third condition holds
if (condition 3) {
do something
}
//if the third condition does not hold
else {
do something else
}
}
``````

Let's understand more about nested if else statement through the following flowchart: ## Some Examples of Nested If-else Statements

Example 1:

``````// program to check the number are equal to each other or not in C
#include <stdio.h>
int main() {
// variables so we can store the values
int a, b, c;
// for taking input from the user
printf("Enter three numbers\n");
scanf("%d %d %d", &a, &b, &c);

//if else condition to check whether the first two numbers are equal
if (a == b) {
//nested if else condition to check if c is equal to a and b
if (a == c) {
//all are equal
printf("Yes all three number are equal to each other");
} else {
//all are not equal
printf("No all three number are not equal to each other");
}
} else {
//if the first two numbers are not equal, so they are not equal
printf("No");
}
return 0;
}
``````

Output 1:

``````Enter three numbers
2
2
2
Yes all three numbers are equal to each other
``````

Output 2:

``````Enter three numbers
2
2
3
No all three number are not equal to each other
``````

Output 3:

``````Enter three numbers
1
2
3
No
``````

Example 2:

``````//program to check the grade of a student
#include <stdio.h>
int main() {

// variable to store the marks of the student
int marks;
//take input from the user
printf("enter the marks to check the grade\n");
scanf("%d", &marks);

//if else condition to check whether the student is pass
if (marks >= 35) {

//nested if else condition to determine the grade of the student
if (marks >= 90) {
printf("You got a A");
} else if (marks >= 80) {
printf("You got a B");
} else if (marks >= 70) {
printf("You got a C");
} else if (marks >= 60) {
printf("You got a D");
} else {
printf("You got a E");
}
} else {
//the student didn't pass
printf("You got a F");
}
return 0;
}
``````

Output 1:

``````Enter the marks of the student to check the grade
99
You got a A
``````

Output 2:

``````Enter the marks of the student to check the grade
80
You got a B
``````

Output 3:

``````Enter the marks of the student to check the grade
55
You got a E
``````