Control Flow: Conditional Statements (if, else, switch)

Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.


Control Flow: Conditional Statements (if-else if-else) in C

Conditional statements are fundamental building blocks in programming that allow your code to execute different blocks of code based on whether a certain condition is true or false. The if-else if-else construct is used when you need to check multiple conditions sequentially.

Understanding the if-else if-else Construct

The if-else if-else statement allows you to chain together multiple conditions. The program evaluates each condition in order, and executes the block of code associated with the first condition that evaluates to true. If none of the conditions are true, the code within the final else block is executed (if it's present).

Syntax

 if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition1 is false AND condition2 is true
} else if (condition3) {
    // Code to execute if condition1 and condition2 are false AND condition3 is true
} else {
    // Code to execute if none of the above conditions are true
} 

Explanation

  • if (condition1): The first condition to be evaluated. If condition1 evaluates to true (non-zero), the code block immediately following the if statement is executed.
  • else if (condition2): If condition1 is false (zero), the program checks condition2. If condition2 is true, its corresponding code block is executed. You can have multiple else if blocks to check multiple conditions.
  • else: This is the optional "catch-all" block. If none of the preceding if or else if conditions are true, the code within the else block is executed. If you omit the else block and none of the conditions are true, the program simply proceeds to the next statement after the if-else if chain.

Example in C

 #include <stdio.h>

int main() {
    int age = 25;

    if (age < 13) {
        printf("You are a child.\n");
    } else if (age < 20) {
        printf("You are a teenager.\n");
    } else if (age < 65) {
        printf("You are an adult.\n");
    } else {
        printf("You are a senior citizen.\n");
    }

    return 0;
} 

Explanation of the Example

In this example, the program checks the value of the age variable against several ranges:

  1. If age is less than 13, it prints "You are a child."
  2. If age is NOT less than 13, it checks if it's less than 20. If so, it prints "You are a teenager."
  3. If age is NOT less than 13 and NOT less than 20, it checks if it's less than 65. If so, it prints "You are an adult."
  4. If age is NOT less than 13, NOT less than 20, and NOT less than 65, it prints "You are a senior citizen."

Since age is 25, the condition age < 65 is true, so the output will be "You are an adult."

Key Considerations

  • Order Matters: The order of your conditions is crucial. The first condition that evaluates to true will have its code block executed, and the rest will be skipped. For example, if the first condition was age < 65, then the code block for adults would always be executed for valid ages, and the other conditions would never be checked.
  • Efficiency: While if-else if-else is powerful, using too many else if blocks can sometimes make your code less readable and potentially less efficient. Consider alternative control flow structures like switch statements when dealing with a large number of discrete, mutually exclusive conditions (especially when comparing a single variable against many possible values).
  • Boolean Expressions: The conditions within the if and else if statements must be boolean expressions (expressions that evaluate to either true or false). In C, 0 represents false, and any non-zero value represents true.