🔍 if Condition in C Programming
📘 Short Explanation:
In C, the if
statement is used to make decisions. It checks a condition—if it's true, the code inside runs. Otherwise, it’s skipped.
It helps control the program’s flow based on logic and input.
✅ Basic Syntax
if (condition) { // code to execute if condition is true }
🧪 Example: Simple if Statement
#include <stdio.h> int main() { int age = 20; if (age >= 18) { printf("You are eligible to vote.\n"); } return 0; }
🧠 Output:
You are eligible to vote.
🔁 if-else Syntax
if (condition) { // runs if true } else { // runs if false }
📋 Example:
int number = 5; if (number % 2 == 0) { printf("Even number\n"); } else { printf("Odd number\n"); }
🔗 if-else if-else Ladder
if (condition1) { // block 1 } else if (condition2) { // block 2 } else { // default block }
📋 Example:
int score = 85; if (score >= 90) { printf("Grade A\n"); } else if (score >= 80) { printf("Grade B\n"); } else { printf("Grade C\n"); }
⚠️ Tips to Remember
- Use
==
for comparison, not=
. - The condition must return true (non-zero) or false (zero).
- You can use logical operators:
&&
(AND),||
(OR),!
(NOT).
📌 Summary
The if
statement is the foundation of decision-making in C. It allows your program to respond to conditions, making it flexible and interactive. With if
, else
, and else if
, you can cover all logic scenarios effectively.