</>
ShikshaCSLearn. Code. Grow.
🔍
☕ Support Us
ShikshaCSNotesProgramming in C
Programming in C
🕒 7 min read

Control Statements (if-else, switch)

How your program makes decisions and picks different paths.

if-else

The if statement runs a block of code only if a condition is true. else runs when it's false, and else if lets you check multiple conditions in sequence, stopping at the first one that matches.

if (marks >= 90) {
  printf("Grade A");
} else if (marks >= 75) {
  printf("Grade B");
} else {
  printf("Grade C");
}

switch-case

switch compares one variable against multiple possible values (cases), running the matching block. Each case should end with 'break' — otherwise execution 'falls through' into the next case, which is a common source of bugs.

switch (day) {
  case 1: printf("Monday"); break;
  case 2: printf("Tuesday"); break;
  default: printf("Other day");
}

if-else vs switch

Use if-else for range checks (marks >= 90) or complex conditions (multiple &&/||). Use switch when comparing one variable against several exact values — it's often cleaner to read for that specific case.

🌍 Real-World Use

A grading system, a traffic light simulator, or an ATM's PIN-check flow all rely on control statements to decide which path of logic to execute based on the current situation.

💡 Pro Tip

Forgetting 'break' in a switch statement is one of the most common real bugs in C — always double check each case ends with break unless you intentionally want fall-through behavior.

🧪 Quick Self-Test

Check what you just learned — no pressure, just practice.

1. What happens if you forget 'break' in a switch case?

2. Which is better for checking a range like marks >= 90?

← Back to all Notes