Conditional Statements

How to make a C++ program choose between different paths.

Conditional Statements

Conditional Statements का उपयोग C++ program में किसी condition (शर्त) के आधार पर decision (निर्णय) लेने के लिए किया जाता है। जब program को किसी condition के true या false होने के आधार पर अलग-अलग statements execute करने हों, तब conditional statements का उपयोग किया जाता है।

याद रखें: Conditional Statement → Condition check करना → उसके अनुसार decision लेना

What is a Conditional Statement?

Conditional statement एक control statement है जो किसी condition को check करता है और condition के result के आधार पर program के execution का path निर्धारित करता है।

उदाहरण के लिए, यदि किसी student के marks 33 या उससे अधिक हैं, तो उसे Pass और अन्यथा Fail कहा जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 65;

    if (marks >= 33)
    {
        cout << "Pass";
    }

    return 0;
}
Output:
Pass

Types of Conditional Statements

C++ में मुख्य रूप से निम्नलिखित conditional statements का उपयोग किया जाता है:

  1. if Statement
  2. if-else Statement
  3. else-if Ladder
  4. Nested if Statement
  5. switch Statement
  6. Conditional Operator
Statement मुख्य उपयोग
if एक condition को check करना
if-else दो alternatives में से एक चुनना
else-if Multiple conditions को check करना
Nested if एक if के अंदर दूसरा if लगाना
switch Multiple fixed choices में से एक चुनना
? : Short conditional decision लेना

1. if Statement

if statement का उपयोग तब किया जाता है जब किसी condition के true होने पर ही कोई statement या statements execute करने हों। यदि condition false है, तो if block execute नहीं होता।

Syntax of if Statement

if (condition)
{
    // statements
}

Example of if Statement

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    if (age >= 18)
    {
        cout << "Eligible to vote";
    }

    return 0;
}
Output:
Eligible to vote

यहाँ age >= 18 condition true है, इसलिए if block execute होता है।

if Statement with False Condition

यदि if statement की condition false हो जाती है, तो उसका block execute नहीं होता और program आगे की statement पर चला जाता है।

#include <iostream>
using namespace std;

int main()
{
    int age = 15;

    if (age >= 18)
    {
        cout << "Eligible to vote";
    }

    cout << "Program continues";

    return 0;
}
Output:
Program continues

2. if-else Statement

if-else statement का उपयोग तब किया जाता है जब किसी condition के आधार पर दो possible alternatives में से एक को execute करना हो।

यदि condition true है, तो if block execute होता है। यदि condition false है, तो else block execute होता है।

Syntax of if-else

if (condition)
{
    // statements if condition is true
}
else
{
    // statements if condition is false
}

Example of if-else

#include <iostream>
using namespace std;

int main()
{
    int marks = 28;

    if (marks >= 33)
    {
        cout << "Pass";
    }
    else
    {
        cout << "Fail";
    }

    return 0;
}
Output:
Fail

यहाँ marks >= 33 false है, इसलिए else block execute होता है।

Example: Even or Odd

if-else statement का उपयोग किसी number के even या odd होने की जाँच के लिए भी किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int number = 24;

    if (number % 2 == 0)
    {
        cout << "Even";
    }
    else
    {
        cout << "Odd";
    }

    return 0;
}
Output:
Even

3. else-if Ladder

जब किसी program में दो से अधिक conditions को क्रम से check करना हो, तब else-if ladder का उपयोग किया जाता है।

इसमें conditions को ऊपर से नीचे की ओर check किया जाता है। जिस condition का result पहली बार true होता है, उसका corresponding block execute होता है और बाकी conditions को skip कर दिया जाता है।

Syntax of else-if Ladder

if (condition1)
{
    // statements
}
else if (condition2)
{
    // statements
}
else if (condition3)
{
    // statements
}
else
{
    // statements
}

Example of else-if Ladder

#include <iostream>
using namespace std;

