In this tutorial you will learn about the C Nested If Else Statement and its application with practical example.
C Nested If else statement
In C programming, when there is an if statement inside another if statement then it is known as a nested if-else statement. Nested if-else can also be simplified using C Switch Case Statement.
Table Of Contents−
Nested If else statement Syntax
Below is the general syntax of nested if-else statements in c programming:
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition1) { if(condition2) // statements } else { // statements } } else { if(condition3){ // statements } else { // statements } } |
Nested If else statement Example
Below is a simple example to demonstrate the use of a nested if-else statement in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <stdio.h> int main() { int a = 25; printf("W3Adda - C Nested If Statement\n"); if( a < 100 ) { if(a < 50){ printf("a is less than 50\n"); } if(a >= 50){ printf("a is greater than 50\m"); } } } |
When we run the above c program, will see the following output –
Output:-