if...else ladder: chaining two or more if...else statements
suggest changeWhile the if ()... else statement allows only one (default) behaviour when the condition within the if () is not met, chaining two or more if () ... else statements allows a couple more behaviours before the last else branch, which acts as a “default”, if any.
Example:
int a = ... /* initialise to some value. */
if (a >= 1)
{
printf("a is greater than or equal to 1.\n");
}
else if (a == 0) /* a is already known to be less than 1 */
{
printf("a equals 0.\n");
}
else /* a is less than 1 and not equal to 0, hence: */
{
printf("a is negative.\n");
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents