if else in C Language – Easy, Beginner Guide – हिंदी

आज हम C Programming भाषा के एक बहुत ही जरुरी और Basic Conceptके बारे में बात करेंगे, जिसे if-else Statement कहते हैं। प्रोग्रामिंग की दुनिया में हमें अक्सर ऐसे हालात आते हैं जब हमें कोई फैसला लेना होता है। जैसे अगर कोई शर्त पूरी हो , तो हम यह काम करेंगे, नहीं तो दूसरा काम करेंगे। if-else Statement हमारी इसी काम में मदद करता है। इसे Decision Making Statement, या Control Statement भी कहा जाता है, क्योंकि यह program के Flow को नियंत्रित करता है।

if else in C Language क्या है?

ज़रा सोचिये, हम अपनी रोजमर्रा की जिंदगी में कितने फैसले लेते हैं:

“अगर बारिश हो रही है तोह छाता लेके बाहर निकलूंगा, नहीं तो मैं छाता नहीं लूंगा। “

” अगर मेरे नंबर 35 नंबर से ज्यादा आये, तो मैं पास हो जाऊँगा, नहीं तो मैं फेल हो जाऊँगा। “

बस ! C Language में इसी तरह के ‘अगर‘ और ‘नहीं तो ‘ वाले फैसले लेने के लिए हम if-else in C language का इस्तेमाल करते हैं।

यह Statement एक Condition को check करता है।

  • अगर Condition सही (True) होती है, तो ‘if’ Block के अंदर लिखा हुआ Code Execute होता है।
  • और अगर Condition गलत (False) होती है, तो ‘else’ Block के अंदर लिखा हुआ कोड Execute होता है।

यह बहुत आसान है : या तो ‘if’ चलेगा या ‘else’ चलेगा। दोनों एक साथ कभी नहीं चलते।

सबसे पहले ‘if’ Statement को समझते हैं।

if Statement in C Lanaguage

if Statement का इस्तेमाल तब होता है जब हमें सिर्फ एक ही काम करना हो, वो भी सिर्फ तभी जब कोई Condition पूरी हो जाए। अगर Condition पूरी नहीं होती है, तो Program आगे बढ़ जाता है।

Syntax of ‘if’ Statement in C Language

if(Condition)
{
  // यह कोड तब चलेगा जब Condition True होगी। 
  
}
// अगर Condition False हुई तो Program सीधा यहां से आगे बढ़ जायेगा। 

if Statement Example Program

मान लीजिये, हमें सिर्फ इतना Check करना है कि अगर किसी की उम्र 18 या 18 से ज्यादा है, तो ही स्क्रीन पर “You Can Vote.” प्रिंट हो।

#include<stdio.h>
int main()
{
  int age = 20;
  if(age >= 20) // check age grater than 20
  {
    printf("You Can Vote.\n");
  }
  printf("Program End.");
  retrun 0;
}

Output:

You Can Vote.

Program End.

इस Image में if-else Statement का Real Life Example दिखाया गया है :

if in c programming

 if-else in C Language Real Life Example

if-else Statement सबसे ज्यादा इस्तेमाल होता है क्योंकि यह आपको दो रास्ते देता है :

  1. अगर Condition सही है, तो ‘if’ वाला काम करो।
  2. अगर Condition गलत है, तो ‘else’ वाला काम करो।

if-else Syntax In :

if(Condition)
{
  // यह Code तब चलेगा 
  // जब Condition True होगी 
}
else
{
    // यह Code तब चलेगा 
      // जब Condition false होगी 
}

Example:

अगर हमें यह भी बताना हो कि अगर उम्र 18 से कम है, तो क्या प्रिंट हो।

if else in c Example Code:

#include <stdio.h>

int main() {
    int age ;

    // Step 1: User input
    printf("Enter Your age: ");
    scanf("%d", &age);

    // Step 2: Condition check
    if (age >= 18) { 
        printf("\nCongratulation ! You can vote.");
    } else {
        printf("\nYour age is less than 18. You are not Eligible for vote.");
    }

    return 0;
}

