Conditional Statements in C: Making Your Code Solve Real Problems

 


When you start coding in C, your program runs line by line, like a train 🚂 following tracks. But life isn’t always that simple. Sometimes you need to make a choice:

  • If your exam score is high, you celebrate 🎉.
  • If it rains, you take an umbrella .
  • If you’re hungry, you order pizza 🍕.

That’s where conditional statements come in. They give your code the power to decide what to do, just like you do in real life.

In this blog, let’s learn the four main types of conditional statements in C — using problem-oriented examples.

1. The if Statement — Checking One Condition

Problem: A college requires 75% attendance to sit for the final exam. Write a program to check if a student is eligible.

#include <stdio.h>
int main() {
int attendance = 80;
if(attendance >= 75) {
printf("Eligible for the final exam ✅\n");
}
return 0;
}

💡 Explanation:

  • The condition (attendance >= 75) checks if the student meets the rule.
  • If true → “Eligible” is printed.
  • If false → nothing happens.

2. 🤝 The if-else Statement — Two Possible Outcomes

Problem: You want to build a login system. If the password is correct, access is granted. Otherwise, access is denied.

#include <stdio.h>
#include <string.h>
int main() {
char password[20] = "Cprogram";
if(strcmp(password, "Cprogram") == 0) {
printf("Login successful 🎉\n");
} else {
printf("Access denied ❌\n");
}
return 0;
}

💡 Explanation:

  • strcmp() compares the given password with "Cprogram".
  • If they match → success message.
  • Else → denial message.

This is a classic yes/no scenario, perfect for if-else.

3. 🔗 The else if Ladder — Multiple Ranges

Problem: An online shop gives discounts based on purchase amount:

  • Above ₹5000 → 20% discount
  • Between ₹2000 and ₹5000 → 10% discount
  • Below ₹2000 → No discount
#include <stdio.h>
int main() {
int amount = 3500;
if(amount > 5000) {
printf("You get a 20%% discount 🥳\n");
} else if(amount >= 2000) {
printf("You get a 10%% discount 👍\n");
} else {
printf("No discount, sorry 😢\n");
}
return 0;
}
💡 Explanation:
  • The program checks conditions top to bottom.
  • If amount > 5000 → first block runs.
  • If not, it tries the next condition.
  • If none are true → the else runs.

This is handy when there are multiple categories.

4. 🎚️ The switch Statement — Menu Style Choices

Problem: You’re making a food ordering system. The user enters a number, and the program prints their order.

#include <stdio.h>
int main() {
int choice = 3;
switch(choice) {
case 1:
printf("You ordered Pizza 🍕\n");
break;
case 2:
printf("You ordered Burger 🍔\n");
break;
case 3:
printf("You ordered Pasta 🍝\n");
break;
case 4:
printf("You ordered Fries 🍟\n");
break;
default:
printf("Invalid choice ❌\n");
}
return 0;
}

💡 Explanation:

  • switch(choice) checks which case matches the value.
  • Each case represents one menu option.
  • default runs if no case matches.
  • break ensures the program doesn’t continue to the next case.

This is perfect when you have fixed options to choose from.

🌟 Quick Recap

  • if → Best for one simple check.
  • if-else → Perfect when there are only two outcomes (yes/no).
  • else if ladder → Use when you have multiple ranges/categories.
  • switch → Great for menu-style fixed choices.

🚀 Final Thoughts

Conditional statements are the foundation of making your code smart and problem-solving. Without them, programs can’t adapt to situations.

Now that you understand these basics, you’re ready to learn the next level: Conditional Expressions (? :) — a shorter, one-line way to make decisions. (That’s our next blog ).

👉 Quick Challenge for You:
Can you write a program using else if that grades students as:

  • 90–100: A
  • 75–89: B
  • 50–74: C
  • Below 50: Fail

Post a Comment

Previous Post Next Post