Header Ads

Conditions & If Statements in C Language

 

Conditions & If Statements in C Language


Conditions and if statements are fundamental concepts in computer programming that allow you to create decision-making logic in your code. They help your program make choices and perform different actions based on specific conditions or criteria. Here's a basic description of conditions and if statements:

1. Conditions: 

   Conditions are expressions that evaluate to either true or false. They are used to represent a certain state or situation in your program. Conditions can involve variables, values, or expressions and are crucial for making decisions in your code.


2. If Statements:

   An "if statement" is a control structure in programming that allows you to execute a block of code only if a specified condition is true. It enables you to create branches in your code, making it dynamic and responsive to different situations.

   Here's the basic syntax of an if statement in most programming languages:

   if condition:

       # Code to be executed if the condition is true

   - `condition` is the expression that evaluates to either true or false.

   - The indented block of code under the if statement is executed if the condition is true.

   - If the condition is false, the code inside the if block is skipped, and the program continues with the next statements.


3. Else Statements:

   Sometimes, you may want to provide an alternative action when the condition in an if statement is false. This is where the "else statement" comes into play. Here's the basic syntax:

   if condition:

       # Code to be executed if the condition is true

   else:

       # Code to be executed if the condition is false

   - If the condition is true, the code in the if block is executed.

   - If the condition is false, the code in the else block is executed.


4. Else If (elif) Statements:

   In more complex decision-making scenarios, you may need to evaluate multiple conditions sequentially. The "elif" statement (short for "else if") allows you to check additional conditions if the previous ones are false. Here's the syntax:

   if condition1:

       # Code to be executed if condition1 is true

   elif condition2:

       # Code to be executed if condition2 is true

   else:

       # Code to be executed if all conditions are false

   - Conditions are evaluated in order, and the code block under the first true condition is executed.

   - If none of the conditions is true, the code in the else block is executed.

Conditions and if statements provide the foundation for creating flexible and responsive programs, enabling them to adapt to different situations and make decisions based on specific criteria.


हिंदी:

Conditions और if स्टेटमेंट्स कंप्यूटर प्रोग्रामिंग में मौलिक अवधारणाओं हैं जो आपको अपने कोड में निर्णय निर्माण करने की अनुमति देते हैं। ये आपके प्रोग्राम को विभिन्न निर्णय लेने और विशेष स्थितियों या मानकों के आधार पर विभिन्न क्रियाएँ करने में मदद करते हैं। यहां शर्तों और if स्टेटमेंट्स का एक मौलिक विवरण है:

1. शर्तें: शर्तें व्यक्तियों को सही या गलत में मूल्यांकित करने वाले अभिव्यक्तियाँ हैं। इन्हें अपने प्रोग्राम में किसी विशिष्ट स्थिति या परिस्थिति को प्रस्तुत करने के लिए उपयोग किया जाता है। शर्तें चर विशेषक, मूल्यों या अभिव्यक्तियों को शामिल कर सकती हैं और आपके कोड में निर्णय बनाने के लिए महत्वपूर्ण हैं।

2. If स्टेटमेंट: "If स्टेटमेंट" प्रोग्रामिंग में एक नियंत्रण संरचना है जो आपको केवल तब कोड का एक खंड को निष्पिष्ट शर्त सही होने पर क्रियान्वित करने की अनुमति देता है। इसके जरिए आप अपने कोड में शाखाएँ बना सकते हैं, जिससे आपका कोड विभिन्न परिस्थितियों के लिए डायनेमिक और प्रतिक्रियाशील होता है। यहां सबसे सामान्य प्रोग्रामिंग भाषाओं में if स्टेटमेंट का मूल संधि दिया गया है:

if शर्त: # अगर शर्त सही है तो क्रियान्वित करने के लिए कोड
- `शर्त` वह अभिव्यक्ति है जो सही या गलत में मूल्यांकित होती है। - इफ स्टेटमेंट के तहत कोड ब्लॉक केवल तब क्रियान्वित होता है जब शर्त सही होती है। - अगर शर्त गलत है, तो इफ ब्लॉक के अंतर्गत का कोड छोड़ दिया जाता है, और प्रोग्राम अगले स्टेटमेंट्स के साथ जारी रहता है।
3. Else स्टेटमेंट: कभी-कभी, जब एक if स्टेटमेंट में निर्दिष्ट शर्त गलत होती है, तो आपको गलत स्थिति में एक वैकल्पिक क्रिया प्रदान करना चाहिए। यहां "else स्टेटमेंट" का उपयोग आता है। निम्नलिखित है बुनायदी बोलचाल:
if शर्त: # अगर शर्त सही है तो क्रियान्वित करने के लिए कोड else: # अगर शर्त गलत है तो क्रियान्वित करने के लिए कोड
- अगर शर्त सही होती है, तो इफ ब्लॉक में कोड क्रियान्वित होता है। - अगर शर्त गलत होती है, तो इल्स ब्लॉक के अंतर्गत कोड क्रियान्वित होता है।
4. इल्स इफ (elif) स्टेटमेंट्स: अधिक जटिल निर्णय निर्माण विचारों में, आपको पूर्व शर्तों के गलत होने पर अतिरिक्त शर्तों की जांच करने की आवश्यकता हो सकती है। "इल्स इफ" स्टेटमेंट (छोटा रूप में "अन्यथा अगर") आपको पूर्व शर्तों के गलत होने पर अतिरिक्त शर्तों की जांच करने की अनुमति देता है। यहां यह सिंटैक्स है:
if शर्त1: # अगर शर्त1 सही है तो क्रियान्वित करने के लिए कोड elif शर्त2: # अगर शर्त2 सही है तो क्रियान्वित करने के लिए कोड else: # अगर सभी शर्तें गलत हैं तो क्रियान्वित करने के लिए कोड
- शर्तें क्रम में मूल्यांकित होती हैं, और पहली सही शर्त के अंतर्गत का कोड क्रियान्वित होता है। - यदि कोई शर्त सही न हो, तो इल्स ब्लॉक के अंतर्गत का कोड क्रियान्वित होता है। शर्तें और if स्टेटमेंट्स प्राकृतिक और प्रतिक्रियाशील प्रोग्राम बनाने के लिए मूल आधार प्रदान करते हैं, जिससे वे विभिन्न परिस्थितियों को अनुकूलित कर सकते हैं और विशिष्ट मानकों के आधार पर निर्णय ले सकते हैं।

No comments

Powered by Blogger.