Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

C if-else ladder

if-else ladder:

  • An if-else ladder is a series of if and else if statements that are executed sequentially until a condition is true.
  • It allows you to handle multiple conditions in a step-by-step manner.

Example:

C
#include <stdio.h>

int main() {
    int num = 2;

    if (num == 0) {
        printf("Number is zero.\n");
    } else if (num == 1) {
        printf("Number is one.\n");
    } else if (num == 2) {
        printf("Number is two.\n");
    } else {
        printf("Number is greater than two.\n");
    }

    return 0;
}

Output:

C
Number is two.

In the example above, the program checks the value of num sequentially. When num is 2, the condition is satisfied, and the corresponding message is printed. The remaining else if and else blocks are skipped once a condition is true.