When programming in C, understanding how to use the if-else statement is fundamental. One common task for beginners is checking whether a student has passed or failed based on their marks. In this tutorial, you’ll learn how to use if-else to determine the student’s result by comparing their marks with a passing threshold.
Why Use If-Else to Check Pass or Fail in C Programming?
The if-else statement in C is an essential decision-making structure. It allows your program to evaluate conditions and execute different code based on whether those conditions are true or false. For example, checking if a student’s marks are above or below a passing threshold is a straightforward application of if-else logic. This technique can also be extended to handle more complex conditions, such as grading systems.
Some common use cases include:
Handling conditions in data analysis and grading systems
School or university result calculations
Creating automated report cards
Validating user input in education apps
C Program to Read Marks and Check Pass or Fail
#include <stdio.h> int main() { int marks; // Read marks from the user printf("Enter your marks: "); scanf("%d", &marks); // Check if the student passed or failed if (marks >= 40) { printf("Result: Pass\n"); } else { printf("Result: Fail\n"); } return 0; }
Sample Input and Output
Example 1:
Input: 55
Output: Result: Pass
Example 2:
Input: 32
Output: Result: Fail
How This Program Works
- First, the program takes the marks from the user.
- Then, it checks if the marks are greater than or equal to 40.
- If the condition is true, the program prints “Pass.”
- Otherwise, it prints “Fail.”
You can also modify the passing mark by changing the condition from 40
to any other number (like 35
or 50
) based on your requirement.
Use Cases and Extensions
This logic is useful not only for simple pass/fail checks but also for:
- Creating grade systems using multiple
if-else if
blocks - Automating result calculations
- Validating user input based on conditions
- Developing school or college result software
Key Concepts for Beginners
- If-Else Logic: The
if-else
statement evaluates a condition and executes code based on whether the condition is true or false. - Threshold Comparison: In this case, we’re comparing the student’s marks to a set threshold to determine pass or fail.
- Conditional Flow: This program demonstrates the basic flow of conditions in a program, which is essential for more complex logic in real-world applications.
Further Enhancements
You can extend this program to:
Generate automated grade reports based on the input
Include additional grades (e.g., A
, B
, C
for different score ranges)
Handle multiple subjects and calculate average marks
Incorporate user-defined passing thresholds
[…] If Else vs Switch in C […]