Nested If-Else Statements Real-Time Examples in C

Nested If-Else Statements Real-Time Examples in C

In this article, I will discuss the Nested If Else Statements Real-Time Examples in C Language. Please read our previous article discussing the basic concepts of Nested If Else Statements in C Language. At the end of this article, you will understand how to implement the following programs using Nested If Else Statements in C Language.

Nested If-Else Statement in C:

Nested if-else statements in C are used when you need to check multiple conditions and execute different blocks of code depending on those conditions. Here are a few examples to illustrate different scenarios:

Example: Checking Largest Number
#include <stdio.h>

int main() {
    int a = 10, b = 20, c = 30;

    if (a > b) {
        if (a > c) {
            printf("a is the largest");
        } else {
            printf("c is the largest");
        }
    } else {
        if (b > c) {
            printf("b is the largest");
        } else {
            printf("c is the largest");
        }
    }

    return 0;
}

In this example, the program checks which among a, b, or c is the largest using nested if-else statements.

Example: Grading System
#include <stdio.h>

int main() {
    int score = 85;

    if (score >= 90) {
        printf("Grade A");
    } else if (score >= 80) {
        if (score > 84) {
            printf("Grade B+");
        } else {
            printf("Grade B");
        }
    } else if (score >= 70) {
        printf("Grade C");
    } else {
        printf("Grade D");
    }

    return 0;
}

In this example, nested if-else statements are used within an else-if block to further categorize the grade based on the score.

Example: Nested If-Else with Logical Operators
#include <stdio.h>

int main() {
    int age = 20;
    char gender = 'F';

    if (age >= 18) {
        if ((gender == 'F') || (gender == 'f')) {
            printf("Adult Female");
        } else {
            printf("Adult Male");
        }
    } else {
        printf("Underage");
    }

    return 0;
}

This example checks for both age and gender using nested if-else statements combined with logical operators.

Example: Loan Eligibility System

This example checks for loan eligibility based on age and income.

#include <stdio.h>

int main() {
    int age, income;
    printf("Enter age and income: ");
    scanf("%d %d", &age, &income);

    if (age > 18) {
        if (income >= 30000) {
            printf("Eligible for loan\n");
        } else {
            printf("Not eligible due to low income\n");
        }
    } else {
        printf("Not eligible due to age\n");
    }

    return 0;
}
Example: Authentication System

This example uses nested if-else to authenticate a user based on username and password.

#include <stdio.h>
#include <string.h>

int main() {
    char username[25], password[25];
    printf("Enter username and password: ");
    scanf("%s %s", username, password);

    if (strcmp(username, "admin") == 0) {
        if (strcmp(password, "admin123") == 0) {
            printf("Access granted\n");
        } else {
            printf("Incorrect password\n");
        }
    } else {
        printf("Incorrect username\n");
    }

    return 0;
}
Example: Even or Odd Number Program

Let’s say you’re writing a program to check if a number is positive, negative, or zero, and then if it is positive, check if it is even or odd.

#include <stdio.h>

int main() {
    int number;
    printf("Enter a number: ");
    scanf("%d", &number);

    if (number > 0) {
        // Number is positive
        printf("The number is positive.\n");
        if (number % 2 == 0) {
            // Check if the number is even
            printf("Also, the number is even.\n");
        } else {
            // Number is odd
            printf("Also, the number is odd.\n");
        }
    } else if (number < 0) {
        // Number is negative
        printf("The number is negative.\n");
    } else {
        // Number is zero
        printf("The number is zero.\n");
    }

    return 0;
}

In this example, the outer if checks if the number is positive, negative, or zero. The nested if inside the first if block then checks if the positive number is even or odd. This structure allows for checking multiple conditions in a sequence.

These examples demonstrate the versatility of nested if-else statements in handling complex decision-making processes in C programming.

Nested If-Else Statement Real-Time Examples in C

Nested if-else statements in C are a fundamental control structure used to handle multiple conditions in programming. They are especially prevalent in real-time systems, where decision-making based on various sensor inputs or conditions is crucial. Here are some examples of real-time applications where nested if-else statements might be extensively used:

  • Traffic Light Control System: In a traffic control system, nested if-else statements can be used to manage traffic lights based on conditions like traffic density, emergency vehicle presence, or specific time schedules. For example, if a sensor detects an emergency vehicle, the traffic lights might change irrespective of the regular schedule.
  • Temperature Control Systems: In applications like air conditioners or industrial temperature controllers, nested if-else statements can manage the temperature based on readings from temperature sensors. Different actions (like turning on the cooling or heating system) are triggered if the temperature is above or below certain thresholds.
  • Home Security Systems: In a security system, various sensors (like motion and door sensors) provide input. Nested if-else statements can be used to determine the course of action (like triggering an alarm or sending notifications) based on which sensors are activated and under what conditions.
  • Robot Navigation Systems: In robotics, particularly in autonomous vehicles, nested if-else statements can evaluate data from multiple sensors (like distance sensors and cameras) to make decisions about movement, obstacle avoidance, or path planning.
  • Health Monitoring Systems: In medical devices like heart rate monitors or insulin pumps, nested if-else can be used to analyze patient data and decide on alerts or dosage adjustments based on various health parameters.
  • Industrial Automation: In manufacturing processes, these statements can control machinery based on inputs from various sensors, ensuring quality control, safety, and efficiency.
  • Smart Agriculture Systems: In agriculture, sensors can provide data about soil moisture, temperature, or light. Nested if-else statements can automate irrigation or other actions based on these parameters.

In the next article, I will discuss Switch Statements in C Language with Examples. In this article, I explain Nested If-Else Statements in Real-Time Examples in C Language. I hope you enjoy this Nested If-Else Statements Real-Time Examples in C Language article. I would like to have your feedback. Please post your feedback, questions, or comments about this article.

Leave a Reply

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