In this tutorial, you will be familiarize with one type of conditional statement that is nested if-else statement available for C programming language, along with its syntax and example of nested if-else .
Conditional statements :
Conditional statements are those code which allows you to control the program’s flow of the execution centred on a condition.
It means that the statements in the program are not executed in sequence.
Rather, one group of statements is executed, depending on how the condition is evaluated.
nested if-else Statement:
Syntax :
if (condition1) if (condition2) if (condition3) statements; else statement;
nested if else statements means you can use one if or else if statement inside another if or else if statement(s).
Example of nested if-else statement
1. Code to check whether the number entered by user is an even number or an odd number:
// Header file which contains functions for standard input and output manipulative operation. #include <stdio.h> int main() // Compiling of code start from main function { int x ; // Declaration of local variable // To display on console screen printf(“Enter a value…… /n ”); // To get input from user scanf(“%d”,&x); // Use of if-else statement // to check whether the entered number is greater than 0 or not if(n>0) { // to check whether the number entered by user is an even or an odd number. if ( x % 2 == 0) { printf(“Entered number is an even number.”); } else { printf(“Entered number is an odd number.”); } } return 0; }
Here I include both the possible aspects for this code such as entered number is an even number like 100 or it may be an odd number like 145.
Output of both the cases shown below :
case 1. if entered value is 100 then
OUTPUT:
Enter a value……100 Entered number is an even number.
But,
case 2. if entered value is 145 then
OUTPUT:
Enter a value……145 Entered number is an odd number.
1. Code to check whether the entered number by user is greater than 100 or not :
// Header file which contains functions for standard input and output manipulative operation. #include <stdio.h> int main() // Compiling of code start from main function { // Declaration of local variable int x ; // To display on console screen printf(“Enter a value…… /n ”); // To get input from user scanf(“%d”,&x); // Use of if-else statement // to check whether the entered number is greater than 0 or not if(n>0) { if ( x > 100 ) { printf(“Entered number is greater than 100.”); } else if ( x = 100 ) { printf(“Entered number is equal to 100.”); } else { printf(“Entered number is lesser than 100.”); } } return 0; }
Here I include three possible aspects for this code such as entered number is greater than 100 like 150 or it may be lesser than 100 like 26 and the entered number may be equal to 100 also.
Output of both the cases shown below :
case 1. if entered value is 150 then
OUTPUT:
Enter a value……150 Entered number is greater than 100.
But,
case 2. if entered value is 26 then
OUTPUT:
Enter a value……26 Entered number is lesser than 100.
Also,
case 3. if entered value is 100 then
OUTPUT:
Enter a value……100 Entered number is equal to 100.
This is all nested if-else statement available for C programming language, along with its syntax and example of nested if-else .
If you have any query please comment below.