While the if ()... else
statement allows to define only one (default) behaviour which occurs when the condition within the if ()
is not met, chaining two or more if () ... else
statements allow to define a couple more behaviours before going to the last else
branch acting as a "default", if any.
Example:
int a = ... /* initialise to some value. */
if (a >= 1)
{
printf("a is greater than or equals 1.\n");
}
else if (a == 0) //we already know that a is smaller than 1
{
printf("a equals 0.\n");
}
else /* a is smaller than 1 and not equals 0, hence: */
{
printf("a is negative.\n");
}