Nested if-else in C
A nested if in C is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Yes, C allow us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.
Syntax of Nested if-else
if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}
Flowchart of Nested if-else
Example of Nested if-else
C
// C program to illustrate nested-if statement #include <stdio.h> int main() { int i = 10; if (i == 10) { // First if statement if (i < 15) printf ( "i is smaller than 15\n" ); // Nested - if statement // Will only be executed if statement above // is true if (i < 12) printf ( "i is smaller than 12 too\n" ); else printf ( "i is greater than 15" ); } return 0; } |
Output
i is smaller than 15 i is smaller than 12 too
Decision Making in C (if , if..else, Nested if, if-else-if )
The conditional statements (also known as decision control structures) such as if, if else, switch, etc. are used for decision-making purposes in C programs.
They are also known as Decision-Making Statements and are used to evaluate one or more conditions and make the decision whether to execute a set of statements or not. These decision-making statements in programming languages decide the direction of the flow of program execution.