if Statement in C++ (C++ में if स्टेटमेंट)

The if statement is used to evaluate a test condition. If the condition is true, the statement or statements inside the if block are executed. Otherwise, the program control moves to the next statement after the if block.

C++ में if स्टेटमेंट का प्रयोग किसी टेस्ट कंडीशन को जाँचने के लिए किया जाता है। यदि टेस्ट कंडीशन सत्य (True) होती है, तो if ब्लॉक के स्टेटमेंट रन किए जाते हैं। अन्यथा प्रोग्राम कंट्रोल if ब्लॉक के बाद आने वाले अगले स्टेटमेंट पर पहुँच जाता है।

Syntax (सिंटैक्स)

1. Single Statement

if (condition)
    statement;

2. Multiple Statements (if Block)

if (condition)
{
    statements;
    statements;
    ...
}

Here, the statements inside the block are executed only when the condition is true.

यहाँ ब्लॉक के अंदर दिए गए स्टेटमेंट तभी रन किए जाते हैं जब कंडीशन सत्य होती है।

Example:

C++ program to print First Division when the percentage of a student is 60% or more.

एक C++ प्रोग्राम लिखिए जो विद्यार्थी का प्रतिशत 60% या उससे अधिक होने पर First Division प्रदर्शित करे।

Program:

#include <iostream>
using namespace std;

int main()
{
    float per;

    cout << "Enter Percentage of student: ";
    cin >> per;

    if (per >= 60)
    {
        cout << "I Division " << per << "%" << endl;
    }

    return 0;
}

Output 1:

Enter Percentage of student: 75
I Division 75%

Output 2:

Enter Percentage of student: 45

In the second output, no division is displayed because the condition per >= 60 is false.

दूसरे आउटपुट में कोई division प्रदर्शित नहीं होती क्योंकि per >= 60 की कंडीशन false है।

Explanation (व्याख्या)

The statement:

if (per >= 60)

checks whether the value of per is greater than or equal to 60.

  • If the condition is true, the statement inside the if block is executed.

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

यह स्टेटमेंट जाँचता है कि per की वैल्यू 60 या उससे अधिक है या नहीं।

  • यदि कंडीशन सत्य है, तो if ब्लॉक के अंदर का स्टेटमेंट रन होगा।

  • यदि कंडीशन असत्य है, तो if ब्लॉक को छोड़ दिया जाएगा।

Note: 

The curly braces { } are optional when the if statement contains only one statement. However, using braces is recommended because it makes the code easier to read and helps prevent errors when additional statements are added.

यदि if स्टेटमेंट में केवल एक स्टेटमेंट है, तो { } का प्रयोग वैकल्पिक होता है। लेकिन एक से अधिक स्टेटमेंट होने पर braces का प्रयोग आवश्यक है। अच्छी प्रोग्रामिंग के लिए braces का प्रयोग करना उचित है।

The if statement is a selection control statement in C++. It allows a program to execute a statement or block of statements only when a specified condition is true.

C++ में if एक selection control statement है। इसका प्रयोग किसी निश्चित कंडीशन के सत्य होने पर स्टेटमेंट या स्टेटमेंट्स के ब्लॉक को रन करने के लिए किया जाता है।

Comments

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Programming Methodologies and Data structures using C++ Language with OOP's Concepts

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)