C if-else Statement

Introduction

In the previous chapter, we learned about basics of control flow statements in C programming. In this chapter, we will focus on the if-else statement in C. The if-else statement is a fundamental control flow structure that allows you to make decisions in your code based on certain conditions.

What is an if-else Statement?

The if-else statement enables your program to execute certain blocks of code conditionally. If the condition specified in the if statement evaluates to true, the corresponding block of code is executed. If the condition evaluates to false, the block of code following the else statement is executed.

If Statement

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

Syntax

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

Example

Example: Checking Voting Eligibility

#include <stdio.h>

int main() {
    int age = 18; // Declaring an integer variable 'age' and assigning it a value of 18

    if (age >= 18) {
        printf("You are eligible to vote.\n"); // Printing a message if the condition is true
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

You are eligible to vote.

If-else Statement

The if-else statement is used to execute one block of code if a condition is true and another block of code if the condition is false.

Syntax

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

Example

Example: Checking Voting Eligibility

#include <stdio.h>

int main() {
    int age = 16; // Declaring an integer variable 'age' and assigning it a value of 16

    if (age >= 18) {
        printf("You are eligible to vote.\n"); // Printing a message if the condition is true
    } else {
        printf("You are not eligible to vote.\n"); // Printing a message if the condition is false
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

You are not eligible to vote.

If else-if Ladder

When you need to check multiple conditions, you can use an if else-if ladder. This structure allows you to test multiple conditions in sequence.

Syntax

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else if (condition3) {
    // Code to execute if condition3 is true
} else {
    // Code to execute if all conditions are false
}

Example

Example: Grading System

#include <stdio.h>

int main() {
    int marks = 85; // Declaring an integer variable 'marks' and assigning it a value of 85

    if (marks >= 90) {
        printf("Grade: A\n"); // Printing Grade A if marks are 90 or above
    } else if (marks >= 75) {
        printf("Grade: B\n"); // Printing Grade B if marks are 75 or above
    } else if (marks >= 60) {
        printf("Grade: C\n"); // Printing Grade C if marks are 60 or above
    } else {
        printf("Grade: D\n"); // Printing Grade D if marks are below 60
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Grade: B

Nested if

You can also nest if statements inside other if statements to create more complex decision-making structures.

Syntax

if (condition1) {
    // Code to execute if condition1 is true
    if (condition2) {
        // Code to execute if condition2 is true
    } else {
        // Code to execute if condition2 is false
    }
} else {
    // Code to execute if condition1 is false
}

Example

Example: Nested if

#include <stdio.h>

int main() {
    int number = 10; // Declaring an integer variable 'number' and assigning it a value of 10

    if (number >= 0) {
        printf("Number is non-negative.\n"); // Printing message if number is non-negative
        if (number % 2 == 0) {
            printf("Number is even.\n"); // Printing message if number is even
        } else {
            printf("Number is odd.\n"); // Printing message if number is odd
        }
    } else {
        printf("Number is negative.\n"); // Printing message if number is negative
    }

    return 0; // Returning 0 to indicate successful execution
}

Output:

Number is non-negative.
Number is even.

Conclusion

The if-else statement is used for making decisions in your C programs. It allows you to execute different blocks of code based on various conditions. By understanding and using if, if-else, if else-if ladder, and nested if structures, you can create complex and dynamic programs that respond to different scenarios and inputs.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top