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

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


C Programming: Control Flow - Conditional Statements (else)

Understanding Control Flow

In programming, control flow determines the order in which statements are executed. Conditional statements, like the if and else statements, allow your program to make decisions and execute different blocks of code based on whether a condition is true or false. This is crucial for creating dynamic and responsive programs.

The else Statement

The else statement is used in conjunction with the if statement. It provides a block of code to be executed when the condition in the if statement is false. Think of it as providing an "alternative" path for your program to follow.

Syntax:

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

Explanation:

  • The condition inside the parentheses () of the if statement is evaluated.
  • If the condition is true (non-zero), the code block enclosed in the curly braces {} immediately following the if statement is executed.
  • If the condition is false (zero), the code block enclosed in the curly braces {} immediately following the else statement is executed.

Example in C:

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

Explanation of the Example:

  1. The program prompts the user to enter their age.
  2. The scanf function reads the age entered by the user and stores it in the age variable.
  3. The if statement checks if age is greater than or equal to 18.
  4. If age is 18 or greater (the condition is true), the message "You are eligible to vote." is printed.
  5. If age is less than 18 (the condition is false), the message "You are not eligible to vote." is printed.

Another Example: Checking if a number is even or odd

 #include <stdio.h>

    int main() {
        int number;

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

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

        return 0;
    } 

Explanation of the Example:

  1. The program prompts the user to enter an integer.
  2. The scanf function reads the number entered by the user and stores it in the number variable.
  3. The if statement checks if the remainder of number divided by 2 is equal to 0 (number % 2 == 0). This determines if the number is even.
  4. If the remainder is 0 (the condition is true), the message that the number is even is printed.
  5. If the remainder is not 0 (the condition is false), the message that the number is odd is printed.

Key Takeaways:

  • The else statement provides a way to execute code when the if condition is false.
  • It improves the flexibility and decision-making capabilities of your programs.
  • Using if and else allows your program to respond differently to various inputs and situations.