if...else ladder: chaining two or more if...else statements

suggest change

While 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");
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents