do-while and for loop

    Looping 

    Program में  Statements (स्टेटमेट्स) के किसी ब्लॉक को बार-बार Execute(एग्जीक्यूट) करने की प्रक्रिया को Looping(लूपिंग) कहते हैं। एक Statement Block(स्टेटमेट ब्लॉक) को Loop(लूप) की सहायता से कई बार Execute(एग्जीक्यूट) कराया जा सकता है। Looping(लूपिंग)  में Statements (स्टेटमेट्स) का Block(ब्लॉक) तब तक Execute(एग्जीक्यूट) होता है जब तक लूप में दी गई Condition(कण्डीशन) true रहती है। 

    C++ में लूप के मुख्यतः दो भाग होते हैं

    • loop की body या block
    • Control Statement(कंट्रोल स्टेटमेंट )
      • entry controlled loop
      • exit controlled loop

    C++ में Looping(लूपिंग) के लिए तीन Control Statement(कंट्रोल स्टेटमेंट) होते हैं

    1.  while Loop 
    2.  do...while Loop 
    3.  for  Loop 

    Last post Chapter में हमने while लूप को समझ लिया था अब हम do-while  को समझेंगे। 

    2. do-while Loop:-

    do-while लूप exit controlled loop की श्रेणी में आता है क्योंकि इसमें loop(लूप) से बाहर निकलते समय Condition(कण्डीशन)  check(जाँच) होती है। while लूप की भांति यह लूप भी तब तक execute(एग्जीक्यूट) होता है जब तक कण्डीशन true होती है। यह लूप कम से कम एक बार अवश्य रन होता है, वहीं while लूप हो सकता है कि किसी कंडीशन में एक बार भी रन ना हो। 

    do स्टेटमेंट का निम्न प्रारूप होता है.

        initialization section;
        do
        {
            Statement 1;
            Statement 2;
            . . . . . . . . . 
            . . . . . . . . . 
            Statement n;
        }while(Test Condition);

    Flowchart of Do-While Loop

    Flowchart of do-while loop, W3 coding club, w3codingclub

    यदि Loop कीconditionहमेशाtrueहोती है तो लूप अनंत समय तक चलता है (जब तक कि मेमोरी भर न जाए)। 

    उदाहरण के लिए,

    // infinite while loop
    while(true) {
    // body of the loop
    }

    do...while  infinite लूप का एक उदाहरण यहां दिया गया है ।

    // infinite do...while loop

    int count = 1;

    do {
    // body of loop
    }
    while(count == 1);

    Example #01:-


        // C++ Program to print numbers from 1 to 5        
        #include <iostream>
        using namespace std;
        int main()
        {
            int i = 1;
            // do...while loop from 1 to 5
            do
            {
                cout << i << " ";
                ++i;
            } while (i <= 5);
            return 0;
        }

    Output:-

    1 2 3 4 5 

    यहां बताया गया है कि Program कैसे काम करता है।

    IterationVariablei <= 5Action
     i = 1not checked1 is printed and i is increased to 2
    1sti = 2true2 is printed and i is increased to 3
    2ndi = 3true3 is printed and i is increased to 4
    3rdi = 4 true4 is printed and i is increased to 5
    4thi = 5true5 is printed and i is increased to 6
    5thi = 6falseThe loop is terminated

    Example 4: 

     Positive Numbers का Sum(योग) ज्ञात करना


        // program to find the sum of positive numbers
        // If the user enters a negative number, the loop ends
        // the negative number entered is not added to the sum
        #include <iostream>
        using namespace std;
        int main()
        {
            int number = 0;
            int sum = 0;
            do
            {
                sum += number;
                // take input from the user
                cout << "Enter a number: ";
                cin >> number;
            } while (number >= 0);
            // display the sum
            cout << "\nThe sum is " << sum << endl;
            return 0;
        }

    Output:-

    Enter a number: 6
    Enter a number: 7
    Enter a number: 12
    Enter a number: 0
    Enter a number: -2
    The sum is 25


    Post a Comment

    0 Comments