Conditional Statements और उसके प्रकारों की संपूर्ण जानकारी
 

What is Conditional Statements in C++ in Hindi 

C++ में Conditional Statements का Use कुछ Condition के आधार एक Program के flow को Control करने के लिए करते  है।
ये Statements Condition के True या False होने की स्थिति में Program में विभिन्न Block के Code को Execute करने तथा Program के Execution के क्रम को बदलने के लिए जिम्मेदार होते है

Types of Conditional Statements in C++ in Hindi 

C में Conditional Statements के निम्न होते है।
1) if Statement
2) if--else Statement
3) Nested if Statement
4) else--if ladder Statement
5) switch Statement

if Statement

C++ में "if" Statement, एक Fundamental control structure है
जो Program को Condition के आधार पर Decision लेने और Condition के True या False होने की स्थिति मे  कुछ block के Code को Execute या Skip करने की अनुमति देता है

Syntax:

if (condition)
{
block of code;
}

यदि Condition True है तब तो if block के अंदर का Code Execute होगा नही तो इसे छोड़ दिया जायेगा


Program for if Statement

#include<iostream>
using namespace std;
int main()
{
int a,b ;
cout<<"Enter the value of a:"<<endl;
cin>>a;
cout<<"Enter the value of b:"<<endl;
cin>>b;

 if(a<b)
   {
     cout<<"a is smaller than b";
   }
return 0;
}

Output:
Enter the value of a:
5
Enter the value of b:
7
a is smaller than b smaller than b

Explanation:
1)ऊपर के Program में हमने दो Variables a और b को के लिए User से Input लिया है जो क्रमश: 5 और 7 है

2) उसके बाद हमने if के बाद (a<b) एक Condition Expression लगाया है चूकि a की Value b से छोटी है अतः Condition True होगा और if के बाद
का Code Execute हो जायेगा

3) यदि a की Value b से बड़ी होती तो Condition False हो जाता और कोई भी Statement Run नही होता है।

if-else Statement

if - else statement, if statement की कमी को पूरा करता है क्योंकि if statement यदि Condition True है तब तो Block के Code Execute होंगे लेकिन Condition False होने पर कोई भी Statements Execute नही होगा

यदि हम Condition के True होने पर Block के code को execute करना चाहते है और False होने पर भी अन्य Block के Code को execute करना चाहते है तब इस स्थिति मे if-else statement  का Use किया जाता है

Syntax:

if (condition)
{
Statement1;
}
else
{
Statement1;
}

Program for if--else Statement Statement

#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter any number"<<endl;
cin>>num;

     if(num%2==0)
        {
     cout<<"This is an even number:";
        } 

     else
        {
      cout<<"This is an odd number:";
        }
return 0;
}


Output:
Enter any number:
17
This is an odd number:


Explanation:
1) ऊपर Program में num नामक, variable की Value, user से Input के रूप में लिया जाता है 

2) उसके बाद if Condition मे num की value को 2 से divide करके यह जांच की जाती है कि number, even है या odd number है  यदि Condition True है तो if Block के Statements Execute होंगे

3) और यदि Condition False है तब else के बाद का Statement execute होगा

Nested if Statement

C ++ में Nested if statement जटिल निर्णय का निर्माण करने के लिए एक शक्तिशाली tool है 

 एक if या if- else Statements के अंतर्गत दूसरा if या if-else Statement nested-if Statement कहलाता है
एक if statement को दूसरे के अंदर nested करके आप क्रमिक रूप से कई Conditions का मूल्यांकन कर सकते है 
ताकि आप कई निर्णयों के आधार पर अपना कार्य कर सकें है 

Syntax:

if (condition1) 
{
  if or else statement
 } 

else
 {
   if or else statement
}


