इस ट्यूटोरियल में, हम उदाहरणों की मदद से निर्णय लेने के कार्यक्रम बनाने के लिए if … और स्टेटमेंट के बारे में जानेंगे।
कंप्यूटर प्रोग्रामिंग में, हम if
एक ब्लॉक कोड को चलाने के लिए स्टेटमेंट का उपयोग केवल तब करते हैं जब कोई निश्चित शर्त पूरी होती है।
उदाहरण के लिए, किसी छात्र द्वारा प्राप्त अंकों के आधार पर ग्रेड (ए, बी, सी) असाइन करना।
- यदि प्रतिशत 90 से ऊपर है , तो ग्रेड ए असाइन करें
- यदि प्रतिशत 75 से ऊपर है , तो ग्रेड बी असाइन करें
- यदि प्रतिशत 65 से ऊपर है , तो ग्रेड सी असाइन करें
if… else
C ++ में स्टेटमेंट्स के तीन रूप हैं ।
if
बयानif… else
बयानif… else if… else
बयान
C ++ अगर स्टेटमेंट
if
कथन का वाक्य विन्यास है:
if (condition) ( // body of if statement )
if
बयान का मूल्यांकन करता है condition
अंदर कोष्ठक ( )
।
- यदि इसका
condition
मूल्यांकन किया जाता हैtrue
, तो निकाय के कोडif
को निष्पादित किया जाता है। - अगर
condition
मूल्यांकन करता हैfalse
, तो शरीर के अंदर का कोडif
छोड़ दिया जाता है।
नोट: अंदर ( )
का कोड if
स्टेटमेंट है।

उदाहरण 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
कोष्ठक के अंदर।

यदि 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
andelse if
statements to the innerif
statement as required. - The inner
if
statement can also be inserted inside the outerelse
orelse 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 to0
.- If
true
, then the innerif… else
statement is executed. - If
false
, the code inside the outerelse
condition is executed, which prints "The number is 0 and neither even nor odd."
- If
- The inner
if… else
statement checks whether the input number is divisible by2
.- If
true
, then we print a statement saying that the number is even. - If
false
, we print that the number is odd.
- If
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 ++ प्रोग्राम