Comparison Operators
Before writing conditions, you need to know comparison operators — these compare two values and return either true (1) or false (0). Every if statement uses these.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | true (1) |
| != | Not equal to | 5 != 3 | true (1) |
| > | Greater than | 7 > 4 | true (1) |
| < | Less than | 3 < 9 | true (1) |
| >= | Greater than or equal | 5 >= 5 | true (1) |
| <= | Less than or equal | 4 <= 6 | true (1) |
| && | AND — both must be true | 5>2 && 3<8 | true (1) |
| || | OR — at least one true | 5>9 || 3<8 | true (1) |
| ! | NOT — reverses result | !(5==5) | false (0) |
if (x = 5) — assigns 5 to x, always evaluates as TRUE — BUG!if (x == 5) — checks if x equals 5 — correct!The compiler will not warn you. This bug is very hard to find.
if / else if / else
The if statement runs a block of code only when a condition is true. Chain multiple conditions using else if. The final else catches everything that did not match.
- if — checks first condition
- else if — checks next condition only if previous was false
- else — runs when nothing above matched
- Only one block ever runs — as soon as one condition is true, the rest are skipped
if / else if / else — execution flow
#include <stdio.h> int main() { int marks; printf("Enter your marks (0-100): "); scanf("%d", &marks); if (marks >= 90) { printf("Grade A — Excellent!\n"); } else if (marks >= 75) { printf("Grade B — Good!\n"); } else if (marks >= 50) { printf("Grade C — Average\n"); } else if (marks >= 0) { printf("Grade F — Failed\n"); } else { printf("Invalid marks!\n"); } return 0; }
#include <stdio.h> int main() { int n; printf("Enter a number: "); scanf("%d", &n); // Nested if — if inside an if if (n > 0) { printf("Positive "); if (n % 2 == 0) printf("and Even\n"); else printf("and Odd\n"); } else if (n < 0) { printf("Negative number\n"); } else { printf("Zero\n"); } return 0; }
switch-case Statement
switch is cleaner than a long chain of else if when you check one variable against many exact values. Each case is one possible value. Always end with break — otherwise execution falls through to the next case!
- switch(x) — evaluates x once
- case n: — matches if x equals n
- break; — exits the switch (required!)
- default: — runs when no case matches (like else)
#include <stdio.h> int main() { int day; printf("Enter day (1-7): "); scanf("%d", &day); switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid!\n"); } return 0; }
#include <stdio.h> int main() { int month = 4; // April switch (month) { // Multiple cases sharing same action (no break between) case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days\n"); break; case 4: case 6: case 9: case 11: printf("30 days\n"); break; case 2: printf("28 or 29 days\n"); break; default: printf("Invalid month\n"); } return 0; }
Without
break, execution continues into the next case automatically — no check needed. This is usually a bug. The only safe intentional use is grouping cases like case 4: case 6: case 9: shown above.
| Feature | if / else if | switch |
|---|---|---|
| Best for | Ranges and complex conditions | Exact single-value matches |
| Condition type | Any expression | Integer or char only |
| Multiple conditions | Use && and || | Multiple case labels |
| Default case | else | default: |
| Readability | Good for 2–3 conditions | Better for 4+ exact values |
Quick Quiz
What does if (x = 10) do in C?
In an if/else if/else chain, how many blocks can run?
What happens if you forget break in a switch case?
When should you use switch instead of if/else if?
Lesson Checklist
- I know all 6 comparison operators and 3 logical operators
- I understand the difference between = (assign) and == (compare)
- I can write if / else if / else chains correctly
- I understand nested if — if inside an if
- I can write a switch-case with break and default
- I understand fall-through and when it is useful
- I know when to use switch vs if-else
- I completed the quiz
Day 5 Preview
- 🔁 for loop — counted repetition Day 5
- 🔄 while loop — condition-driven repetition Day 5
- 🔃 do-while loop — runs at least once Day 5
- ⛔ break & continue — control loop flow Day 5