🪜 if-else if Ladder in C
🧪 Short Explanation:
The if-else if
ladder checks multiple conditions one by one. The first true condition runs its block and skips the rest. If none match, the else
block runs (optional).
✅ Syntax
if (condition1) { // block 1 } else if (condition2) { // block 2 } else if (condition3) { // block 3 } else { // default block }
📋 Example
#include <stdio.h> int main() { int score = 75; if (score >= 90) { printf("Grade A\\n"); } else if (score >= 80) { printf("Grade B\\n"); } else if (score >= 70) { printf("Grade C\\n"); } else { printf("Grade D\\n"); } return 0; }
🧠 Output: Grade C
💡 Common Use Cases
- Grading systems
- Tax calculation slabs
- Menu or user input logic
⚠️ Tips & Mistakes to Avoid
- Ensure conditions are in correct order (most specific to least)
- Don't forget to use curly braces
{ }
- Only the first true condition runs
📌 Summary
The if-else if
ladder is useful when you need to test several possible conditions. It improves code readability and logic flow in programs.