Structure of if-else Statement in Hindi
/ BCA / Programming with C and CPP
if-else Statement Full Explanation in Hindi
Structure of if-else Statement in Hindi
जब हम C या C++ जैसी programming language में कोई ऐसा program बनाना चाहते हैं जिसमें कुछ conditions के आधार पर decision लेना होता है, तब हम if-else statement का उपयोग करते हैं। यह decision making के लिए सबसे basic और important control structure है। चलिए इसे एक शिक्षक की तरह step by step समझते हैं।
What is if-else Statement?
if-else statement एक ऐसी structure है जो हमें यह decide करने की अनुमति देती है कि कोई particular block of code चलेगा या नहीं, condition के true या false होने पर। अगर condition true है तो if block execute होगा, और अगर false है तो else block execute होगा।
Syntax of if-else Statement
if (condition) {
// यह block तब चलेगा जब condition true होगी
} else {
// यह block तब चलेगा जब condition false होगी
}
Example:
int number = 5;
if (number > 0) {
printf("Number is positive");
} else {
printf("Number is not positive");
}
Structure को समझाने के लिए Flowchart
नीचे एक सरल flowchart है जो if-else की working को दर्शाता है:
| Step | Explanation |
|---|---|
| 1 | Condition check होती है |
| 2 | अगर condition true है, तो if block चलेगा |
| 3 | अगर condition false है, तो else block चलेगा |
Difference between if and if-else Statement in Hindi
Main Difference
- if statement केवल एक condition को check करता है और true होने पर ही code execute करता है।
- if-else statement condition के true या false होने पर दोनों स्थिति में code execute करता है (either if block or else block)।
Syntax Comparison
if Statement:
if (condition) {
// execute if true
}
if-else Statement:
if (condition) {
// execute if true
} else {
// execute if false
}
Use Case Difference
| Aspect | if Statement | if-else Statement |
|---|---|---|
| Condition | सिर्फ true होने पर code चलेगा | true या false दोनों स्थिति में code चलेगा |
| Flexibility | कम | ज्यादा |
| Usage | सिर्फ एक तरफ़ा निर्णय के लिए | दो विकल्पों में निर्णय के लिए |
Practical Examples of if-else Statement in Hindi
Example 1: Check Even or Odd Number
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("Even number");
} else {
printf("Odd number");
}
Example 2: Check Eligibility to Vote
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("Eligible to vote");
} else {
printf("Not eligible to vote");
}
Example 3: Positive, Negative or Zero
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("Positive");
} else if (number < 0) {
printf("Negative");
} else {
printf("Zero");
}
Example 4: Check Pass or Fail
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 33) {
printf("Pass");
} else {
printf("Fail");
}
if-else Statement for User Input Programs in Hindi
जब हम user से कोई value input लेते हैं और उस value के आधार पर निर्णय लेते हैं, तब if-else बहुत उपयोगी होता है। नीचे user input पर आधारित कुछ programs दिए गए हैं:
Program 1: Check Greater Number
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
if (a > b) {
printf("a is greater");
} else {
printf("b is greater or equal");
}
Program 2: Grade Based on Marks
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90) {
printf("Grade A");
} else if (marks >= 75) {
printf("Grade B");
} else if (marks >= 50) {
printf("Grade C");
} else {
printf("Fail");
}
Program 3: Check Leap Year
int year;
printf("Enter year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("Leap Year");
} else {
printf("Not a Leap Year");
}
Program 4: Simple Login Verification
char username[20];
char password[20];
printf("Enter username: ");
scanf("%s", username);
printf("Enter password: ");
scanf("%s", password);
if (strcmp(username, "admin") == 0 && strcmp(password, "1234") == 0) {
printf("Login Successful");
} else {
printf("Invalid Credentials");
}
FAQs
if (condition) {
// code if true
} else {
// code if false
}