Friday, December 6, 2019

Flow Control in C++

Introduction to C++ Flow Control

Normally जब भी कोई C++ program execute होता है तो पहला दूसरा तीसरा ऐसे ही sequence में सारे statements execute होते है। Program execution की यह sequence program का execution flow कहलाती है।

इस execution flow को आप control कर सकते है। आप चाहे तो किसी statement के execution को skip कर सकते है, किसी statement को एक से ज्यादा बार execute करवा सकते है या फिर program में एक statement से दूसरे statement पर jump कर सकते है।



Program के execution flow को control करने के लिए C++ आपको कुछ built in flow control statements provide करती है। C++ आपको 3 प्रकार के flow control statements provide करती है।

1. Selection Statements
2. Looping Statements
3. Jump Statements
इन statements के बारे में निचे detail से दिया जा रहा है।

Selection Statements
   
ये वो statements होते है जो किसी statement को condition के base पर execute करते है। जब condition match होती है तो statement execute हो जाता है नहीं तो उसे skip कर दिया जाता है। Selection statements 4 प्रकार के होते है। निचे इनके बारे में detail से बताया जा रहा है।

If Statement

If statement एक condition block होता है। Condition true होने पर इस block में दिए गए सभी statements execute हो जाते है। यदि condition true नहीं है तो इस block का कोई भी statement execute नहीं होता है। If statement का general syntax निचे दिया जा रहा है।

if(condition)
{
    // Statements to be executed when condition is true.
}
If statement को निचे उदाहरण के माध्यम से समझाया जा रहा है।

if(5>3)
{
     cout<<“5 is greater than 3”;
}

If-else Statement

If else statement में if statement के साथ else block भी जोड़ दिया जाता है। Else block में वे statements होते है जो condition के false होने पर execute होंगे। इसका general syntax निचे दिया जा रहा है।

if(condition)
{
     // Statements to be executed when condition is true.
}
else
{
     // Statements to be executed when condition is false.
}

If else statement को निचे उदाहरण के माध्यम से समझाया जा रहा है।

if(5>3)
{
     cout<<“5 is greater than 3”;
}
else
{
     cout<<“5 is less than 3”;
}

Nested If
जब आप एक if block में दूसरा if block define करते है तो वह nested if कहलाता है। ऐसे आप कितने भी if block define कर सकते है। इसका general syntax निचे दिया जा रहा है।

if(condition)
{
      if(condition)
      {


      }

}

इसका उदाहरण निचे दिया जा रहा है।

