Home »
Syntax References
if else if (ladder if) statement syntax in C/C++ language
Syntax of if else if (ladder if) statement in C/C++ programming language, this article contains syntax, examples and explanation about the if else if (ladder if) statement in C language.
Here, is the syntax of if else if (ladder if) statement in C or C++ programming language:
if(test_condition1)
{
//statement(s)/block1;
}
else if(test_condition2)
{
//statement(s)/block2;
}
.
.
.
else
{
//statement(s)/else block;
}
This is a form of if else statement, where we can check multiple test conditions it is also known as multiple if else statement, it checks test conditions one by one.
If test_condition1 is true then block1 will be executed, if it is false then test_condition2 will be checked, if test_condition2 is true then block2 will be executed, if it is false then next condition will be checked.
If any of the test conditions is not true then else block will be executed.
Consider the given example:
#include <stdio.h>
int main()
{
int a=-20;
if(a>0)
printf("%d is a postive integer\n",a);
else if(a<0)
printf("%d is a negative integer\n",a);
else
printf("Its ZERO\n");
return 0;
}
Output
-20 is a negative integer
In this program, the value of a is -20, so first condition (a>0) is false and then second condition (a<0) is true. Thus, "-20 is a negative integer" is printed.