C

Decision-Making in C: if, if-else, and switch

Decision-making structures require the programmer to specify one or more conditions to be evaluated or tested by the program. In C, these are known as Selection Statements. They allow the program to skip or execute specific blocks of code based on whether a condition is true or false.

1. The if Statement

The if statement is the simplest form of decision-making. It evaluates a condition inside parentheses.

  • Theory: If the condition evaluates to a non-zero value (True), the block of code inside the braces {} is executed. If the condition is zero (False), the block is skipped.

  • Logical Flow: It is a "one-way" branch.

Syntax:

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

2. The if-else Statement

The if-else statement provides an alternative path for the program.

  • Theory: When the if condition is false, the program doesn't just move on; it executes the code block provided in the else section. This creates a "two-way" branch.

  • Nested if-else: You can place an if inside another if to check for multiple layers of conditions.

  • Else-if Ladder: Used when you have multiple conditions to check in sequence. The program checks them one by one until it finds a true condition.

Syntax:

C
if (condition1) {
    // executes if condition1 is true
} else if (condition2) {
    // executes if condition2 is true
} else {
    // executes if neither are true
}

3. The switch Statement

The switch statement is a multi-way branch statement that provides an easy way to dispatch execution to different parts of code based on the value of an expression.

  • Theory: The switch expression is evaluated once and compared against the values of each case. It is often more readable than a long else-if ladder when comparing a single variable against several constant values.

  • The break Keyword: Crucial in switch. Without a break, the program will "fall through" and execute all subsequent cases regardless of whether they match.

  • The default Case: Acts like the else in an if-else ladder. It executes if none of the cases match.

Constraints:

  1. The switch expression must result in an integer or character. Floating-point numbers are not allowed.

  2. Case labels must be constants or literals (e.g., case 1: or case 'A':), not variables.

4. Comparison: if-else vs switch

Featureif-else Ladderswitch Statement
ExpressionCan evaluate relational and logical expressions (<, >, &&).Can only evaluate equality against constants.
Data TypesWorks with all types (int, float, etc.).Limited to int and char.
FlexibilityMore flexible for ranges (e.g., x > 10 && x < 20).Best for specific discrete values.
SpeedCan be slower if many conditions exist.Often faster due to "jump table" optimization.

5. Practical Code Example

#include <stdio.h>

int main() {
    int score;
    printf("Enter your score (0-100): ");
    scanf("%d", &score);

    // Using if-else ladder for range checking
    if (score >= 90) {
        printf("Performance: Excellent\n");
    } else if (score >= 70) {
        printf("Performance: Good\n");
    } else {
        printf("Performance: Needs Improvement\n");
    }

    // Using switch for menu-based selection
    int choice;
    printf("\nSelect Category:\n1. Student\n2. Teacher\nEnter choice: ");
    scanf("%d", &choice);

    switch (choice) {
        case 1:
            printf("You selected Student Portal.\n");
            break;
        case 2:
            printf("You selected Teacher Portal.\n");
            break;
        default:
            printf("Invalid choice.\n");
    }

    return 0;
}
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now