if-else Statement

The if-else statement is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true

Syntax

Here is the general syntax of if-else statement


if (condition) {
    // Code to execute if condition is true
} else {
    // Code to execute if condition is false
}               

Example 1: Check if Given Number is positive or negative


#include <stdio.h>

int main() {
    int number = -10;
    if (number > 0) {
        printf("The number is positive.\n");
    } else {
        printf("The number is not positive.\n");
    }
    return 0;
}

Output

The number is not positive.

Example 2: Find the maximum of two numbers.


#include <stdio.h>
    int main() {
        int num1, num2;
        printf("Enter two numbers: ");
        scanf("%d %d", &num1, &num2);
        if (num1 > num2) {
            printf("The maximum is %d.\n", num1);
        } else {
            printf("The maximum is %d.\n", num2);
        }
        return 0;
    }

Output

Enter two numbers: 5 8 The maximum is 8.

Example 1: Determine if a year is a leap year.


#include <stdio.h>
    int main() {
        int year;
        printf("Enter a year: ");
        scanf("%d", &year);
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
            printf("%d is a leap year.\n", year);
        } else {
            printf("%d is not a leap year.\n", year);
        }
        return 0;
    }

Output

Enter a year: 2024 2024 is a leap year.

Nested if-else

A nested if-else statement is used when we need to check multiple conditions. The inner if statement is used to check a condition that depends on the result of the outer if statement.

Example 1: Check Divisibility by 2 and 3.


#include <stdio.h>
    int main() {
        int num;
        printf("Enter a number: ");
        scanf("%d", &num);
        if (num % 2 == 0) { // Outer if
            if (num % 3 == 0) { // Inner if
                printf("The number is divisible by both 2 and 3.\n");
            } else {
                printf("The number is divisible by 2 but not by 3.\n");
            }
        } else {
            printf("The number is not divisible by 2.\n");
        }
        return 0;
    }

Output

Enter a number: 6 The number is divisible by both 2 and 3. Enter a number: 4 The number is divisible by 2 but not by 3.

Example 1: Check Eligibility for a Driving License.


#include <stdio.h>
int main() {
    int age;
    char vision;
    printf("Enter your age: ");
    scanf("%d", &age);
    if (age >= 18) {
        printf("Do you have good vision? (y/n): ");
        scanf(" %c", &vision);

        if (vision == 'y' || vision == 'Y') {
            printf("You are eligible for a driving license.\n");
        } else {
            printf("You are not eligible due to poor vision.\n");
        }
    } else {
        printf("You are not eligible due to age.\n");
    }
    return 0;
    }

Output

Enter your age: 20 Do you have good vision? (y/n): y You are eligible for a driving license.

Key points

  1. Execution Flow:
    • The inner if executes only when the outer if condition is true.
    • Multiple levels of nesting are possible but should be avoided for readability.

if-else-if Ladder

The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.

Syntax


if(condition1){  
    //code to be executed if condition1 is true  
    }else if(condition2){  
    //code to be executed if condition2 is true  
    }  
    else if(condition3){  
    //code to be executed if condition3 is true  
    }  
    ...  
    else{  
    //code to be executed if all the conditions are false  
    }  

Example 1: Assign grades based on marks.


#include <stdio.h>
int main() {
    int marks;
    printf("Enter your marks: ");
    scanf("%d", &marks);
    if (marks >= 90) {
        printf("Grade: A\n");
    } else if (marks >= 75) {
        printf("Grade: B\n");
    } else if (marks >= 50) {
        printf("Grade: C\n");
    } else if (marks >= 35) {
        printf("Grade: D\n");
    } else {
        printf("Grade: F (Fail)\n");
    }
    return 0;
}

Output

Enter your marks: 82 Grade: B

Example 2: Display the name of the day based on a number input.


#include <stdio.h>
int main() {
    int day;
    printf("Enter a number (1-7): ");
    scanf("%d", &day);
    if (day == 1) {
        printf("Monday\n");
    } else if (day == 2) {
        printf("Tuesday\n");
    } else if (day == 3) {
        printf("Wednesday\n");
    } else if (day == 4) {
        printf("Thursday\n");
    } else if (day == 5) {
        printf("Friday\n");
    } else if (day == 6) {
        printf("Saturday\n");
    } else if (day == 7) {
        printf("Sunday\n");
    } else {
        printf("Invalid input! Please enter a number between 1 and 7.n");
    }
    return 0;
}

Output

Enter a number (1-7): 3 Wednesday

Explanation: