×

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside another condition.

Purpose of using Nested if else Statement:

We can check multiple conditions and return different values, all in a single form, depending on the results of those checks.

Example Statement:

All children under or equal the age of 12 are entitled to free tuition. However, the organization does not offer free classes to all children. Therefore, we use another if statement (also known as a nested if statement in C) to see their family's income. If annual income of their family is below or equal to 100000 (1 lakh), they will get free tuition.

Syntax:

if (req 1) //if the condition is true , this will execute
{
 
   if (req 2) //if the condition is true ,it will execute and print 
   {
    printf("___");
   }


   else //if req 2 condition is not true then only else part will execute and print
   {
    printf("___");
   }


else //if req 1 condition is false then this else part will execute.
{
 printf("____");
}

Example of if else statement:

This example uses only if-else conditions. If students have a score of 75% or higher here, they are eligible for a scholarship, otherwise they are not.

Code:

#include <stdio.h>
int main()
{
   int score;
   printf("Enter your score out of 100:");
   scanf("%d",&score);
   if(score >=75) //if the condition is true , this will execute
   {
	printf("You are eligible for scholarship");
   }
   else //if the 'if' condition is false then this else part will execute.
   {
	printf("You are not eligible for scholarship");
   }
   return 0;
}

Output:

Input: 96
Output:
You are eligible for scholarship

Examples of Nested if else Statement:

Example 1:

All children under or equal the age of 12 are entitled to free lessons. However, this organization does not offer free classes to all children. If their family's annual income is less than or equal to 1 lakh, you will receive a free tuition.

Code:

#include <stdio.h>
int main()
{
   int age, income; //considering two variables
   printf("Enter your age: ");
   scanf("%d",&age);


   if(age <=12) //if the condition is true , this will execute
   { 
     printf("\nEnter your family income: ");
     scanf("%d",&income);
     
    	if(income<=100000) // it executes if their annual income is below or more than 1 lakh
    	printf("You are eligible for free tuition");
    	
 	else //more than annual 1lakh income is not eligible for scholarship
	printf("You are not eligible for free tuition");
   }
   
   else //if the 'if' condition is false then this else part will execute.
   {
	printf("You are not eligible for free tuition");
	
   }
   return 0;
}

Output 1:

Input 1: 
25
Output:
You are not eligible for free tuition

Output 2:

Input 2:
11
96000
Output :
You are eligible for free tuition

Output 3:

Input 3:
9
1200000
Output :
You are not eligible for free tuition

Example 2:

All students under or equal the age of 25 are eligible to enrol in a prestigious university. However, the university does not accept all students. If a student has a percentage of the latest board exams of 75 or above, you have a chance to enrol.

Code:

#include <stdio.h>
int main()
{
   int age,score; //considering two variables
   printf("Enter your age: ");
   scanf("%d",&age);


   if(age <= 25) //if the condition is true , this will execute
   { 
     printf("\nEnter your percentage of last board exam : ");
     scanf("%d",&score);
     
    	if(score<=100 && score>=75) // it executes if a student has more than or equal to 75% score
    	printf("You are eligible to get admission in our College");
    	
 	else //less than 75% score is not qualified to get admission
	printf("You are not qualified to get admission in our College");
   }
   
   else //if the 'if' condition is false then this else part will execute.
   {
	printf("You are not qualified to get admission in our College");
	
   }
   return 0;
}

Output 1:

Input 1:
22
76
Output :
You are eligible to get admission in our College

Output 2:

Input 2:
31
Output:
You are not qualified to get admission in our College

Output 3:

Input 3:
20
60
Output:
You are not qualified to get admission in our College

Related Topics

Nested Structure in C

In C language, we can create nested Structure (Structure within Structure). There are two ways to define a nested structure. By separate structure By Embedded structure   Separate structure In a separate...

1 minute read.

Continue in C

C language: C language is a procedure oriented programming language. We can say that it is a platform dependent language. C language is introduced by Dennis Ritchie in the year 1970. We...

2 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

How to Merge Array in C?

Introduction Merging arrays is a common task for many developers but might be difficult for beginners of C programming. Fortunately, our guide will show you how to quickly merge arrays in...

6 minutes read.

How to use floor() function in C

Introduction: The floor() used as a function in the C programming language. This function uses to return the largest or most significant integer value. The integer value is smaller than...

3 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

Difference between Array and List in C

C# Array vs List is where the abstraction and implementation of human computing meet.  Arrays are incredibly related to the hardware concept of contiguous and contiguous memory, where each part is...

3 minutes read.

C Pointers

Pointers in C A pointer is a variable, which contains the address of another variable, i.e., it’s a variable which has the address of another variable as its value. The utilization...

4 minutes read.

Enumeration (Enum) in C

Enumeration (Enum) in C: In the C programming language, the enum is referred to as an enumerated type. Enum is a user defined data type consisting of integer values and...

3 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Random Access - Lseek in C

Introduction lseek is a system call that is used to alter the location of the read/write pointer of a file descriptor. The lseek system call basically moves the current read/write location...

4 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 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.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.