Introduction to Selective Statements in C Programming

In programming, we often need to make decisions. For example, think about deciding whether you should go outside. If it’s raining, you might decide to stay indoors; if it’s sunny, you might go for a walk. In the same way, we use selective statements in C programming to make decisions in our code based on certain conditions.

Selective statements allow the program to choose which block of code to execute based on the value of some condition. This is like a fork in the road: the program will follow one path if the condition is true and a different path if it’s false.

Types of Selective Statements in C

In C programming, there are several types of selective statements, each allowing us to handle conditions in different ways. These are:

  • IF statement
  • ELSE statement
  • ELSE IF statement
  • NESTED IF statement
  • SWITCH statement

IF Statement

The IF statement is the simplest form of decision-making in C. It allows you to execute a block of code only if a specified condition is true. Think of it as a simple "Yes or No" question. If the answer is "Yes," do something; if "No," do nothing.

Syntax:

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

Example:

#include <stdio.h>

void main() {
    int age = 20;
    if (age >= 18) {
        printf("You are eligible to vote.");
    }
}

In this example, the program checks if the value of age is 18 or older. If the condition is true (which it is, since age is 20), the message "You are eligible to vote" is printed. If the condition were false (for example, if age were 16), the program would skip the block of code inside the IF statement.

Note: If you place a semicolon after the condition, like this if(condition);, it will check the condition but will not execute any code. The semicolon effectively ends the block.

ELSE Statement

The ELSE statement is used when you want to do something if the condition in the IF statement is not true. It’s like saying, "If it’s not true, do this other thing instead."

Syntax:

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

Example:

#include <stdio.h>

void main() {
    int marks = 45;
    if (marks >= 35) {
        printf("You passed the exam!");
    }
    else {
        printf("You failed the exam.");
    }
}

In this example, if marks are 35 or higher, the program prints "You passed the exam!" If marks are less than 35, it prints "You failed the exam." This helps the program handle both possible outcomes.

Note: The else statement is not mandatory after an if statement, but it's good practice to include it to handle all possible cases and ensure that the user is always informed of the result.

ELSE IF Statement

The ELSE IF statement allows you to check multiple conditions in sequence. It’s like asking a series of "Yes or No" questions, and the program will stop at the first "Yes" it finds and execute that block of code.

Syntax:

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

Example:

#include <stdio.h>

void main() {
    float percentage = 75;
    if (percentage >= 80) {
        printf("You got a Distinction!");
    }
    else if (percentage >= 60) {
        printf("You got a First Class!");
    }
    else if (percentage >= 50) {
        printf("You got a Second Class.");
    }
    else {
        printf("You need to improve.");
    }
}

In this example, the program checks the value of percentage. If it’s 80 or higher, it prints "You got a Distinction!" If it’s between 60 and 79.9, it prints "You got a First Class!" If it’s between 50 and 59.9, it prints "You got a Second Class." If it’s less than 50, it prints "You need to improve." This allows the program to handle multiple outcomes based on different conditions.

NESTED IF Statement

A NESTED IF statement is an IF statement inside another IF statement. This is useful when you have a situation where you need to check another condition only if the first condition is true.

Syntax:

if (condition1) {
    if (condition2) {
        // Code to execute if both condition1 and condition2 are true
    }
}

Example:

#include <stdio.h>

void main() {
    int number = 35;
    if (number % 5 == 0) {
        if (number % 7 == 0) {
            printf("The number is divisible by both 5 and 7.");
        }
        else {
            printf("The number is divisible by 5 but not by 7.");
        }
    }
}

In this example, the program first checks if the number is divisible by 5. If it is, then it checks if the number is also divisible by 7. If both conditions are true, it prints "The number is divisible by both 5 and 7." If only the first condition is true, it prints "The number is divisible by 5 but not by 7."

SWITCH Statement

The SWITCH statement is like a more organized and readable version of an ELSE IF ladder. It’s used when you have many conditions to check, and each condition is compared against the same variable or expression.

Syntax:

switch (expression) {
    case value1:
        // Code to execute if expression matches value1
        break;
    case value2:
        // Code to execute if expression matches value2
        break;
    ...
    default:
        // Code to execute if none of the above cases match
        break;
}

Example:

#include <stdio.h>

void main() {
    int choice;
    printf("Choose a color: ");
    printf("\n1. Red");
    printf("\n2. Black");
    printf("\n3. Yellow");
    printf("\n4. Blue");
    printf("\n5. Pink");
    scanf("%d", &choice);
    switch (choice) {
        case 1: printf("\nYou chose Red.");
            break;
        case 2: printf("\nYou chose Black.");
            break;
        case 3: printf("\nYou chose Yellow.");
            break;
        case 4: printf("\nYou chose Blue.");
            break;
        case 5: printf("\nYou chose Pink.");
            break;
        default: printf("\nInvalid choice. Please choose a number between 1 and 5.");
    }
}

In this example, the program asks the user to choose a color by entering a number. The SWITCH statement then matches the user’s choice with the correct case. If the user enters 1, the program prints "You chose Red." If they enter 2, it prints "You chose Black," and so on. If the user enters a number that doesn’t match any case (like 6), the program will go to the default case and print "Invalid choice."

Note: Instead of a condition, you can pass other values or statements in the if(1) expression. For example, you can pass numbers or use if(0) statements. Experiment with different values to see how the statement behaves.printf("hi");

Understanding selective statements is crucial for making decisions in your programs. Whether you use IF, ELSE IF, NESTED IF, or SWITCH statements, the goal is to control the flow of your program based on different conditions. Practice these concepts with different examples, and soon you'll be making complex decisions in your programs with ease!

Thank you for reading! Your thoughts and suggestions are always welcome—let’s connect in the comments below!