int main()
{
    int marks = 75;

    if (marks >= 80)
    {
        cout << "Grade A";
    }
    else if (marks >= 60)
    {
        cout << "Grade B";
    }
    else if (marks >= 33)
    {
        cout << "Grade C";
    }
    else
    {
        cout << "Fail";
    }

    return 0;
}
Output:
Grade B

यहाँ पहली condition false है, दूसरी condition marks >= 60 true है, इसलिए Grade B print होता है।

Example: Find the Largest of Three Numbers

Multiple conditions का उपयोग करके तीन numbers में से largest number भी find किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int a = 25;
    int b = 40;
    int c = 30;

    if (a >= b && a >= c)
    {
        cout << a;
    }
    else if (b >= a && b >= c)
    {
        cout << b;
    }
    else
    {
        cout << c;
    }

    return 0;
}
Output:
40

4. Nested if Statement

जब एक if statement के अंदर दूसरा if statement लिखा जाता है, तो उसे Nested if कहा जाता है। इसका उपयोग तब किया जाता है जब दूसरी condition को पहली condition के true होने के बाद check करना हो।

Syntax of Nested if

if (condition1)
{
    if (condition2)
    {
        // statements
    }
}

Example of Nested if

#include <iostream>
using namespace std;

int main()
{
    int age = 25;
    bool citizen = true;

    if (age >= 18)
    {
        if (citizen)
        {
            cout << "Eligible to vote";
        }
    }

    return 0;
}
Output:
Eligible to vote

यहाँ पहले age की condition check होती है। यदि age 18 या उससे अधिक है, तभी दूसरी condition check की जाती है।

5. switch Statement

switch statement का उपयोग किसी expression की value के आधार पर multiple fixed choices में से एक block को execute करने के लिए किया जाता है।

Syntax of switch

switch (expression)
{
    case value1:
        // statements
        break;

    case value2:
        // statements
        break;

    default:
        // statements
}

switch में case labels अलग-अलग possible values को represent करते हैं। break statement matching case के बाद switch से बाहर निकलने के लिए उपयोग किया जाता है। default तब execute होता है जब कोई case match नहीं करता।

Example of switch Statement

#include <iostream>
using namespace std;

int main()
{
    int day = 2;

    switch (day)
    {
        case 1:
            cout << "Monday";
            break;

        case 2:
            cout << "Tuesday";
            break;

        case 3:
            cout << "Wednesday";
            break;

        default:
            cout << "Invalid day";
    }

    return 0;
}
Output:
Tuesday

Role of break in switch

break statement का उपयोग matching case के statements execute होने के बाद switch statement से बाहर निकलने के लिए किया जाता है। यदि break नहीं लगाया जाए, तो control अगले case में भी जा सकता है। इसे fall-through कहा जाता है।

Example of break

#include <iostream>
using namespace std;

int main()
{
    int number = 1;

    switch (number)
    {
        case 1:
            cout << "One";
            break;

        case 2:
            cout << "Two";
            break;

        default:
            cout << "Other";
    }

    return 0;
}
Output:
One

default in switch

default case तब execute होता है जब switch expression की value किसी भी available case से match नहीं करती।

#include <iostream>
using namespace std;

int main()
{
    int choice = 5;

    switch (choice)
    {
        case 1:
            cout << "Add";
            break;

        case 2:
            cout << "Delete";
            break;

        default:
            cout << "Invalid choice";
    }

    return 0;
}
Output:
Invalid choice

if-else vs switch

if-else switch
Complex conditions को check कर सकता है। एक expression की अलग-अलग fixed values को check करने के लिए उपयोगी है।
Relational और logical operators का उपयोग आसानी से किया जा सकता है। मुख्यतः case values के साथ selection किया जाता है।
Ranges जैसे marks >= 80 check किए जा सकते हैं। Fixed choices जैसे 1, 2, 3 के लिए अधिक suitable है।

Conditional Operator

Conditional operator ? : का उपयोग simple decision को short form में लिखने के लिए किया जाता है। यह एक ternary operator है क्योंकि इसमें तीन operands होते हैं।

#include <iostream>
using namespace std;

