If else statement in C एक ऐसा fundamental concept है जिसे हर C programmer को समझना जरूरी है। जब भी हमें किसी program में कोई decision लेनी होती है – जैसे कि “अगर यह condition सच है तो यह करो, नहीं तो वो करो” – तब हम if else statement का उपयोग करते हैं। यह concept बिल्कुल वैसा ही है जैसे हम रोज़ाना की ज़िंदगी में सोचते हैं।
इस blog post में मैं आपको if else statement in C with Example in Hindi में आसानी से समझाऊंगा कि यह कैसे काम करती है, इसके कितने types होते हैं, और real-life examples के साथ इसे C language में कैसे लिखा जाता है। चाहे आप एक beginner हों या अपनी C programming skills को और मजबूत बनाना चाहते हों, यह guide आपके लिए बहुत helpful साबित होगी।
हम यहाँ simple if, if-else, if-else-if ladder, और nested if-else – सभी को step by step समझेंगे, साथ में working code examples और उनका output भी देखेंगे। तो चलिए शुरू करते हैं!
Table of Contents
C में Decision Making क्या होती है?
Programming में हम हमेशा एक ही क्रम में instructions नहीं देते। कभी-कभी हमें program को यह decide करना होता है कि किस condition में क्या करना है। इसी को decision making कहते हैं।
C language में decision making के लिए मुख्यतः इन statements का उपयोग होता है:
- if statement
- if-else statement
- if-else-if ladder
- nested if-else
- switch statement
इस post में हम if else statement in C with Example in Hindi में पूरी तरह focus करेंगे।
If Statement in C – Basic Syntax और Example
जब हमें सिर्फ एक condition check करनी हो और condition सच होने पर कुछ execute करना हो, तब हम simple if statement का use करते हैं।
Syntax:
if (condition)
{
// If the condition is true, then this code will execute
}
Example – Simple If Statement:
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
return 0;
}
यहाँ age >= 18 condition true है, इसलिए printf() execute हुआ। अगर age 15 होती, तो कुछ भी print नहीं होता।
If Else Statement in C – पूरी Understanding Example के साथ
If else statement C में समझना बहुत आसान है। जब condition true हो तो एक ( if ) block execute होता है, और जब condition false हो तो दूसरा ( else ) block execute होता है।
Syntax:
if (condition)
{
// condition true होने पर
}
else
{
// condition false होने पर
}Example – If Else Statement:
#include <stdio.h>
int main() {
int number = 7;
if (number % 2 == 0) {
printf("%d is an Even number.\n", number);
}
else {
printf("%d is an Odd number.\n", number);
}
return 0;
}
यहाँ 7 % 2 == 0 condition false है, इसलिए else block चला। यह if else statement का एक classic example है।