Program for Nested if Statement Statement
#include <iostream>
using namespace std;
int main()
 {
  int a, b, c;
cout<<"Enter three numbers:"<<endl;
cin>>a>>b>>c;
    
   //Using Nested if-else statements to find the largest number

if (a >= b) //Outer if block
{
        if (a >= c) //Inner if block
          {
 cout<<"a is the largest number"<<endl;
          } 
      else  
         {
  cout<<"c is the largest number"<<endl;
         }
 } 
else //else block start if Outer if block condition false
{
        if (b >= c) 
         {
  cout<<"b is the largest number"<<endl;
         } 
    else 
         {
   cout<<"c is the largest number"<<endl;
         }
}
    return 0;
}
 
Output:
Enter three numbers: 15 10 25
c is the largest number.

Explanation:
सबसे पहले User से तीन Numbers Input के रूप मे लिया जाएगा

Outer if block अगर True है तो निम्न कार्य होगा
1) पहले Outer if block के Condition को check किया जायेगा कि क्या a, b से बड़ा है यदि है तो Control Inner if block में जाकर पुन: a को c से Check किया जायेगा कि क्या a, c से बड़ा है 

यदि Condition True है अर्थात a बड़ा है तो a को Print किया जायेगा
यदि यदि Condition False है अर्थात c बड़ा है तो c को Print किया जायेगा

Outer if block अगर False है तो निम्न कार्य होगा
1) यदि a, b से बड़ा नही है तो Program का Control else block में आ जायेगा
अब b को c से Check किया जायेगा अगर b बड़ा है, तो b Print होगा अन्यथा C Print होगा

Output Explanation:
Enter three numbers: 15 10 25
c is the largest number.

ऊपर a की Value 15, b की 10 और c की 25 है चूकि 25 जो की c की Value है बड़ी है इसलिए c को Largest number के रूप में Print किया जाएगा

else-if ladder

C++ मे "else-if ladder" एक Conditional Steps का क्रम है जब एक Condition पूरी नही होती, तो Program अगले Condition में चली जाती है और यह इसी तरह चलती रहती है जब तक कोई Condition पूरी न हो जाए या ladder के अन्त तक न पहुंच जाए

अतः जब कई निर्णयों के आधार पर कार्य करना होता है तब else-if-ladder का प्रयोग किया जाता है यह भी nested if Statement का एक रूप है

"else-if ladder" का Use करके Programmer ऐसे Software बना सकते है जहां पर Programmer को विभिन्न प्रकार के Conditions की एक व्यापक श्रृंखला को handle करना है  

Syntax:

if (condition1) 
    {
  Statement 1
    } 
else if (condition2) 
   {
    Statement 2
    } 
else if (condition3)
   {
    Statement 3
   } 
...
else {
  Statement 4
}


Program for else--if ladder

#include <iostream>
using namespace std;
int main()
{
int yourScore;
cout<<"Enter your score:";
cin>>yourScore;

     if(yourScore>= 90)
      {
   cout<<"Your grade is: A"<<endl;
       }
    else if (yourScore >= 80)
      {
  cout<<"Your grade is: B"<<endl;
      }
    else if (yourScore >= 70)
      {
  cout<<"Your grade is: C"<<endl;
      }
    else if (yourScore >= 60)
     {
  cout<<"Your grade is: D"<<endl;

     }
   else
    {
  cout<<"Your grade is : F"<<endl;
    }
return 0;
}

Output:
Enter your score:78
Your grade is : C

Explanation:
1) सबसे पहले User से Score Input के रूप में लेते हैं

2) अब else-if ladder का Use करके उन विभिन्न Conditions के आधार पर Enter किए गए Score को Check किया जायेगा और उससे संबंधित Grade को Print किया जायेगा

switch statement

C++ में एक switch statement एक शक्तिशाली Control structure है 
जो आपको expression या value के आधार पर कई Conditions को Handle करने का एक वैकल्पिक तरीका प्रदान करता है 

else..if ladder के बदले switch statement का प्रयोग करने पर switch statement कई Conditions को Manage करने के लिए ज्यादा संगठित और समझने योग्य सरल तरीका प्रदान करता है