Output Example:

Enter Your age: 17

Your age is less than 18. You are not Eligible for vote.

if else in C Language Flowchart:

if else in c language

else Statement अकेले क्यों नहीं इस्तेमाल कर सकते ?

else हमेशा if के साथ जुड़ा होता है। अकेले use करने पर Program में error आएगा।

#include<stdio.h>
int main(){
    else {
        printf("Invalid use of else");
    }
    return 0;
}

 Output: Error

 Nested if-else क्या है?

कई बार हमें एक Condition के अंदर दूसरी Condition को चेक करना होता है। मतलब जब एक ‘if’ की Condition सही हो जाये, तो उसके अंदर जाकर हमें एक और Decision लेना हो। तो उसे Nested if-else कहते हैं।

Nested if-else Syntax :

if(Outer_Condition)
{
  // यहाँ लिखा हुआ Code तब चलेगा जब Outer_Condition सही होगी
  if(Inner_Condtion)
  {
    // यहाँ का Code तभी Execute होगा जब Outer_Condition और Inner_Condition दोनों सही होगी
  }
  else
  {
    // यहाँ का Code तभी chalega जब Outer_Condition सही हो पर Inner_Condition गलत हो
  }
}
else
{
      // यहाँ का Code तभी chalega जब Outer_Condition गलत हो
}

Example:

मान लीजिये, हमें एक व्यक्ति की योग्यता चेक करनी है। Condition यह है कि अगर उसकी उम्र 18 से ज्यादा और उसके पास Driving लाइसेंस भी है, तभी वह गाड़ी चला सकता है।

Nested if-else Example Code

#include <stdio.h>

int main() {
    int age, license;

    printf("Enter your age: ");
    scanf("%d", &age);

    printf("Do you have a driving license? (Press 1 for Yes, 0 for No): ");
    scanf("%d", &license);

    if (age >= 18) {
        if (license == 1) {
            printf("You can drive.\n");
        } else {
            printf("You don't have a license, you cannot drive.\n");
        }
    } else {
        printf("You are underage, you cannot drive.\n");
    }

    return 0;
}

Output:

Enter your age: 20
Do you have a driving license? (Press 1 for Yes, 0 for No): 1

You can drive.

Nested if else in C Flowchart:

Nested if else in c language

कुछ जरुरी बातें (Important Points)

1. Condition हमेशा Brackets () में आते हैं: हमेशा if के बाद, शर्त को छोटे ब्रैकेट्स () के अंदर लिखना होता है, जैसे : if (age > 18).

2. कर्ली ब्रैकेट्स {} (Block): अगर हमें ‘if’ या ‘else’ के अंदर एक से ज़यादा Statement लिखनी है, तो उन्हें हमेशा कर्ली ब्रैकेट्स {} में बंद करें। इसे Block कहते हैं। अगर सिर्फ एक Statement है , तो {} लगाना जरुरी नहीं है, लेकिन अच्छी Programming के लिए, लगाना हमेशा बेहतर होता है।

// सही, क्योंकि सिर्फ एक statement है
if(x > 0)
printf("Positive Nmumber.");
// सही, एक से ज्यादा statement के लिए
if(x > 0)
{
  printf("Positive Number");
  x = x + 1;
}

3. सेमीकोलन ; (Semicolon ): ‘if’ Statement के तुरंत बाद कभी भी Semicolon ; न लगाएं। If(x > 0); लगाने पर C इसे एक खली Statement मान लेगा, और हमारा Code शायद गलत तरीके से काम करे।

if else In C Language में Operator का इस्तेमाल

‘if’ के साथ Condition में हम Relational Operator और Logical Operator का खूब इस्तेमाल करते हैं:

Relational Operator (तुलना करने वाले)

OperatorMeaning(मतलब)Example(उदाहरण)
==बराबर है (Equal to)a == b
!=बराबर नहीं है (Not equal to)a != b
>से बड़ा है (Greater than)a > b
<से छोटा है (Less than)a < b
>=से बड़ा या बराबर हैa >= b
<=से छोटा या बराबर हैa <= b

