If Else Statements Real-Time Examples in C

SPONSOR AD

If Else Statements Real-Time Examples in C

In this article, I will discuss the If Else Statements Real-Time Examples in C Language. Please read our previous article discussing the basic concepts of If Else Statements in C Language. At the end of this article, you will understand how to implement the following programs using If Else Statements in C Language. The IF Else Statements extends the if statement to execute a different code block if the condition is false.

  1. Grading System
  2. Checking Even or Odd
  3. Age Group Classification
  4. Simple Login System
  5. Leap Year Checker
  6. Traffic Light Control
  7. Emergency Alert System
  8. Industrial Automation
  9. Temperature Monitoring System
Grading System

A common application is to categorize scores into grades:

#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;
}
Checking Even or Odd

Determine 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 even.\n", number);
    } else {
        printf("%d is odd.\n", number);
    }

    return 0;
}
Age Group Classification

Classifying age groups for different life stages:

SPONSOR AD
#include <stdio.h>

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

    if (age < 13) {
        printf("You are a child.\n");
    } else if (age < 20) {
        printf("You are a teenager.\n");
    } else if (age < 60) {
        printf("You are an adult.\n");
    } else {
        printf("You are a senior.\n");
    }

    return 0;
}
Simple Login System

A basic example of a login system using if-else:

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

int main() {
    char storedPassword[] = "myPassword";
    char inputPassword[50];

    printf("Enter your password: ");
    scanf("%s", inputPassword);

    if (strcmp(storedPassword, inputPassword) == 0) {
        printf("Access granted.\n");
    } else {
        printf("Access denied.\n");
    }

    return 0;
}
Leap Year Checker

Determining if a year is a leap year:

#include <stdio.h>

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

    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0) {
                printf("%d is a leap year.\n", year);
            } else {
                printf("%d is not a leap year.\n", year);
            }
        } else {
            printf("%d is a leap year.\n", year);
        }
    } else {
        printf("%d is not a leap year.\n", year);
    }

    return 0;
}
Traffic Light Control
#include <stdio.h>

int main() {
    char trafficLight = 'G'; // 'R' for Red, 'Y' for Yellow, 'G' for Green

    if (trafficLight == 'R') {
        printf("Stop.\n");
    } else if (trafficLight == 'Y') {
        printf("Caution.\n");
    } else if (trafficLight == 'G') {
        printf("Go.\n");
    } else {
        printf("Invalid traffic light signal.\n");
    }

    return 0;
}

This could be part of a traffic control system interpreting signals from traffic lights.

Emergency Alert System
#include <stdio.h>

int main() {
    int smokeDetected = 1; // 1 for true, 0 for false
    int temperatureHigh = 0; // 1 for true, 0 for false

    if (smokeDetected && temperatureHigh) {
        printf("Fire alarm: Evacuate immediately.\n");
    } else if (smokeDetected || temperatureHigh) {
        printf("Warning: Check for potential fire hazards.\n");
    } else {
        printf("All clear.\n");
    }

    return 0;
}

This example represents an emergency system that reacts to smoke and high-temperature readings.

Industrial Automation
#include <stdio.h>

int main() {
    int conveyorBeltSpeed = 5; // Speed of a conveyor belt

    if (conveyorBeltSpeed > 10) {
        printf("Warning: Conveyor belt is moving too fast.\n");
    } else if (conveyorBeltSpeed < 2) {
        printf("Alert: Conveyor belt is moving too slow.\n");
    } else {
        printf("Conveyor belt is operating at optimal speed.\n");
    }

    return 0;
}
Temperature Monitoring System

In this scenario, the program will:

  • Read the temperature value (simulated by a variable for the sake of example).
  • If the temperature is above a certain threshold, it will trigger a cooling mechanism.
  • If the temperature is below a certain threshold, it will trigger a heating mechanism.
  • Otherwise, it will maintain the current state.

Sample Code

#include <stdio.h>

int main() {
    int temperature = 25; // Assume this is the temperature reading from a sensor
    int coolThreshold = 30;
    int heatThreshold = 20;

    if (temperature > coolThreshold) {
        printf("Temperature is too high. Activating cooling system.\n");
    } else if (temperature < heatThreshold) {
        printf("Temperature is too low. Activating heating system.\n");
    } else {
        printf("Temperature is optimal. Maintaining current state.\n");
    }

    return 0;
}
Explanation
  • The temperature variable is set to 25, simulating a sensor reading.
  • The coolThreshold is the upper limit, above which the cooling system will be activated.
  • The heatThreshold is the lower limit, below which the heating system will be activated.
  • The if-else statements check these conditions and print the corresponding actions.
  • In a real-world application, you would replace the fixed temperature value with actual sensor readings. This program can be part of a larger system in smart homes, industrial temperature regulation systems, or environmental control systems in data centers.

These examples demonstrate how if-else statements can be used in different scenarios, ranging from simple condition checks to more complex decision-making processes in C programming.

SPONSOR AD
If-Else Statement Real-Time Examples in C

Using if-else statements in real-time programs in C is essential for making decisions based on certain conditions. Real-time programs often interact with hardware or external events, where timely and accurate responses are critical. Here are some scenarios in real-time programming where if-else statements are commonly used:

  • Sensor Data Processing: In systems like embedded devices, if-else statements can be used to process and respond to sensor data. For example, if a temperature sensor reads above a certain threshold, an if-else statement can trigger a cooling system.
  • Control Systems: In control systems, such as those found in automotive applications or industrial machinery, if-else statements can be used to decide the system’s response based on current states or sensor inputs, like adjusting speed, changing direction, or managing safety features.
  • User Input Handling: In interactive systems, such as ATMs or medical devices, if-else statements can direct the program flow based on user input, like choosing options from a menu or entering specific data.
  • Communication Protocols: In systems that require communication, like networking devices, if-else statements are used to handle different types of incoming data packets or to manage error conditions in transmission.
  • Safety Checks and Alarms: In safety-critical systems, such as fire alarm systems or hospital patient monitoring systems, if-else statements can evaluate sensor data to trigger alarms or other safety measures when certain conditions are met.
  • Real-Time Scheduling: In real-time operating systems, if-else statements can be used for task scheduling based on priority or other criteria, ensuring that critical tasks get the necessary CPU time.
  • Battery-Powered Devices: In battery-operated devices, if-else statements can help manage power consumption by checking the battery level and adjusting device operation accordingly, such as dimming a display or entering a low-power mode.
  • Robotics: In robotics, if-else statements can be used for decision-making based on sensor inputs, like obstacle detection or pathfinding algorithms.

In the next article, I will discuss Nested If-Else Statements in C Language with examples. In this article, I explain If-Else Statements in Real-Time Examples in C Language. I hope you enjoy this 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.

SPONSOR AD

Leave a Reply

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