Syntax:

switch (expression) 
 {
    case constant1:
        statement 1
        break;
    case constant2:
       statement 2
        break;
       .
       .
       .
    default:
default statement 3        
}

Program for switch Statement
#include <iostream> 
using namespace std;
int main() 
{
   char yourGrade;
 cout<<"Enter your grade:"<<endl;
 cin>>yourGrade;
   
    switch (yourGrade)
     {
        case 'A':
           cout<<"Excellent";
            break;
        case 'B':
            cout<<"Good";
            break;
        case 'C':
            cout<<"Average";
            break;
     
        default:
           cout<<"Improve your grade";
       }
    return 0;
}
Output: Enter your grade: B
Good

Explanation

switch statement:
ऊपर Program में switch statement एक Variable(yourGrade) के value को जो कि एक Character है, को मूल्यांकित करता है और उस Value के आधार पर उचित Case को Execute करता है 

जैसे यदि अपने yourGrade, Variable की Value, 'B' दी है तो वह case 'B' के Statement (Good) को Print करेगा


break statement:
break statement का Use संबंधित case के execution के बाद switch block से exit होने के लिए किया जाता

default statement:
default case उस स्थिति को Handle करता है जब variable की Value के लिए कोई भी cases match नही होती जैसे आपने Variable की Character में Value 'D' दी है उस स्थिति में default statement execute होगी




 Related Posts

> C++ क्या है? उसके इतिहास, गुण, उपयोग, फायदे और नुकसान 

> Basic structure of C++ Program 

> C++ में Tokens क्या है? और उसके प्रकार

> C++ Variables क्या है?, उसके प्रकार, उसे कैसे Declare, Define करते हैं

> C++ में Constants क्या है? उसके प्रकारों की संपूर्ण जानकारी

> C++ में Basic Input और Output (cin,cout,cerr) की जानकारी

> Data type in C++ की संपूर्ण जानकारी 

> C+ में Operators और उसके प्रकार जानें Practical सहित 

> C++ में Looping statements और उसके प्रकार Practical सहित 

> C++ में Jump Statements और उसके प्रकारों की संपूर्ण जानकारी Practical सहित

> C++ में Array क्या है? और उसके प्रकारों की जानकारी Practical सहित

> C++ में Function क्या है उसके प्रकार, उपयोग प्रोग्राम सहित 

> C++ में Structure क्या है Practical सहित

> OOPs Concepts in C++ in Hindi- C++ में OOPs के बारे में

> Oops के फायदे और नुकसान की जानकारी 

> OOP और POP के बीच अंतर 

> C++ में Class और Object की सम्पूर्ण जनकारी

> C++ में Array of Objects क्या है? 

> C++ में Pointers, Pointer to an objects, Pointer to an Array की संपूर्ण जानकारी हिंदी में।

C++ में Passing objects क्या है

> C++ में Reference और Type Casting की संपूर्ण जानकारी

> C++ में Access specifier की संपूर्ण जानकारी 

> C++ में Static Data Members और Member Functions के बारे में Practical सहित 

> C++ में Memory allocation और Memory management operators (new और delete) Practical सहित 

> Friend Function in C++ in Hindi 

> Friend Class in C++ in Hindi Practical सहित 

> Inline function in C++ in Hindi 

> Function Overloading in C++ in Hindi Practical सहित 

> Operator Overloading in C++ in Hindi Practical सहित 

> C++ में Constructor क्या है और उसके प्रकारों की संपूर्ण जानकारी

> C++ में Destructor क्या है ?उसकी संपूर्ण जानकारी

> C++ मे Inheritance क्या है उसके प्रकारों को जानें प्रोग्राम सहित

> C++ में Polymorphism क्या है? और उसके प्रकारों को जानें

> C++ में Virtual function की संपूर्ण जानकारी

> C++ में File handling की संपूर्ण जानकारी 

> C++ में Exception handling  की संपूर्ण जानकारी