If else if control statements in C and C++
if else if
If else if statements can be used to execute an alternate course of action for each of the several conditions. If the condition is true for the first if statement, then the first block will be executed. If the condition is true for the second if statement, then the second block will be executed, and so on. Suppose if none of the conditions are true, then the default else statement will be executed.
Syntax: if(conditional_expression){ statement1; } else if(conditional_expression){ statement2; } else if(conditional_expression){ statement3; } else if(conditional_expression){ statement4; } else{ statement5; }
Example C Program for Nested if statement
#include <stdio.h> #include <conio.h> int main() { int A=20; int B=15; if (A < B){ printf("The A value is less than B \n"); } else if (A > 100){ printf("The A value is greater than 100 \n"); } else if (A == B){ printf("The A value is equal to B \n"); } else { printf("The A value is neither less than B nor greater than 100, It is also not equal to B \n"); } printf("The A value is: %d ",A); return(0) }
Output
The A value is neither less than B nor greater than 100, It is also not equal to B The A value is:20