If Else Statement in C with Example in Hindi – Beginner Guide

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 भी देखेंगे। तो चलिए शुरू करते हैं!

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;
}
▶️ Output:
You are eligible to vote.

यहाँ 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;
}
▶️ Output:
7 is an Odd number.

यहाँ 7 % 2 == 0 condition false है, इसलिए else block चला। यह if else statement का एक classic example है।

If Else Statement in C with Example in Hindi

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;
}
Output:
▶️ Output:
Grade: A

यह 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;
}
▶️ Output:
The largest number is: 25

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 – दोनों truea>0 && b>0
||OR – कोई एक truea>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;
}
▶️ Output:
You can vote.

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;
}
▶️ Output:
Transaction Successful! Remaining Balance: 2000 rupees

यह 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 में इस तरह समझें:

If Else Statement in C 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;
}
▶️ Output:
Larger number: 20

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

Frequently Asked Questions

Q1. If else statement in C क्या होती है?
Ans. If else statement C language में एक decision-making statement है। यह एक condition check करती है – अगर condition true हो तो `if` block execute होता है, और false होने पर `else` block execute होता है। यह program को अलग-अलग paths पर execute करने की ability देती है।
Q2. If और If-Else में क्या फर्क है?
Ans. Simple if statement में सिर्फ condition के true होने पर कोई action होता है। अगर condition false हो तो कुछ नहीं होता। लेकिन if-else में दोनों cases handle होते हैं – true और false दोनों के लिए अलग code blocks होते हैं।
Q3. Nested If Else कब use करते हैं?
Ans. जब एक condition के अंदर और conditions check करनी हों, तब nested if-else use करते हैं। जैसे किसी number का सबसे बड़ा ढूँढना, या login system में पहले username फिर password check करना।
Q4. If-Else-If Ladder और Switch में क्या अंतर है?
Ans. if-else-if ladder किसी भी type की condition (ranges, expressions) handle कर सकती है। जबकि `switch statement` सिर्फ fixed values (integers, characters) के साथ काम करती है। Complex conditions के लिए if-else-if ladder बेहतर होती है।
Q5. क्या If Else के बिना C Program लिखा जा सकता है?
Ans. बहुत simple programs के लिए हाँ, लेकिन real-world applications में conditional logic जरूरी होती है। कोई भी meaningful program बिना decision making के अधूरा होता है। इसीलिए if else statement C programming की backbone मानी जाती है।

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top