C ++ यदि ... और (उदाहरणों के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से निर्णय लेने के कार्यक्रम बनाने के लिए if … और स्टेटमेंट के बारे में जानेंगे।

कंप्यूटर प्रोग्रामिंग में, हम ifएक ब्लॉक कोड को चलाने के लिए स्टेटमेंट का उपयोग केवल तब करते हैं जब कोई निश्चित शर्त पूरी होती है।

उदाहरण के लिए, किसी छात्र द्वारा प्राप्त अंकों के आधार पर ग्रेड (ए, बी, सी) असाइन करना।

  • यदि प्रतिशत 90 से ऊपर है , तो ग्रेड असाइन करें
  • यदि प्रतिशत 75 से ऊपर है , तो ग्रेड बी असाइन करें
  • यदि प्रतिशत 65 से ऊपर है , तो ग्रेड सी असाइन करें

if… elseC ++ में स्टेटमेंट्स के तीन रूप हैं ।

  1. if बयान
  2. if… else बयान
  3. if… else if… else बयान

C ++ अगर स्टेटमेंट

ifकथन का वाक्य विन्यास है:

 if (condition) ( // body of if statement )

ifबयान का मूल्यांकन करता है conditionअंदर कोष्ठक ( )

  • यदि इसका conditionमूल्यांकन किया जाता है true, तो निकाय के कोड ifको निष्पादित किया जाता है।
  • अगर conditionमूल्यांकन करता है false, तो शरीर के अंदर का कोड ifछोड़ दिया जाता है।

नोट: अंदर ( )का कोड ifस्टेटमेंट है।

कथन यदि C ++ का कार्य करना है

उदाहरण 1: C ++ यदि कथन

 // Program to print positive number entered by the user // If the user enters a negative number, it is skipped #include using namespace std; int main() ( int number; cout <> number; // checks if the number is positive if (number> 0) ( cout << "You entered a positive integer: " << number << endl; ) cout << "This statement is always executed."; return 0; )

आउटपुट 1

 पूर्णांक दर्ज करें: 5 आपने एक सकारात्मक संख्या दर्ज की: 5 यह कथन हमेशा निष्पादित होता है।

जब उपयोगकर्ता प्रवेश करता है 5, तो स्थिति number> 0का मूल्यांकन किया जाता है trueऔर निकाय के अंदर विवरण ifनिष्पादित किया जाता है।

आउटपुट 2

 कोई संख्या दर्ज करें: -5 यह कथन हमेशा निष्पादित होता है।

जब उपयोगकर्ता प्रवेश करता है -5, तो स्थिति number> 0का मूल्यांकन किया जाता है falseऔर शरीर के अंदर के कथन को ifनिष्पादित नहीं किया जाता है।

C ++ अगर … और

ifबयान एक वैकल्पिक हो सकता है elseखंड। इसका सिंटैक्स है:

 if (condition) ( // block of code if condition is true ) else ( // block of code if condition is false )

if… elseबयान का मूल्यांकन करता है conditionकोष्ठक के अंदर।

C ++ का कार्य यदि… और

यदि conditionमूल्यांकन करता है true,

  • निकाय के कोड ifको निष्पादित किया जाता है
  • शरीर के अंदर का कोड elseनिष्पादन से छोड़ दिया जाता है

यदि conditionमूल्यांकन करता है false,

  • निकाय के कोड elseको निष्पादित किया जाता है
  • शरीर के अंदर का कोड ifनिष्पादन से छोड़ दिया जाता है

उदाहरण 2: C ++ यदि … और कथन

 // Program to check whether an integer is positive or negative // This program considers 0 as a positive number #include using namespace std; int main() ( int number; cout <> number; if (number>= 0) ( cout << "You entered a positive integer: " << number << endl; ) else ( cout << "You entered a negative integer: " << number << endl; ) cout << "This line is always printed."; return 0; )

आउटपुट 1

 पूर्णांक दर्ज करें: 4 आपने एक सकारात्मक पूर्णांक दर्ज किया: 4. यह रेखा हमेशा मुद्रित होती है।

उपरोक्त कार्यक्रम में, हमारी शर्त है number>= 0। यदि हम 0 से अधिक या बराबर संख्या दर्ज करते हैं, तो स्थिति का मूल्यांकन करता है true

यहाँ, हम 4 दर्ज करते हैं true। इसलिए, निकाय के अंदर का विवरण ifनिष्पादित किया जाता है।

आउटपुट 2

एक पूर्णांक दर्ज करें: -4 आपने एक नकारात्मक पूर्णांक दर्ज किया: -4। यह लाइन हमेशा प्रिंट की जाती है।

यहां, हम -4 में प्रवेश करते हैं। तो, शर्त है false। इसलिए, निकाय के अंदर का विवरण elseनिष्पादित किया जाता है।

C ++ यदि … और … यदि कथन है

if… elseबयान दो विकल्पों के बीच कोड का एक खंड पर अमल किया जाता है। हालांकि, अगर हमें दो से अधिक विकल्पों के बीच चयन करने की आवश्यकता है, तो हम if… else if… elseकथन का उपयोग करते हैं ।

if… else if… elseकथन का वाक्य विन्यास है:

 if (condition1) ( // code block 1 ) else if (condition2)( // code block 2 ) else ( // code block 3 )

यहाँ,

  • यदि condition1मूल्यांकन किया जाता है true, तो code block 1निष्पादित किया जाता है।
  • यदि condition1मूल्यांकन किया जाता है false, तो condition2मूल्यांकन किया जाता है।
  • यदि condition2है true, तो code block 2निष्पादित है।
  • यदि condition2है false, तो code block 3निष्पादित है।
कैसे अगर … और अगर … और स्टेटमेंट वर्क्स

नोट: एक से अधिक else ifकथन हो सकते हैं लेकिन केवल एक ifऔर elseकथन।

उदाहरण 3: C ++ यदि … और … यदि और

 // Program to check whether an integer is positive, negative or zero #include using namespace std; int main() ( int number; cout <> number; if (number> 0) ( cout << "You entered a positive integer: " << number << endl; ) else if (number < 0) ( cout << "You entered a negative integer: " << number << endl; ) else ( cout << "You entered 0." << endl; ) cout << "This line is always printed."; return 0; )

आउटपुट 1

 पूर्णांक दर्ज करें: 1 आपने एक सकारात्मक पूर्णांक दर्ज किया है: 1. यह पंक्ति हमेशा मुद्रित होती है।

आउटपुट 2

 Enter an integer: -2 You entered a negative integer: -2. This line is always printed.

Output 3

 Enter an integer: 0 You entered 0. This line is always printed.

In this program, we take a number from the user. We then use the if… else if… else ladder to check whether the number is positive, negative, or zero.

If the number is greater than 0, the code inside the if block is executed. If the number is less than 0, the code inside the else if block is executed. Otherwise, the code inside the else block is executed.

C++ Nested if… else

Sometimes, we need to use an if statement inside another if statement. This is known as nested if statement.

Think of it as multiple layers of if statements. There is a first, outer if statement, and inside it is another, inner if statement. Its syntax is:

 // outer if statement if (condition1) ( // statements // inner if statement if (condition2) ( // statements ) )

Notes:

  • We can add else and else if statements to the inner if statement as required.
  • The inner if statement can also be inserted inside the outer else or else if statements (if they exist).
  • We can nest multiple layers of if statements.

Example 4: C++ Nested if

 // C++ program to find if an integer is even or odd or neither (0) // using nested if statements #include using namespace std; int main() ( int num; cout <> num; // outer if condition if (num != 0) ( // inner if condition if ((num % 2) == 0) ( cout << "The number is even." << endl; ) // inner else condition else ( cout << "The number is odd." << endl; ) ) // outer else condition else ( cout << "The number is 0 and it is neither even nor odd." << endl; ) cout << "This line is always printed." << endl; )

Output 1

 Enter an integer: 34 The number is even. This line is always printed.

Output 2

 Enter an integer: 35 The number is odd. This line is always printed.

Output 3

 Enter an integer: 0 The number is 0 and it is neither even nor odd. This line is always printed.

In the above example,

  • We take an integer as an input from the user and store it in the variable num.
  • We then use an if… else statement to check whether num is not equal to 0.
    • If true, then the inner if… else statement is executed.
    • If false, the code inside the outer else condition is executed, which prints "The number is 0 and neither even nor odd."
  • The inner if… else statement checks whether the input number is divisible by 2.
    • If true, then we print a statement saying that the number is even.
    • If false, we print that the number is odd.

Notice that 0 is also divisible by 2, but it is actually not an even number. This is why we first make sure that the input number is not 0 in the outer if condition.

Note: As you can see, nested if… else makes your logic complicated. If possible, you should always try to avoid nested if… else.

Body of if… else With Only One Statement

If the body of if… else has only one statement, you can omit ( ) in the program. For example, you can replace

 int number = 5; if (number> 0) ( cout << "The number is positive." << endl; ) else ( cout << "The number is negative." << endl; )

with

 int number = 5; if (number> 0) cout << "The number is positive." << endl; else cout << "The number is negative." << endl;

The output of both programs will be the same.

Note: Although it's not necessary to use ( ) if the body of if… else has only one statement, using ( ) makes your code more readable.

More on Decision Making

कुछ स्थितियों में, एक टर्नरी ऑपरेटर एक if… elseबयान को बदल सकता है । अधिक जानने के लिए, C ++ Ternary Operator पर जाएं।

यदि हमें दिए गए परीक्षण की स्थिति के आधार पर एक से अधिक विकल्पों के बीच चयन करने की आवश्यकता है, तो switchकथन का उपयोग किया जा सकता है। अधिक जानने के लिए, C ++ स्विच पर जाएँ।

अधिक जानने के लिए इन उदाहरणों को देखें:

C ++ प्रोग्राम यह जाँचने के लिए कि संख्या सम है या विषम

C ++ प्रोग्राम यह जाँचने के लिए कि क्या एक अक्षर स्वर या व्यंजन है।

तीन नंबर में से सबसे बड़ी संख्या ज्ञात करने के लिए C ++ प्रोग्राम

दिलचस्प लेख...