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

if else in c language का इस्तेमाल तब करते हैं, जब हमें किसी शर्त (condition) के आधार पर यह तय करना हो कि कौन सा code block चलाना है, तब हम c langauage में if else का इस्तेमाल करते हैं।
ये एक decision-making control statement है, जो program को अलग-अलग रास्तों पर ले जाने में मदद करता है।

अगर आपको नहीं पता :

  • if else in C language में क्या है?
  • कैसे काम करता है?
  • कहाँ use होता है?
  • Nested if else क्या है?

तो ये article आपके लिए है।

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

 if else in C language में एक control statement है, जो किसी condition (logical expression) को check करता है।

  • अगर condition true (सही) है → पहला block execute होगा।
  • अगर condition false(गलत) है → दूसरा block execute होगा।

Syntax of if else in C Lanaguage

if (condition) {

    // code block if condition is true

} else {

    // code block if condition is false

}

if else in C Language Flowchart:

if else in c language

 if else in C Language Real Life Example

मान लो बैंक में account खोलने के लिए salary चेक करनी है:

  • Salary > 5000 → “Eligible”
  • Salary < 5000 → “Not Eligible”

if else in c Example Code:

#include <stdio.h>

int main() {
    int salary;

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

    // Step 2: Condition check
    if (salary > 5000) { 
        printf("\nEligible for Account Opening");
    } else {
        printf("\nNot Eligible");
    }

    return 0;
}

Output Example:

Enter Your Salary: 6000

Eligible for Account Opening

सिर्फ if Statement का उपयोग

अगर आपको c programming में एक condition check करनी है और false होने पर कुछ नहीं करना है, तो सिर्फ if का use कर सकते हैं।

#include<stdio.h>
int main(){
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);

    if(age >= 18) {
        printf("You are eligible to vote.");
    }
    return 0;
}

Output:

Enter your age : 22

You are eligible to vote.

else Statement अकेले क्यों नहीं चलता?

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

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

 Output: Error

 Nested if else क्या है?

जब किसी if या else block के अंदर दूसरा if else होता है, तो उसे Nested if else कहते हैं।
ये तब useful होता है जब हमें multi-level decision लेने हों।

Nested if else in C Language Example Code :

इस Example Code में  salary के साथ age भी चेक करेंंगे :

#include <stdio.h>

int main() {
    int age, salary;

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

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

    if (age >= 18) {
        if (salary >= 5000) {
            printf("You are eligible for the Open Account.");
        } else {
            printf("Your salary is below the required amount.");
        }
    } else {
        printf("Your age is below 18. Not eligible.");
    }

    return 0;
}

Output:

Enter your age : 20

Enter your salary : 10000

You are eligible for the Open Account.

Nested if else in C Flowchart:

Nested if else in c language

Common Mistakes if else in C

  1. Semicolon लगाना

if (age >= 18); // Wrong

  1. Condition में assignment करना

if (x = 5) // Wrong

3. Deep nesting without brackets → Readability कम हो जाती है।

Quiz: if else and Nested if else in C

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. if else और nested if else में क्या फर्क है?

if else → एक condition check करता है।
nested if else → एक condition के अंदर दूसरी condition check करता है।

Q2. Nested if else कब use करें?

जब multi-level decision लेना हो।

Q3. Nested if else की जगह क्या use कर सकते हैं?

 if-else if ladder या switch case।

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

Leave a Comment

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

Scroll to Top