×

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

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Exit control loop in C

To examine the termination condition for an exit, we usually use an exit control loop in C. If the evaluation for termination condition results in true, then the control would...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Big O Notation in C

Big O Notation in C: In the C programming language, we have so many algorithms and solutions to the problems that exist, which have different aspects and purposes to an...

3 minutes read.

GCD of Two Numbers in C

The GCD means the greatest common divisor of two or more integers, it is also called as hcf. The gcd returns the greatest integer of the given two integers that...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

If else Programs in C Programming

If else Programs in C programming The if-else statement in C is based on some particular conditions to perform the operations. If and only if the given condition is valid, the operations listed in...

4 minutes read.

Purpose of a Function Prototype in C

A function prototype in C is a declaration of a function that specifies the function's name, return type and parameters. It has the following syntax: return_type function_name(parameter_list); For example, the prototype for...

4 minutes read.

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.