If else in C

Introduction

C is a powerful and widely used programming language that allows developers to create efficient and performant applications. One of the fundamental features of C programming is the “If else” statement. The “If else” statement is a conditional control structure that allows the program to make decisions based on certain conditions. In this article, we will explore the “If else in C” statement in detail and provide a comprehensive understanding of its usage and best practices. Let’s dive in!

What is if else in C ?

The “If else” statement in C is used to create decision-making capabilities in a program. It allows the program to execute different sets of code based on whether a specified condition evaluates to true or false. The basic syntax of the “If else” statement is as follows:

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

The “condition” is an expression that can be either true or false. If the condition is true, the code block under the “if” section will be executed, otherwise, the code block under the “else” section will be executed.

Logical Operators in If else Statements

To create complex conditions in the “If else” statement, you can use logical operators such as “&&” (AND), “||” (OR), and “!” (NOT). These operators allow you to combine multiple conditions to make more sophisticated decisions.

Example:

int age = 25;
int salary = 50000;

if (age >= 18 && salary >= 30000)
{
    printf("You are eligible for the loan.\n");
}
else
{
    printf("Sorry, you are not eligible for the loan.\n");
}

In this example, the code checks whether the person’s age is 18 or above and their salary is 30,000 or above. If both conditions are true, the person is eligible for a loan.

Nested If else Statements

You can also nest “If else” statements within each other to create more intricate decision-making structures.

Example:

int num = 10;

if (num > 0)
{
    if (num % 2 == 0)
    {
        printf("The number is positive and even.\n");
    }
    else
    {
        printf("The number is positive but odd.\n");
    }
}
else
{
    printf("The number is not positive.\n");
}

In this example, the code checks whether the number is positive or not. If it’s positive, it further checks if it’s even or odd.

Examples of If else:

Now that we have a good understanding of the “If else” statement, let’s explore some practical examples to see how it can be used in real-world scenarios.

Example 1: Grading System

Suppose you are tasked with implementing a grading system based on students’ scores. The grading system should output the corresponding grade based on the score obtained.

#include <stdio.h>

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

    if (score >= 90)
    {
        printf("Grade: A\n");
    }
    else if (score >= 80)
    {
        printf("Grade: B\n");
    }
    else if (score >= 70)
    {
        printf("Grade: C\n");
    }
    else if (score >= 60)
    {
        printf("Grade: D\n");
    }
    else
    {
        printf("Grade: F\n");
    }

    return 0;
}

In this example, the program takes the student’s score as input and uses nested “If else” statements to determine the corresponding grade.

Example 2: ATM Withdrawal

Let’s consider an ATM withdrawal scenario where the user wants to withdraw cash. The ATM should check the account balance and the withdrawal amount to decide whether the transaction can be processed or not.

#include <stdio.h>

int main()
{
    int account_balance = 5000;
    int withdrawal_amount;

    printf("Enter the amount to withdraw: ");
    scanf("%d", &withdrawal_amount);

    if (withdrawal_amount <= account_balance)
    {
        account_balance -= withdrawal_amount;
        printf("Withdrawal successful. Remaining balance: %d\n", account_balance);
    }
    else
    {
        printf("Insufficient funds. Withdrawal cannot be processed.\n");
    }

    return 0;
}

In this example, the program checks whether the withdrawal amount is less than or equal to the account balance. If it is, the withdrawal is successful; otherwise, it informs the user of insufficient funds.