🔁 if-else Condition in C
🧪 Short Explanation:
In C, if-else
is used to choose between two blocks of code. If the condition is true, the if
block runs; otherwise, the else
block executes.
✅ Syntax
if (condition) { // code if true } else { // code if false }
📋 Example
#include <stdio.h> int main() { int number = 10; if (number % 2 == 0) { printf("Even number\n"); } else { printf("Odd number\n"); } return 0; }
🧠 Output: Even number
💡 Use Cases
- Checking even or odd numbers
- Determining eligibility (e.g., age ≥ 18)
- Menu selections or user inputs
⚠️ Common Mistakes
- Using
=
instead of==
for comparison - Forgetting the curly braces
{ }
for multiple lines - Leaving out the
else
block when needed
📌 Summary
The if-else
structure is the foundation of decision-making in C programming. It helps direct program flow based on logical conditions.