C - If-else

In C, the if-else statement is a control structure used for conditional execution. It allows you to specify two blocks of code: one to be executed if a specified condition is true, and another to be executed if the condition is false. The if-else statement provides a way to create branching logic in your program, enabling it to take different actions based on the evaluation of a condition.

Here is the syntax of the if-else statement in C:



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

  • The 'condition' is an expression or a test that evaluates to either true (non-zero) or false (zero).
  • If the 'condition' is true, the code block inside the first set of curly braces {} is executed.
  • If the 'condition' is false, the code block inside the second set of curly braces {} (after else) is executed.

Let's illustrate the if-else statement with an example:


#include <stdio.h>

int main() {
    int age;

    printf("Enter your age: ");
    scanf("%d", &age);

    if (age >= 18) {
        printf("You are eligible to vote.\n");
    } else {
        printf("You are not eligible to vote.\n");
    }

    return 0;
}

In this example:

  1. The program prompts the user to enter their age and stores it in the variable age.
  2. The if-else statement checks if age is greater than or equal to 18 (the voting age). If the condition is true, it prints "You are eligible to vote." If the condition is false, it prints "You are not eligible to vote."
  3. Depending on the value entered by the user, one of the two messages will be displayed. This demonstrates how the if-else statement allows the program to make decisions and execute different code blocks based on the condition's evaluation.

Here are a few more examples of if-else statements in C where the input is already entered, and the program displays output messages based on the conditions:

Example 1: Determining if a Number is Even or Odd

#include <stdio.h>

int main() {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    if (number % 2 == 0) {
        printf("%d is an even number.\n", number);
    } else {
        printf("%d is an odd number.\n", number);
    }

    return 0;
}

In this example, the program asks the user to enter an integer, and then it checks whether the number is even or odd using the modulus operator %. It displays a message accordingly.

In this example, if my input is 7, which is an odd number, so the program displays "7 is an odd number."


Enter an integer: 7
7 is an odd number.
Example 2: Determining 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;
}

In this example, the program prompts the user to enter a year, and then it checks whether the year is a leap year or not based on the leap year rules. It displays an appropriate message.

In this example, if my input is 2024, which is a leap year according to the leap year rules, so the program displays "2024 is a leap year."


Enter a year: 2024
2024 is a leap year.
Example 3: Grading Student Scores

#include <stdio.h>

int main() {
    int score;

    printf("Enter the student's score: ");
    scanf("%d", &score);

    if (score >= 70) {
        printf("Grade: A\n");
    }else {
        printf("Grade: F (Fail)\n");
    }

    return 0;
}

In this example, the program takes a student's score as input and assigns a letter grade based on the score range using multiple if-else statements.

In this example, the input score is 85, which falls within the range for a "A" grade, so the program displays "Grade: A" based on the grading criteria.


Enter the student's score: 85
Grade: A

These examples demonstrate how if-else statements can be used to make decisions and display relevant output messages based on the input values or conditions.