Conditional statements are fundamental in programming as they allow you to execute different code blocks based on certain conditions. In C, the primary conditional statements are if, else if, else, and switch. This section will cover each of these in detail, providing examples and exercises to solidify your understanding.

  1. The if Statement

The if statement is used to execute a block of code if a specified condition is true.

Syntax

if (condition) {
    // code to be executed if condition is true
}

Example

#include <stdio.h>

int main() {
    int number = 10;

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

    return 0;
}

Explanation

  • The condition number > 0 is checked.
  • If the condition is true, the code inside the if block is executed, printing "The number is positive."

  1. The else Statement

The else statement is used to execute a block of code if the condition in the if statement is false.

Syntax

if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Example

#include <stdio.h>

int main() {
    int number = -5;

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

    return 0;
}

Explanation

  • The condition number > 0 is checked.
  • If the condition is false, the code inside the else block is executed, printing "The number is not positive."

  1. The else if Statement

The else if statement allows you to check multiple conditions.

Syntax

if (condition1) {
    // code to be executed if condition1 is true
} else if (condition2) {
    // code to be executed if condition2 is true
} else {
    // code to be executed if both conditions are false
}

Example

#include <stdio.h>

int main() {
    int number = 0;

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

Explanation

  • The conditions number > 0 and number < 0 are checked in sequence.
  • If neither condition is true, the code inside the else block is executed, printing "The number is zero."

  1. The switch Statement

The switch statement is used to execute one block of code among many options.

Syntax

switch (expression) {
    case constant1:
        // code to be executed if expression equals constant1
        break;
    case constant2:
        // code to be executed if expression equals constant2
        break;
    // you can have any number of case statements
    default:
        // code to be executed if expression doesn't match any case
}

Example

#include <stdio.h>

int main() {
    int day = 3;

    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        case 4:
            printf("Thursday\n");
            break;
        case 5:
            printf("Friday\n");
            break;
        case 6:
            printf("Saturday\n");
            break;
        case 7:
            printf("Sunday\n");
            break;
        default:
            printf("Invalid day\n");
    }

    return 0;
}

Explanation

  • The value of day is compared against each case.
  • When a match is found, the corresponding block of code is executed.
  • The break statement prevents the execution from falling through to the next case.

Practical Exercises

Exercise 1: Positive, Negative, or Zero

Write a program that reads an integer from the user and prints whether the number is positive, negative, or zero.

Solution

#include <stdio.h>

int main() {
    int number;

    printf("Enter an integer: ");
    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;
}

Exercise 2: Grade Evaluation

Write a program that reads a grade (A, B, C, D, F) from the user and prints the corresponding description (Excellent, Good, Average, Poor, Fail).

Solution

#include <stdio.h>

int main() {
    char grade;

    printf("Enter a grade (A, B, C, D, F): ");
    scanf(" %c", &grade);

    switch (grade) {
        case 'A':
            printf("Excellent\n");
            break;
        case 'B':
            printf("Good\n");
            break;
        case 'C':
            printf("Average\n");
            break;
        case 'D':
            printf("Poor\n");
            break;
        case 'F':
            printf("Fail\n");
            break;
        default:
            printf("Invalid grade\n");
    }

    return 0;
}

Common Mistakes and Tips

  • Forgetting the break statement in switch cases: This can lead to fall-through behavior, where multiple cases are executed.
  • Using = instead of == in conditions: = is the assignment operator, while == is the equality operator.
  • Not handling all possible cases: Always include a default case in switch statements to handle unexpected values.

Conclusion

In this section, you learned about conditional statements in C, including if, else if, else, and switch. These constructs allow you to control the flow of your program based on different conditions. Practice the exercises provided to reinforce your understanding, and remember to watch out for common mistakes. Next, we will explore loops in C, which will further enhance your ability to control program flow.

© Copyright 2024. All rights reserved