×

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

Prime Number code in C

Prime Number Programs in C language In this tutorial, we will learn how to check whether the given number is a prime number or not, and how to print all the...

4 minutes read.

Free() Function in C

Free() function is a built-in function which is define in the stdlib.h header file. If we want to use this function in our program, we must include the stdlib.h header...

4 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it...

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

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

fgets() function in C

fgets() function is present in standard input and output library i.e, stdio.h library. It is a built-in function or pre-define function. It is used to read the specified stream from...

4 minutes read.

Prototype in C

A prototype is nothing but a model, a model of initial creation of an intended product. Similarly, in the C programming language, all functions have a prototype. In general, all...

4 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

How to use sine() function in C

What is Function? The function is a set of statements. It takes input and performs some computation to produce output. The process is a set of codes only achieved when it is...

3 minutes read.

10 Best IDEs for C or C++ Developers in 2024

Nobody can deny the fact that C and C++ were the first programming languages used by significant developers worldwide. Even now, newcomers who want to start programming are most frequently...

6 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

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

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.

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.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Character Set in C

Introduction Every language has some basic elements which are used to represent information. The English language includes alphabets which together shape a sentence, after which the sentences are used to shape...

3 minutes read.

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

C Function Argument and Return Values

Functions in the C programming language are declared to avoid repeatedly writing a performable block of code; it is the same in any programming language. When it comes to the...

3 minutes read.