int main()
{
    int age = 20;

    string result = (age >= 18) ? "Adult" : "Minor";

    cout << result;

    return 0;
}
Output:
Adult

Multiple Conditions using Logical Operators

Conditional statements में multiple conditions को combine करने के लिए logical operators का उपयोग किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 75;

    if (marks >= 33 && marks <= 100)
    {
        cout << "Valid passing marks";
    }

    return 0;
}
Output:
Valid passing marks

Nested if vs else-if Ladder

Nested if else-if Ladder
एक if के अंदर दूसरा if होता है। Multiple conditions को क्रम से check किया जाता है।
Dependent conditions के लिए उपयोगी है। Multiple alternatives में से एक चुनने के लिए उपयोगी है।
Example: पहले age check, फिर citizenship check। Example: marks के आधार पर grade निर्धारित करना।

Example: Student Grade

Conditional statements का उपयोग student के marks के आधार पर grade निर्धारित करने के लिए किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int marks = 86;

    if (marks >= 90)
    {
        cout << "Grade A+";
    }
    else if (marks >= 80)
    {
        cout << "Grade A";
    }
    else if (marks >= 70)
    {
        cout << "Grade B";
    }
    else if (marks >= 60)
    {
        cout << "Grade C";
    }
    else if (marks >= 33)
    {
        cout << "Grade D";
    }
    else
    {
        cout << "Fail";
    }

    return 0;
}
Output:
Grade A

Example: Simple Calculator using switch

switch statement का उपयोग menu-driven program या simple calculator जैसे programs में किया जा सकता है।

#include <iostream>
using namespace std;

int main()
{
    int a = 20;
    int b = 5;
    char op = '+';

    switch (op)
    {
        case '+':
            cout << a + b;
            break;

        case '-':
            cout << a - b;
            break;

        case '*':
            cout << a * b;
            break;

        case '/':
            cout << a / b;
            break;

        default:
            cout << "Invalid operator";
    }

    return 0;
}
Output:
25

Important Points

  • Conditional statements का उपयोग decision making के लिए किया जाता है।
  • if statement condition के true होने पर statements execute करता है।
  • if-else दो alternatives में से एक चुनता है।
  • else-if ladder multiple conditions को क्रम से check करता है।
  • एक if के अंदर दूसरा if होने पर उसे Nested if कहते हैं।
  • switch multiple fixed choices में से selection के लिए उपयोगी है।
  • case switch में possible values को represent करता है।
  • break matching case के बाद switch से बाहर निकलने में सहायता करता है।
  • default तब execute होता है जब कोई case match नहीं करता।
  • ? : conditional या ternary operator है।
  • Logical operators की सहायता से multiple conditions को combine किया जा सकता है।

Board Focus

Exam के लिए याद रखें:
if → एक condition check करना
if-else → दो alternatives में decision लेना
else-if → Multiple conditions check करना
Nested if → if के अंदर if
switch → Fixed choices में selection
case → switch की possible value
break → switch से बाहर निकलना
default → कोई case match न होने पर execute
? : → Conditional/Ternary operator

Board Important Questions

Very Short Answer Questions

Q1. Conditional statement क्या है?

Answer: वह control statement जो किसी condition के आधार पर program में decision लेने के लिए उपयोग होता है, conditional statement कहलाता है।

Q2. if statement का क्या उपयोग है?

Answer: if statement का उपयोग condition के true होने पर किसी statement या block को execute करने के लिए किया जाता है।

Q3. if-else statement क्या है?

Answer: if-else statement condition के आधार पर दो alternatives में से किसी एक को execute करता है।

Q4. else-if ladder क्या है?

Answer: Multiple conditions को क्रम से check करने के लिए उपयोग किए जाने वाले if और else-if statements के sequence को else-if ladder कहते हैं।

Q5. Nested if क्या है?

Answer: एक if statement के अंदर दूसरे if statement का उपयोग Nested if कहलाता है।

Q6. switch statement का क्या उपयोग है?

Answer: switch statement का उपयोग किसी expression की value के आधार पर multiple fixed choices में से एक को select करने के लिए किया जाता है।

