if-else Statement in C Language with Examples
The if-else statement is a decision-making statement in C language. It is used to execute one block of statements when a given condition is true and another block of statements when the condition is false. If the test condition evaluates to true (non-zero), the statements inside the if block are executed. Otherwise, the statements inside the else block are executed.
C भाषा में if-else एक Decision-Making Statement है। इसका प्रयोग किसी Condition के सत्य या असत्य होने के आधार पर अलग-अलग Statements को Execute करने के लिए किया जाता है। यदि Test Condition सत्य (Non-zero) होती है, तो if ब्लॉक के Statements Execute किए जाते हैं। अन्यथा else ब्लॉक के Statements Execute किए जाते हैं।
Syntax of if-else Statement
The if-else statement can be written in two ways.
if-else Statement को दो तरीकों से लिखा जा सकता है।
1. Single Statement Syntax
if (condition)statement1;elsestatement2;
2. Multiple Statements Syntax
if (condition){statement1;statement2;}else{statement1;statement2;}
Here, the condition is evaluated first. If it is true, the if block is executed; otherwise, the else block is executed. Only one of these two blocks is executed.
यहाँ सबसे पहले Condition का मूल्यांकन किया जाता है। यदि Condition सत्य होती है, तो if ब्लॉक Execute होता है, अन्यथा else ब्लॉक Execute होता है। इन दोनों ब्लॉक्स में से केवल एक ही Execute होता है।
Flowchart of if-else Statement
The flowchart of an if-else statement shows that the condition is checked first. If the condition is true, the control moves to the if block. If it is false, the control moves to the else block. After executing either block, the program continues with the next statement.
if-else Statement के Flowchart में सबसे पहले Condition की जाँच की जाती है। यदि Condition True होती है, तो Control if ब्लॉक में जाता है। यदि Condition False होती है, तो Control else ब्लॉक में जाता है। किसी एक ब्लॉक को Execute करने के बाद प्रोग्राम अगले Statement पर चला जाता है।
Example 1: Check Whether a Number is Even or Odd
A number is even if it is completely divisible by 2. Otherwise, it is odd. The modulus operator % is used to calculate the remainder.
यदि कोई संख्या 2 से पूर्णतः विभाजित हो जाती है, तो वह Even Number होती है। अन्यथा वह Odd Number होती है। Remainder ज्ञात करने के लिए Modulus Operator % का प्रयोग किया जाता है।
Program:
#include <stdio.h>int main(void){int num;printf("Enter a number: ");scanf("%d", &num);if (num % 2 == 0){printf("Number is even.");}else{printf("Number is odd.");}return 0;}
Output 1:
Enter a number: 10Number is even.
Output 2:
Enter a number: 7Number is odd.
Explanation:
The user enters an integer.
The condition
num % 2 == 0checks whether the number is divisible by 2.If the condition is true, the
ifblock displays "Number is even."Otherwise, the
elseblock displays "Number is odd."
व्याख्या:
यूजर एक Integer इनपुट करता है।
num % 2 == 0Condition की सहायता से जाँचा जाता है कि संख्या 2 से विभाजित होती है या नहीं।यदि Condition सत्य होती है, तो
ifब्लॉक "Number is even." प्रदर्शित करता है।अन्यथा
elseब्लॉक "Number is odd." प्रदर्शित करता है।
Example 2: Check Whether a Number is Positive or Negative
A number greater than or equal to zero is considered non-negative, while a number less than zero is negative. In this example, we use if-else to check whether a number is negative or non-negative.
शून्य से बड़ी या शून्य के बराबर संख्या Non-negative होती है, जबकि शून्य से छोटी संख्या Negative होती है। इस उदाहरण में if-else का प्रयोग करके संख्या की जाँच की गई है।
Program:
#include <stdio.h>int main(void){int num;printf("Enter a number: ");scanf("%d", &num);if (num >= 0){printf("Number is non-negative.");}else{printf("Number is negative.");}return 0;}
Output 1:
Enter a number: 15Number is non-negative.
Output 2:
Enter a number: -8Number is negative.
Note: Zero is neither positive nor negative. This example groups zero with non-negative numbers.
नोट: शून्य न तो Positive होता है और न ही Negative। इस उदाहरण में शून्य को Non-negative संख्याओं के साथ रखा गया है।
Example 3: Check Voting Eligibility
A person is eligible to vote if their age is 18 years or above. Otherwise, the person is not eligible to vote.
यदि किसी व्यक्ति की आयु 18 वर्ष या उससे अधिक है, तो वह मतदान करने के लिए पात्र होता है। अन्यथा वह मतदान करने के लिए पात्र नहीं होता है।
Program:
#include <stdio.h>int main(void){int age;printf("Enter your age: ");scanf("%d", &age);if (age >= 18){printf("You are eligible to vote.");}else{printf("You are not eligible to vote.");}return 0;}
Output 1:
Enter your age: 25You are eligible to vote.
Output 2:
Enter your age: 16You are not eligible to vote.
Difference Between if and if-else Statements
| if Statement | if-else Statement |
|---|---|
| Executes a block when the condition is true. | Executes one block when true and another when false. |
| If the condition is false, the block is skipped. | If the condition is false, the else block executes. |
It uses only the if keyword. | It uses both if and else keywords. |
हिन्दी में अंतर:
| if Statement | if-else Statement |
|---|---|
| Condition सत्य होने पर ब्लॉक Execute होता है। | Condition सत्य होने पर if और असत्य होने पर else ब्लॉक Execute होता है। |
| Condition असत्य होने पर ब्लॉक Skip हो जाता है। | Condition असत्य होने पर else ब्लॉक Execute होता है। |
इसमें केवल if Keyword का प्रयोग होता है। | इसमें if और else दोनों Keywords का प्रयोग होता है। |
Important Points About if-else Statement
The
if-elsestatement is a decision-making statement.The condition is written inside parentheses
().If the condition is true (non-zero), the
ifblock executes.If the condition is false (zero), the
elseblock executes.Only one of the two blocks is executed.
Curly braces
{}are optional for a single statement but recommended for clarity.The
elsekeyword must be associated with a correspondingifstatement.A semicolon should not be placed immediately after the
ifcondition when writing a block.if-elseStatement एक Decision-Making Statement है।Condition को Parentheses
()के अंदर लिखा जाता है।Condition सत्य (Non-zero) होने पर
ifब्लॉक Execute होता है।Condition असत्य (Zero) होने पर
elseब्लॉक Execute होता है।दोनों ब्लॉक्स में से केवल एक ही Execute होता है।
Single Statement के लिए Curly Braces
{}वैकल्पिक हैं, लेकिन स्पष्टता के लिए इनका प्रयोग करना अच्छा रहता है।elseKeyword हमेशा किसी संबंधितifStatement से जुड़ा होता है।Block लिखते समय
ifकी Condition के तुरंत बाद Semicolon नहीं लगाना चाहिए।
The if-else statement is used to make decisions in C programs. It allows a program to choose between two alternative blocks of statements depending on whether a condition is true or false.
if-else Statement का प्रयोग C प्रोग्राम में निर्णय लेने के लिए किया जाता है। यह किसी Condition के सत्य या असत्य होने के आधार पर दो अलग-अलग Statements के समूहों में से एक को Execute करने की सुविधा प्रदान करता है।
Comments
Post a Comment