C If...Else


In C programming, decision-making is essential because programs often need to choose between different actions based on certain conditions. The if...else statement is one of the most common ways to implement decisions. It allows the program to execute specific blocks of code only if a condition is true, and optionally execute other code if the condition is false.

Understanding if...else is critical because it forms the foundation for more complex decision-making structures, including nested conditions, loops, and switch statements.

What is an If Statement?

The if statement evaluates a condition and executes a block of code if the condition is true. If the condition is false, the code inside the if block is skipped.

Syntax:

if (condition) {
    // code to execute if condition is true
}

Example:

#include <stdio.h>

int main() {
    int age = 18;

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

    return 0;
}

Output:

You are eligible to vote.

Here, the condition age >= 18 is true, so the message is printed.

Using Else

The else statement defines a block of code to execute when the if condition is false. Syntax:

if (condition) {
    // code if true
} else {
    // code if false
}

Example:

#include <stdio.h>

int main() {
    int age = 16;

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

    return 0;
}

Output:

You are not eligible to vote.

If...Else If...Else Ladder

When you have multiple conditions to check, you can use else if:

if (condition1) {
    // code if condition1 is true
} else if (condition2) {
    // code if condition2 is true
} else {
    // code if none are true
}

Example:

#include <stdio.h>

int main() {
    int marks = 85;

    if (marks >= 90) {
        printf("Grade: A+\n");
    } else if (marks >= 75) {
        printf("Grade: A\n");
    } else if (marks >= 60) {
        printf("Grade: B\n");
    } else {
        printf("Grade: C or below\n");
    }

    return 0;
}

Output:

Grade: A

This ladder allows the program to evaluate conditions sequentially and execute the first true block.

Nested If Statements

You can also use an if statement inside another if to handle more complex logic:

#include <stdio.h>

int main() {
    int age = 20;
    int hasID = 1; // 1 = true, 0 = false

    if (age >= 18) {
        if (hasID) {
            printf("Entry allowed.\n");
        } else {
            printf("Entry denied: ID required.\n");
        }
    } else {
        printf("Entry denied: Age restriction.\n");
    }

    return 0;
}

Output:

Entry allowed.

Nested if statements help manage multiple levels of conditions but should be used carefully to avoid complexity.

Using Logical Operators with If

if statements often use relational and logical operators to combine conditions:

  • AND (&&): True if both conditions are true

  • OR (||): True if at least one condition is true

  • NOT (!): Reverses the Boolean value

Example:

#include <stdio.h>

int main() {
    int age = 22;
    int hasTicket = 1;

    if (age >= 18 && hasTicket) {
        printf("You can enter the event.\n");
    } else {
        printf("Entry denied.\n");
    }

    return 0;
}

Output:

You can enter the event.

Common Mistakes

  1. Using = instead of ==:
    = assigns a value, while == compares values. Example:

    if (age = 18) { // Incorrect
    
  2. Missing braces {} for multiple statements:
    Without braces, only the first statement after if or else is executed.

  3. Overlapping conditions:
    Ensure else if conditions are mutually exclusive to avoid logical errors.

Practical Example

#include <stdio.h>

int main() {
    int temperature = 30;

    if (temperature > 35) {
        printf("It's a hot day.\n");
    } else if (temperature > 25) {
        printf("It's a warm day.\n");
    } else if (temperature > 15) {
        printf("It's a mild day.\n");
    } else {
        printf("It's a cold day.\n");
    }

    return 0;
}

Output:

It's a warm day.

This program demonstrates how to use if...else if...else to categorize ranges of values.

Best Practices

  1. Keep conditions simple:
    Complex expressions can be broken into smaller Boolean variables for clarity.

  2. Use braces {} even for single statements:
    Improves readability and reduces mistakes during future edits.

  3. Order conditions logically:
    Check the most likely or most important conditions first.

  4. Comment complex logic:
    Especially useful in nested or multiple condition statements.

Summary of the Tutorial

The if...else statement in C is a fundamental tool for decision-making. It allows a program to execute specific code based on conditions, making programs dynamic and responsive. Understanding how to use simple if, if...else, if...else if...else, and nested if statements prepares you for more advanced control structures such as switch statements, loops, and logical operations. Proper use of if...else ensures readable, maintainable, and error-free programs.


Practice Questions

  1. Write a program to check if a number entered by the user is positive, negative, or zero.

  2. Create a program that takes the marks of a student and prints their grade using if...else if...else.

  3. Write a program to find the largest of three numbers entered by the user.

  4. Create a program that checks if a given year is a leap year or not.

  5. Write a program to determine if a number is even or odd.

  6. Develop a program to check if a person is eligible to vote (age 18 or above).

  7. Write a program to categorize a number as small (<10), medium (10–50), or large (>50).

  8. Create a program to check if a character entered is a vowel or consonant.

  9. Write a program to calculate discount: if purchase > 1000, apply 10% discount; else, no discount.

  10. Create a program to check if a person qualifies for a senior citizen discount (age 60 or above) and print an appropriate message.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top