There’s a moment I’ll never forget while teaching if, else and nested if in C. It was a humid Tuesday afternoon, the kind where the ceiling fans in the lab spun more out of habit than effectiveness. I had just scribbled a question on the whiteboard:
“How does a program make decisions?” One of my students, Karan, who usually sat in the back building code like spaghetti, looked up and said, “Sir, I thought we make decisions, not the code.” I smiled. “True. But once you give code the power to choose, it becomes alive.” And that’s when I introduced the beating heart of logic in C programming: if, else, and when you feel brave nested if.
The First “If”: Teaching Decisions With Marks
Like every beginner’s rite of passage, we start with the grades example.if (marks >= 90) {
printf(“Grade A”);
} else if (marks >= 80) {
printf(“Grade B”);
} else {
printf(“Work harder!”);
}
It seems so innocent, right? Students usually nod along, and someone says, “Okay, easy. Conditions. If one is true, do this. If not, check the next.”
But then I throw in a curveball.
“What if marks is 85?”
Multiple hands go up. “Grade B!”
“Perfect. And what if it’s 95?”
“Grade A!”
Then I ask: “What if it’s 75?”
Silence. Then a shy voice: “Work harder?”
The room giggles. That’s when it clicks for most of them code doesn’t get emotional. It’s not your mom. It doesn’t say, “Well, you almost got a B.” It follows cold, hard logic.
A Real Moment: Misplaced Curly Braces and Wrong Outputs
In one batch, a student named Harika came to me, utterly frustrated.
“Sir, it always prints ‘Work harder’ no matter what the marks are!”
We sat down together and looked at her code:
if (marks >= 90);
{
printf(“Grade A”);
}
Did you spot it? That innocent looking semicolon after if (marks >= 90); is lethal. It ends the if prematurely, and the {} block that follows executes regardless of the condition.
This tiny mistake led to so many wrong outputs that I now say:
“In C, a semicolon can kill logic.”
We rewrote it without the semicolon and everything worked. She smiled. I smiled. And that day, Harika taught half the class with her mistake.
Nested if: The Jenga Tower of Logic
There’s power in nesting but with great power comes… absolute chaos if you go too far.
Let me share something humbling. Years ago, in my early teaching days, I wrote a five level nested if to simulate a login system with user roles, password checks, timeouts, location constraints, and even user behavior.
And then I debugged it for three hours. That code became a jungle. Indentation got weird. else blocks seemed to attach to the wrong if. I couldn’t tell who was guarding what.
Here’s a simple version I use now:
if (loggedIn) {
if (isAdmin) {
printf(“Access granted to admin panel.”);
} else {
printf(“Access granted to user panel.”);
}
} else {
printf(“Please log in first.”);
}
See how tidy and readable that is?
Now imagine four more layers on top. Students start breathing heavily just looking at it.
So I now say:
“A nested if should read like a conversation, not a conspiracy theory.”
Real Life Analogy: Traffic Lights
To make it relatable, I ask students to picture a traffic signal.
- If it’s green, you go.
- Else if it’s yellow, you slow down.
- Else, you stop.
Sounds familiar?
if (light == GREEN) {
printf(“Go\n”);
} else if (light == YELLOW) {
printf(“Slow down\n”);
} else {
printf(“Stop\n”);
}
Then I ask, “What if the light is blinking red and it’s 2 AM?”
That’s when you bring in nested logic.
if (light == RED) {
if (isLateNight) {
printf(“Proceed with caution.\n”);
} else {
printf(“Stop.\n”);
}
}
Suddenly, students start seeing decision trees all around them.
Common Mistakes My Students Made (and What They Taught Me)
1. Confusing = with ==
I’ve seen dozens of students write:
if (marks = 90) // Wrong!
This assigns 90 to marks instead of comparing it. And it always returns true unless the value is zero.
Now I always say:
“Think of == as a mirror. Both sides should look the same.”
2. Ignoring else if Order
One student once wrote:
if (marks >= 50) {
printf(“Pass”);
} else if (marks >= 90) {
printf(“Excellent”);
}
Guess what happened with 95 marks?
Only “Pass” printed, because the first if ate the condition.
Lesson: Always check for higher thresholds first.
3. Forgetting Braces in Nested Blocks
They assume:
if (a > b)
if (a > c)
printf(“A is biggest”);
else
printf(“C is biggest”);
But without curly braces, the else belongs to the second if, not the first. This “dangling else” problem has haunted even professionals.
That’s why I tell students:
“When in doubt, brace it out.”
Conditional Operator: The Slick Cousin
Just when students start getting comfy with if else, I introduce its cousin the ternary operator:
result = (marks >= 33) ? ‘P’ : ‘F’;
One line. Elegant. Dangerous if misused.
I explain it as a compressed if else, good for quick decisions, but not for complex logic.
I remember I once asked, “Which one do you prefer: this compact line or a longer if?”
A student shot back, “The short one, obviously!”
“Great,” I said, “But can you read it after 6 months?”
That got a chuckle. But also made them think. Writing code isn’t just about getting it to work. It’s about making it understandable to your future self.
The Day I Let Students Write Rules for a Game
I once gave a class project where students had to simulate a board game. Every player got health, points, and magic power. They had to write logic like:
if (health <= 0) {
printf(“You died.”);
} else if (magic > 50 && points > 100) {
printf(“You leveled up!”);
} else {
printf(“Keep playing.”);
}
Watching them piece together game rules using nested if was beautiful. And chaotic. One student’s game allowed resurrection if you had a magic sword written as a 7 level deep nested if.
They learned two things that day:
- How powerful decision making is.
- Why too much nesting is a sign to refactor.
Practical Tips I Give Every Batch
- Write the condition like you’re talking to a human. “If user is logged in and is admin” reads better than chained conditions.
- Use comments in nested if. Not for the compiler, but for the next person who will cry reading it.
- When conditions feel heavy, try breaking logic into functions.
- Avoid nesting beyond 3 levels. If it’s that deep, rethink your design.
- Use else if for mutually exclusive conditions. If multiple conditions could be true at once, use plain if.
Final Thought: Teach Code Like Ethics
I often say:
“if is not just code. It’s a choice.”
And choice is what makes your program think, adapt, and act.
Just like in life, your choices in code must be clear, fair, and maintainable.
A program without if is just a monologue. But with it? It becomes a conversation.
And that’s what makes it powerful.
Can I use if without an else?
Yes! You don’t always need an else. If you only want to act when a condition is true, skip the else.
What’s the maximum number of else if blocks I can use?
Technically, as many as you want. But for readability, keep it under 5 6 unless you enjoy debugging mazes.
Should I use ternary operators over if else?
Use ternary (? 🙂 only for short, simple decisions. Avoid it when there’s more than one line of logic. :
Is nested if bad practice?
No, but deep nesting is. When nesting gets too complex, use helper functions or rethink your logic.