Logical Operator in C Programming

OperatorMeaning(मतलब)Example(उदाहरण)
&&और (AND) दोनों Condition सही हों(age > 18 && Marks < 50)
||Or Operator दोनों Condition में से कोई एक सही हो(math > 70 || Science > 70)
!नहीं (NOT) Condition को उल्टा कर देता है! isRaining(अगर बारिश नहीं हो रही है)

Quiz: if else and Nested if else in C

पहले इस Quiz का answer comment बॉक्स में खुद से देने की कोशिस करें, फिर answer देखें।

If Else & Nested If Else Quiz

Q1. Output क्या होगा?

#include <stdio.h>
int main() {
    int x = 10;
    if (x > 5)
        printf("A");
    else
        printf("B");
    return 0;
}
      
Answer: A
Reason: x > 5 true है, इसलिए if block execute होगा।

Q2. Nested if में output?

#include <stdio.h>
int main() {
    int age = 20, salary = 4000;
    if (age >= 18) {
        if (salary >= 5000)
            printf("Eligible");
        else
            printf("Salary low");
    } else {
        printf("Age low");
    }
    return 0;
}
      
Answer: Salary low
Reason: Age true है लेकिन salary condition false है, इसलिए inner else execute हुआ।

Q3. Output क्या होगा?

#include <stdio.h>
int main() {
    int a = 5;
    if (a == 5)
        if (a > 3)
            printf("X");
        else
            printf("Y");
    else
        printf("Z");
    return 0;
}
      
Answer: X
Reason: First if और second if दोनों true हैं।

Q4. क्या Output होगा?

#include <stdio.h>
int main() {
    int n = 7;
    if (n % 2 == 0)
        printf("Even");
    else
        printf("Odd");
    return 0;
}
      
Answer: Odd
Reason: 7 odd number है, इसलिए else block execute होगा।

Q5. Output बताइए

#include <stdio.h>
int main() {
    int age = 17, marks = 90;
    if (age >= 18)
        if (marks >= 50)
            printf("Pass");
        else
            printf("Fail");
    else
        printf("Underage");
    return 0;
}
      
Answer: Underage
Reason: Outer if false है, इसलिए inner if check नहीं होगा।

FAQ

Q1. C language में if statement का use क्यों किया जाता है?

Ans: C में if statement का use किसी condition के true या false होने पर code block को execute करने के लिए किया जाता है। यह decision making के लिए सबसे basic control statement है।

if (a > b) {
    printf("A is greater");
}
Q2. if-else statement कैसे काम करता है?

Ans: जब if की condition true होती है तो उसका block execute होता है, वरना else वाला block चलता है।

if (num % 2 == 0)
    printf("Even");
else
    printf("Odd");
Q3. Nested if क्या होता है?

Ans: जब एक if statement के अंदर दूसरा if statement होता है तो उसे Nested if कहा जाता है। इसका use तब किया जाता है जब multiple conditions check करनी हों।

if (age >= 18) {
    if (marks >= 50)
        printf("Eligible");
}
Q4. if-else ladder क्या होता है?

Ans: जब कई conditions को एक के बाद एक check किया जाता है, तो उसे if-else ladder कहा जाता है। यह multiple decisions लेने के लिए useful होता है।

if (marks >= 90)
    printf("Excellent");
else if (marks >= 75)
    printf("Good");
else if (marks >= 50)
    printf("Pass");
else
    printf("Fail");
Q5. क्या if के बिना else statement लिखा जा सकता है?

Ans: नहीं, else statement हमेशा किसी if के साथ जुड़ा होता है। अगर अकेले लिखा जाए तो syntax error मिलेगा।

// ❌ Wrong
else
    printf("Error");

अगर यह article आपको अच्छा लगा , तो इसे अपने दोस्तों और classmates के साथ share करें ताकि वो भी C programming सीख सकें।

Leave a Comment

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

Scroll to Top