C If…Else Examples

Welcome to The Coding College! In this tutorial, we’ll focus on if...else statements in C programming through a series of practical examples. Understanding how to use if...else effectively is crucial for implementing decision-making in your programs.

What is if...else in C?

The if...else construct is used to execute one block of code if a condition is true and another block of code if the condition is false.

Basic Syntax

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

Example 1: Checking Even or Odd

#include <stdio.h>

int main() {
    int number;

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

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

    return 0;
}

Output:

Enter a number: 5  
5 is odd.

Example 2: Checking Age Eligibility

#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;
}

Output:

Enter your age: 16  
You are not eligible to vote.

Example 3: Largest of Two Numbers

#include <stdio.h>

int main() {
    int num1, num2;

    printf("Enter two numbers: ");
    scanf("%d %d", &num1, &num2);

    if (num1 > num2) {
        printf("%d is larger.\n", num1);
    } else {
        printf("%d is larger.\n", num2);
    }

    return 0;
}

Output:

Enter two numbers: 10 20  
20 is larger.

Example 4: Grading System

#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 {
        printf("Grade: F\n");
    }

    return 0;
}

Output:

Enter your marks: 82  
Grade: B

Example 5: Positive, Negative, or Zero

#include <stdio.h>

int main() {
    int number;

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

    if (number > 0) {
        printf("The number is positive.\n");
    } else if (number < 0) {
        printf("The number is negative.\n");
    } else {
        printf("The number is zero.\n");
    }

    return 0;
}

Output:

Enter a number: -7  
The number is negative.

Example 6: Login System

#include <stdio.h>
#include <string.h>

int main() {
    char username[20], password[20];

    printf("Enter username: ");
    scanf("%s", username);
    printf("Enter password: ");
    scanf("%s", password);

    if (strcmp(username, "admin") == 0 && strcmp(password, "1234") == 0) {
        printf("Login successful!\n");
    } else {
        printf("Invalid credentials.\n");
    }

    return 0;
}

Output:

Enter username: admin  
Enter password: 1234  
Login successful!

Example 7: Leap Year Checker

#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.

Key Points to Remember

  1. Structure: Always ensure proper use of braces {} to avoid errors, especially when nesting conditions.
  2. Conditions Order: Use logical and practical order when checking multiple conditions.
  3. Alternative Logic: Use the ternary operator for simple if-else conditions.

Common Mistakes

1. Missing Braces

if (x > y)
    printf("X is greater.");
    printf("This line is outside if.\n");

Solution: Always use braces for clarity:

if (x > y) {
    printf("X is greater.\n");
}

2. Overlapping Conditions

if (x > 10) {
    // Executes this block
} else if (x > 10) {
    // This condition is redundant
}

Solution: Avoid redundant or overlapping conditions.

Conclusion

The if...else statement is a cornerstone of decision-making in C programming. By mastering its use, you can build logical and robust programs. For more in-depth tutorials and examples, explore The Coding College—your ultimate guide to programming success!

Leave a Comment