×

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:

Difference between If Else and Nested If Else

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:

Difference between If Else and Nested If Else

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:

Difference between If Else and Nested If Else

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) {
      //A grade
printf("You got a A");
    } else if (marks >= 80) {
      //B grade
printf("You got a B");
    } else if (marks >= 70) {
      //C grade
printf("You got a C");
    } else if (marks >= 60) {
      //D grade
printf("You got a D");
    } else {
      //E grade
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

Related Topics

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

Unformatted input() and output() function in C

Unformatted Input and Output functions take the character, character array, and strings as input. We can read-only character data types with these functions. These functions are already defined in the...

5 minutes read.

Ferror() in c

Ferror():  In C, the ferror() function checks assuming there is a blunder in the given stream. The ferror() function is utilized to check for the document blunder on given stream. A return...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Star Program in C Language

Star Program in C Star patterns are a sequence of * or any other character used to construct any pattern or any like-square geometric form, triangle, hollow square, pyramid, rhombus, etc.  Many programmers worldwide highly...

4 minutes read.

How to lock top row in Excel

How to lock top row in excel While working with an Excel spreadsheet, the user utilizes the rows and columns to enter the details under various headings. Sometimes while scrolling the...

4 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

2f in C language

Float data type in c: Double-precision floating-point numbers with up to 17 significant digits are stored in the FLOAT data type. FLOAT is equivalent to C's double data type and IEEE...

3 minutes read.

Bank management system in C

This is a mini project that is constructed completely using the C language. The bank management system is a beginner friendly project. To construct this user only needs to know...

12 minutes read.

Explain the Increment and Decrement Operators in C

In the C programming language, the increment operator (++) and the decrement operator (--) are used to increase or decreasing a variable’s value by 1, respectively. The increment operator is written...

10 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.