Q7. switch में break का क्या उपयोग है?

Answer: break statement matching case के statements execute होने के बाद switch statement से बाहर निकलने के लिए उपयोग किया जाता है।

Short Answer Questions

Q8. if और if-else statement में अंतर बताइए।

if if-else
केवल condition true होने पर block execute करता है। True और false दोनों situations के लिए अलग blocks हो सकते हैं।
False condition पर कोई alternative block नहीं होता। False condition पर else block execute होता है।

Q9. Nested if और else-if ladder में अंतर बताइए।

Answer: Nested if में एक if statement दूसरे if statement के अंदर होता है और dependent conditions को check करने के लिए उपयोगी है। else-if ladder में multiple independent alternatives को क्रम से check किया जाता है और पहली true condition का block execute होता है।

Q10. switch statement में case और default का क्या महत्व है?

Answer: case किसी specific value के लिए statements का block निर्धारित करता है। यदि switch expression किसी case से match नहीं करता, तो default block execute होता है।

Q11. if-else और switch में अंतर बताइए।

if-else switch
Complex conditions और ranges को check कर सकता है। Fixed values या choices के selection के लिए अधिक उपयुक्त है।
Logical और relational expressions का उपयोग किया जा सकता है। case labels के माध्यम से values match की जाती हैं।

Long Answer Questions

Q12. C++ में विभिन्न conditional statements को समझाइए।

Answer: C++ में decision making के लिए if, if-else, else-if ladder, nested if और switch statements का उपयोग किया जाता है। if एक condition को check करता है। if-else दो alternatives में से एक को select करता है। else-if ladder multiple conditions को क्रम से check करता है। Nested if में एक if के अंदर दूसरा if होता है। switch किसी expression की fixed values के आधार पर multiple choices में से एक block को execute करता है।

Q13. निम्न program का output बताइए:

#include <iostream>
using namespace std;

int main()
{
    int marks = 72;

    if (marks >= 80)
    {
        cout << "A";
    }
    else if (marks >= 60)
    {
        cout << "B";
    }
    else
    {
        cout << "C";
    }

    return 0;
}
Output:
B

Q14. निम्न switch program का output बताइए:

#include <iostream>
using namespace std;

int main()
{
    int choice = 2;

    switch (choice)
    {
        case 1:
            cout << "First";
            break;

        case 2:
            cout << "Second";
            break;

        default:
            cout << "Invalid";
    }

    return 0;
}
Output:
Second

Quick Revision

  • if: Condition true होने पर execution
  • if-else: दो alternatives में selection
  • else-if: Multiple conditions
  • Nested if: if के अंदर if
  • switch: Fixed choices के आधार पर selection
  • case: Specific switch value
  • break: switch से बाहर निकलना
  • default: No matching case होने पर execution
  • ? : Conditional/Ternary operator
One-Line Revision: Conditional statements C++ program को conditions के आधार पर अलग-अलग decisions लेने और statements execute करने की सुविधा प्रदान करते हैं।

Practice Questions

  1. Conditional statement क्या है?
  2. if statement का syntax लिखिए।
  3. if और if-else में क्या अंतर है?
  4. else-if ladder को उदाहरण सहित समझाइए।
  5. Nested if क्या है?
  6. switch statement का syntax लिखिए।
  7. switch statement में break और default का क्या उपयोग है?
  8. if-else और switch में अंतर बताइए।
  9. एक C++ program लिखिए जो किसी number के positive, negative या zero होने की जाँच करे।
  10. एक C++ program लिखिए जो तीन numbers में से सबसे बड़ा number find करे।
  11. एक C++ program लिखिए जो marks के आधार पर student का grade निर्धारित करे।
  12. एक C++ program लिखिए जो switch statement की सहायता से 1 से 7 के बीच day number लेकर day का नाम प्रदर्शित करे।
  13. निम्न program का output बताइए:
    int n = 15;
    
    if (n % 2 == 0)
    {
        cout << "Even";
    }
    else
    {
        cout << "Odd";
    }
Lesson 24 of 39
On This Page