C - Nested if Control Statements

<<Previous

Next >>



Nested if

In C programming, we can use one if statement inside the another if statement. This is known as nested if statement.

Syntax

if(conditional-expression-1)
{
	/* statements can be executed only the conditional_expression_1 is true */
	statement1;
	if(conditional-expression-2)
	{
		/* statements can be executed only the conditional_expression_2 is true */
		statement2;
	}
}

Example C Program for Nested if statement

#include <stdio.h>

int main()
{
	int A=10;
	int B=15;
	if (A < B){
	   printf("Value of A is less than Value of B \n");
	   if (A > 5){
		   printf("This is inner if statement\n");
		   A = A - 5;
	     }
	}
	printf("Value of A is: %d ",A);
	return(0);
}

Output

Value of A is less than Value of B
This is inner if statement
Value of A is: 5

<< Previous

Next >>