If-Else-If Ladder – Multiple Conditions Handle करना
जब हमें एक से अधिक conditions check करनी हों, तब हम if-else-if ladder का use करते हैं। यह एक chain की तरह होती है जिसमें conditions ऊपर से नीचे check होती हैं।
Syntax:
if (condition1)
{
// condition1 true
}
else if (condition2)
{
// condition2 true
}
else if (condition3)
{
// condition3 true
}
else
{
// सभी conditions false
}
Example – Student Grade System:
#include <stdio.h>
int main() {
int marks = 75;
if (marks >= 90) {
printf("Grade: A+\n");
}
else if (marks >= 75) {
printf("Grade: A\n");
}
else if (marks >= 60) {
printf("Grade: B\n");
}
else if (marks >= 45) {
printf("Grade: C\n");
}
else {
printf("Grade: Fail\n");
}
return 0;
}
यह example बहुत practical है। Real-world programs में इस तरह की conditional logic बहुत काम आती है।
Nested If Else – If के अंदर If
Nested if else का मतलब है एक if statement के अंदर दूसरी if statement लिखना। जब decision making complex हो, तब इसका use होता है।
Example – Largest of Three Numbers:
#include <stdio.h>
int main() {
int a = 10, b = 25, c = 15;
if (a > b) {
if (a > c) {
printf("The largest number is: %d\n", a);
}
else {
printf("The largest number is: %d\n", c);
}
}
else {
if (b > c) {
printf("The largest number is: %d\n", b);
}
else {
printf("The largest number is: %d\n", c);
}
}
return 0;
}
Nested if else लिखते समय indentation का ध्यान रखें, नहीं तो code पढ़ना मुश्किल हो जाता है।
Relational और Logical Operators – If Else के साथ
If else statement में conditions बनाने के लिए हम relational और logical operators का उपयोग करते हैं।
Relational Operators:
| Operator | मतलब | Example |
|---|---|---|
| == | बराबर है | a == b |
| != | बराबर नहीं है | a != b |
| > | बड़ा है | a > b |
| < | छोटा है | a < b |
| >= | बड़ा या बराबर | a >= b |
| <= | छोटा या बराबर | a <= b |
Logical Operators:
| Operator | मतलब | Example |
|---|---|---|
| && | AND – दोनों true | a>0 && b>0 |
| || | OR – कोई एक true | a>0 || b>0 |
| ! | NOT – उल्टा | !(a==b) |
Example – Logical Operator के साथ If Else:
#include <stdio.h>
int main() {
int age = 22;
int hasVoterID = 1; // 1 = true
if (age >= 18 && hasVoterID == 1) {
printf("You can vote.\n");
}
else {
printf("You are not eligible to vote.\n");
}
return 0;
}
Real Life Example – ATM Machine Logic
अभी तक हमने if else statement in C with Example in Hindi में लगभग सारे Topic cover कर लिये हैं, तो आइए एक interesting example देखते हैं जो if else conditional statement की real-world usefulness दिखाता है:
#include <stdio.h>
int main() {
int balance = 5000;
int withdrawal = 3000;
if (withdrawal <= balance) {
balance = balance - withdrawal;
printf("Transaction Successful!\n");
printf("Remaining Balance: %d rupees\n", balance);
}
else {
printf("Insufficient Balance!\n");
printf("Your Balance: %d rupees\n", balance);
}
return 0;
}
यह example दिखाता है कि if else statement in C का use करके हम real applications कैसे बना सकते हैं।
If Else लिखते समय होने वाली Common Mistakes
C programming सीखते वक्त beginners अक्सर कुछ गलतियाँ करते हैं। इन्हें ध्यान में रखें:
1. Semicolon का गलत use:
if (a > b); // यह गलत है! Semicolon नहीं लगाना चाहिए
{
printf(“Hello”);
}
2. Assignment vs Comparison:
if (a = 5) // यह assignment है, comparison नहीं
if (a == 5) // यह सही comparison है
3. Curly Braces भूलना: Single statement में braces optional हैं, लेकिन multiple statements के लिए {} जरूरी हैं।
Flowchart – If Else Statement को Visually समझें
If else statement की logic को flowchart में इस तरह समझें:

यह visual representation conditional branching को बहुत clearly दिखाता है।
Ternary Operator – If Else का Short Form
C में ternary operator if-else का एक short form है। यह एक line में if-else का काम करता है।
Syntax:
variable = (condition) ? value_if_true : value_if_false;
Example:
#include <stdio.h>
int main() {
int a = 10, b = 20;
int max;
max = (a > b) ? a : b;
printf("Larger number: %d\n", max);
return 0;
}
Conclusion ( निष्कर्ष )
If else statement in C with example in Hindi – यह topic programming की नींव है। इस post में हमने simple if, if-else, if-else-if ladder, nested if-else, और ternary operator सभी को real code examples और output के साथ समझा। आपने यह भी जाना कि common mistakes कौन-सी होती हैं और logical operators को if-else के साथ कैसे use किया जाता है। इन सभी concepts को अच्छे से समझ लेने के बाद आप C programs में effective decision making कर पाएंगे और आपका code ज़्यादा smart और functional बनेगा।
अब आपकी बारी है! इन examples को अपने compiler में लिखकर खुद run करें, values बदलकर देखें कि output कैसे change होता है, और अपने खुद के examples बनाने की कोशिश करें। Practice ही programming सीखने का सबसे अच्छा तरीका है। अगर आपके कोई सवाल हों तो नीचे comment करें, और इस post को अपने दोस्तों के साथ share करें जो C programming सीख रहे हैं।Frequently Asked Questions