if(num>0)
{
     if(num<2 b="">
     {
              cout<<“Num is 1”;
     }
}

Switch Case

Switch case एक ऐसा block होता है जिसमें आप अपनी choice pass करते है। Switch block में define किये गए जिस case से आपकी choice match हो जाती है उसी case के statements execute हो जाते है। इसका general syntax निचे दिया जा रहा है।

switch(choice)
{
   case 1:
                // Statements to be executed;
                break;
  case 2:
                // Statements to be executed;
                break;
  case 3:
               // Statements to be executed;
               break;
  default:
               // Statements to be executed;
               break;

}

जब आपकी choice से कोई भी case match नहीं करता है तो default case execute हो जाता है। सभी cases के statements के बाद break statement लगाना अनिवार्य है। इसे निचे उदाहरण के माध्यम से समझाया जा रहा है।

#include
using namespace std;


int main()
{
    int choice=4;

    // Passing case in switch
    switch(choice)
    {
        case 1:
                        cout<<“This is case 1”;
                        break;
       case 2:
                        cout<<“This is case 2”;
                        break;
       case 3:
                        cout<<“This is case 3”;
                        break;
       default:
                       cout<<“Please enter number between 1 to 3”;
                       break;
    }

     return 0;
}

output :-

Please enter number between 1 to 3 

Looping Statements

एक loop statement ऐसा block होता है जिसमें define किये गए blocks एक से अधिक बार execute किये जाते है। यदि आप किसी statement को एक से अधिक बार execute करवाना चाहते है तो इसके लिए आप एक loop use कर सकते है। C++ में looping statements 3 प्रकार के है। इनके बारे में निचे दिया जा रहा है।

While loop 

While loop एक ऐसा block होता है जिसमें दिए गए statements तब तक बार बार execute होते है जब तक की दी गयी condition true होती है। जैसे ही condition false होती है loop terminate हो जाता है। यदि condition false ना हो तो loop infinitely चलता रहेगा, इसलिए आप loop control variable को increase करते है।

Loop control variable condition में define किया जाता है। हर loop iteration में इसे increase किया जाता है ताकि condition false हो सके और loop terminate हो जाए। While loop का general syntax निचे दिया जा रहा है।

while(condition)
{
      // Statements to be executed;
      // X++, where X is loop control variable;
}

While loop को निचे उदाहरण के माध्यम से समझाया जा रहा है।
           
#include
using namespace std;


int main()
{
       int num=0;
     
       // While loop iterating until num is less than 5
       while(num<5 b="">
       {
           cout<<“Best Hindi Tutorials”<<“n”;

           // Incrementing num to reach the loop termination condition
           num++;
       }

       return 0;
}

 output :-

Best Hindi Tutorials
Best Hindi Tutorials
Best Hindi Tutorials
Best Hindi Tutorials
Best Hindi Tutorials

Do While Loop

एक do while loop भी while loop की तरह ही होता है। Do while loop में ख़ास बात ये है की ये कम से कम एक बार जरूर execute होता है फिर चाहे condition true हो या फिर flase हो। साथ ही do while loop में condition loop के आख़िर में check की जाती है। Do while loop का general syntax निचे दिया जा रहा है।

do
{
    statements to be executed;
    X++;  // Where X is loop control variable
}while(condition)

जैसा की आप ऊपर दिए गए syntax में देख सकते है condition के check होने से पहले do block में दिए गए statements execute होंगे और इसके बाद condition check होगी।

यदि condition false होती है तो loop terminate हो जायेगा नहीं तो do block वापस execute होगा। Loop control variable को do block में ही increase किया जायेगा। आइये अब इसे एक उदाहरण के माध्यम से समझने का प्रयास करते है।

#include
using namespace std;


int main()
{

    int num=0;

    // Checking condition after executing statements
    do
    {
       cout<<“Best Hindi Tutorials”;
       num++;
    }while(num>10);

    return 0;

}

output :-
Best Hindi Tutorials

For Loop

सभी loops में for loop सबसे simple loop माना जाता है। For loop में loop control variable का initialization, condition और increment तीनों एक ही statement में लिखे जाते है। इसके बाद block में वे statements लिखे जाते है

जिन्हें आप condition true होने पर execute करना चाहते है। For loop का general syntax निचे दिया जा रहा है।

for(initialization;condition;increment)
{
     // Statements to be executed when condition is true.
}
आइये अब for loop के use को एक उदाहरण के माध्यम से समझने का प्रयास करते है।

#include
using namespace std;


int main()
{
    int num;

    // 3 times iterating for loop
    for(num=0;num<=2;num++)
    {
          cout<<“Best Hindi Tutorials”<<“n”;
    }

    return 0;
}

output :-
                 Best Hindi Tutorials
                 Best Hindi Tutorials
                 Best Hindi Tutorials 

Jump Statements

Jump statements वे statements होते है जो execution control को एक statement से दूसरे statement को pass कर देते है। C++ में jump statements 3 प्रकार के होते है। इनके बारे में निचे दिया जा रहा है।

Break Statement 

Break statement loop को terminate करने के लिए use किया जाता है। जब भी आप किसी block में break statement use करते है तो इसे execute होते है ही program का control उस block से बाहर आ जाता है। इसका general syntax निचे दिया जा रहा है।

break;

Break statement के use को निचे उदाहरण के माध्यम से समझाया गया है।

#include
using namespace std;


int main()
{
    int num;
    for(num=0;num<5 b="" num="">
    {
         if(num==3)
         {
             cout<<“Loop terminated by break statement”;
     
             // Terminating loop on 4th iteration
             break;
         }
         else
         {
              cout<<“Best Hindi Tutorials”<<“n”;  
         }
   }

    return 0;
}

output :-
               Best Hindi Tutorials
               Best Hindi Tutorials
               Best Hindi Tutorials

Continue Statement

Continue statement loop की iteration को skip करने के लिए use किया जाता है। Continue statement के use से loop उस iteration को skip करके next iteration को execute करता है। इसका general syntax निचे दिया जा रहा है।

continue;
Continue statement को निचे उदाहरण के माध्यम से समझाया गया है।

#include
using namespace std;


int main()
{
     for(int num=0;num<5 b="" num="">
     {
          if(num==2)
          {
               cout<<“Third iteration skipped!”<<“n”;

               // Skipping third iteration of loop
               continue;
          }
          else
          {
               cout<<“Best Hindi Tutorials”<<“n”;
          }
     }

      return 0;
}

output :- 
              Best Hindi Tutorials
              Best Hindi Tutorials
              Third iteration skipped!
              Best Hindi Tutorials
              Best Hindi Tutorials 

Go to Statement

Goto statement program में एक point से किसी दूसरे point पर jump करने के लिए use किया जाता है। ये jump किसी label पर किया जाता है, इसलिए जब भी आप program में कँही jump करना चाहते है तो पहले label define करते है। एक label define करने का general syntax निचे दिया जा रहा है।

label-name:

जैसा की आप ऊपर दिए गए syntax में देख सकते है, सबसे पहले आप label का नाम define करते है और उसके बाद colon लगाते है। किसी भी label पर jump करने के लिए आप goto statement define करते है। इसका general syntax निचे दिया जा रहा है।

goto label-name;

Goto statement के use को निचे उदाहरण के माध्यम से समझाया गया है।

#include
using namespace std;


int main()
{

     cout<<“Hello Reader!”;
     goto bhtLabel;

   
     if(5>3)
     {
          cout<<“This will not be executed!”;
     }

     bhtLabel:
      cout<<“Hello Reader Again!”;

     return 0;
}

output :-
              Hello Reader!
              Hello Reader Again! 

No comments:

Post a Comment

how to add watermark in movavi video editor | movavi watermark | how to ...

What is static keywords in java ? static keywords in java is a main topic of java.