if-else Statement in C++ (C++ में if-else स्टेटमेंट)
The if-else statement is used to check a test condition and execute one of two alternative blocks of statements. If the test condition is true, the statements inside the if block are executed. Otherwise, the statements inside the else block are executed.
if-else स्टेटमेंट का प्रयोग किसी टेस्ट कंडीशन को जाँचने तथा दो विकल्पों में से एक विकल्प के स्टेटमेंट रन करने के लिए किया जाता है। यदि टेस्ट कंडीशन सत्य (True) होती है, तो if ब्लॉक के स्टेटमेंट रन किए जाते हैं। अन्यथा else ब्लॉक के स्टेटमेंट रन किए जाते हैं।
Syntax (सिंटैक्स)
1. Single Statement
if (condition)statement;elsestatement;
2. Multiple Statements (if-else Block)
if (condition){statements;...}else{statements;...}
Example (उदाहरण)
C++ program to find whether a given integer is even or odd.
एक C++ प्रोग्राम लिखिए जो यह पता करे कि दिया गया integer even number है या odd number।
Program:
#include <iostream>using namespace std;int main(){int n;cout << "Enter an Integer: ";cin >> n;if (n % 2 == 0){cout << n << " is Even Number" << endl;}else{cout << n << " is Odd Number" << endl;}return 0;}
Output 1:
Enter an Integer: 88 is Even Number
Output 2:
Enter an Integer: 77 is Odd Number
Explanation (व्याख्या)
The statement:
if (n % 2 == 0)checks whether the remainder obtained after dividing n by 2 is equal to zero.
If the remainder is
0, the number is even and theifblock is executed.Otherwise, the number is odd and the
elseblock is executed.
यह स्टेटमेंट जाँचता है कि n को 2 से भाग देने पर शेषफल शून्य है या नहीं।
यदि शेषफल
0है, तो संख्या even होती है औरifब्लॉक रन होता है।अन्यथा संख्या odd होती है और
elseब्लॉक रन होता है।
The else block is associated with the nearest preceding unmatched if statement. The else block does not require a separate condition. The if-else statement is a selection control structure in C++. It allows a program to choose and execute one of two alternative blocks based on a condition.
else ब्लॉक सबसे नज़दीकी ऐसे if स्टेटमेंट से जुड़ा होता है जिसके साथ पहले से else नहीं जुड़ा है। else के साथ अलग से condition नहीं दी जाती है। C++ में if-else एक selection control structure है। यह किसी condition के आधार पर दो वैकल्पिक blocks में से एक को execute करता है।
Comments
Post a Comment