if Statement in C Language with Examples

The if statement is a decision-making statement in C language. It is used to execute a statement or a block of statements only when a specified condition is true. If the condition evaluates to true (non-zero), the statements inside the if block are executed. Otherwise, the program control moves to the next statement after the if block.

C भाषा में if एक Decision-Making Statement है। इसका प्रयोग किसी निर्धारित Condition के सत्य होने पर एक या अधिक Statements को Execute करने के लिए किया जाता है। यदि Condition सत्य (Non-zero) होती है, तो if ब्लॉक के अंदर लिखे Statements Execute होते हैं। अन्यथा, प्रोग्राम कंट्रोल if ब्लॉक के बाद आने वाले अगले Statement पर चला जाता है।

Syntax of if Statement

The if statement can be written in two ways.

if Statement को दो तरीकों से लिखा जा सकता है।

1. Single Statement Syntax

if (condition)
statement;

2. Multiple Statements Syntax

if (condition)
{
statement1;
statement2;
statement3;
}

Here, the condition is an expression that is evaluated by the program. If it is true (non-zero), the body of the if statement is executed.

यहाँ Condition एक Expression होती है जिसका मूल्यांकन प्रोग्राम द्वारा किया जाता है। यदि Condition सत्य (Non-zero) होती है, तो if Statement के अंदर लिखे Statements Execute किए जाते हैं।

Flowchart of if Statement

The flowchart of an if statement shows that the condition is checked first. If the condition is true, the statements inside the if block are executed. If it is false, the program skips the block.

if Statement के Flowchart में सबसे पहले Condition की जाँच की जाती है। यदि Condition True होती है, तो if ब्लॉक के Statements Execute होते हैं। यदि Condition False होती है, तो प्रोग्राम इस ब्लॉक को छोड़कर आगे बढ़ जाता है।

Example 1: Simple if Statement

C Program to check whether a number is positive.

This program accepts a number from the user and displays a message only if the entered number is greater than zero.

यह प्रोग्राम यूजर से एक संख्या इनपुट करता है तथा केवल तभी संदेश प्रदर्शित करता है जब इनपुट की गई संख्या शून्य से बड़ी होती है।

Program:

#include <stdio.h>
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
{
printf("Number is positive.");
}
return 0;
}

Output 1:

Enter a number: 10
Number is positive.

Output 2:

Enter a number: -5

Explanation:

  1. The user enters a number.

  2. The condition num > 0 is checked.

  3. If the condition is true, the message "Number is positive" is displayed.

  4. If the condition is false, the if block is skipped.

व्याख्या:

  1. यूजर एक संख्या इनपुट करता है।

  2. num > 0 Condition की जाँच की जाती है।

  3. यदि Condition सत्य होती है, तो "Number is positive" संदेश प्रदर्शित होता है।

  4. यदि Condition असत्य होती है, तो if ब्लॉक Execute नहीं होता।

Example 2: Check Even Number Using if Statement

C Program to check whether a number is even.

A number is even if it is completely divisible by 2. The modulus operator % is used to find the remainder. If the remainder is zero, the number is even.

कोई संख्या Even होती है यदि वह 2 से पूर्णतः विभाजित हो जाती है। Remainder ज्ञात करने के लिए Modulus Operator % का प्रयोग किया जाता है। यदि शेषफल शून्य होता है, तो संख्या Even होती है।

Program:

#include <stdio.h>
int main(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
{
printf("Number is even.");
}
return 0;
}

Output:

Enter a number: 8
Number is even.

Example 3: Check Eligibility for Voting

A person is eligible to vote if their age is 18 years or above. The if statement can be used to check this condition.

यदि किसी व्यक्ति की आयु 18 वर्ष या उससे अधिक है, तो वह मतदान करने के लिए पात्र होता है। इस Condition की जाँच if Statement द्वारा की जा सकती है।

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.");
}
return 0;
}

Output:

Enter your age: 20
You are eligible to vote.

Important Points About if Statement

  1. The if statement is a decision-making statement in C.

  2. The condition is written inside parentheses ().

  3. If the condition is true (non-zero), the body of the if statement is executed.

  4. If the condition is false (zero), the body is skipped.

  5. Curly braces {} are optional when the body contains only one statement, but they are recommended for clarity.

  6. Multiple statements can be written inside an if block using curly braces.

  7. The if statement can use relational and logical expressions as conditions.

  8. if Statement C भाषा का एक Decision-Making Statement है।

  9. Condition को Parentheses () के अंदर लिखा जाता है।

  10. यदि Condition सत्य (Non-zero) होती है, तो if Statement का Body Execute होता है।

  11. यदि Condition असत्य (Zero) होती है, तो Body को Skip कर दिया जाता है।

  12. यदि if के अंदर केवल एक Statement है, तो Curly Braces {} लगाना अनिवार्य नहीं है, लेकिन स्पष्टता के लिए इनका प्रयोग करना अच्छा रहता है।

  13. Curly Braces का प्रयोग करके if ब्लॉक में एक से अधिक Statements लिखे जा सकते हैं।

  14. if Statement में Condition के रूप में Relational एवं Logical Expressions का प्रयोग किया जा सकता है।

The if statement is one of the basic decision-making statements in C language. It helps a program execute specific statements according to a given condition. It is also used as a building block for other decision-making statements such as if-else, nested if and else-if ladder.

if Statement C भाषा का एक मूलभूत Decision-Making Statement है। इसकी सहायता से प्रोग्राम किसी निर्धारित Condition के अनुसार Statements को Execute करता है। इसका उपयोग if-else, Nested if तथा else-if Ladder जैसे अन्य Decision-Making Statements को समझने के लिए भी किया जाता है।

Comments

Popular posts from this blog

C Language Topics in Hindi and English

Complete C Programming Syllabus | Advanced C Roadmap for Students

Top High-Paying Tech Skills to Learn in 2025