Nested If Statement in C
In this article, you will learn about the Nested If statements in C programming and the usage of them in your programs.
Nested If statement in C
Multiple If statement inside the same block is known as the Nested If statement. It is nothing but placing an if statement inside another if statement. This can be used where you need to check a condition inside a condition.
If-else statements will execute based on the result of the expression. i.e True or False. But the Nested If statement will check for further conditions when the result is True.
Syntax
if( boolean_expression 1)
{
// if condition is True then check the following
if(boolean_expression 2)
{
// if condition is True then execute the following
}
}
[maxbutton id=”1″ ]
#include <stdio.h> int main() { int x = 50; int y = 50; if (x == 50) { if (y == 50) { printf("the value of x and y are the same"); } } return 0; }
In the above example, it checks the first condition i.e ‘x == 50’ if this is true then it goes into the loop and checks the next if condition i.e ‘y == 50’ if it returns true then prints the statement inside the loop or if the result is false then it skips the statements and comes out of the loop.
Similar way you can also nest else if..else as you did for the if statements.