C++ 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 Chapter में हमने while व do-while लूप को समझ लिया था अब हम  for  को समझेंगे। 

    3.) For Loop:-

    यह भी एक Entry controlled लूप होता है। इसका प्रयोग सामान्यतः तब किया जाता है जब लूप को निश्चित बार रन करना हो। इसका प्रारूप निम्नानुसार होता है:-

    for (initialization; condition; update) {
    // body of-loop
    }
    for loop flowchart, w3 coding club, w3codingclub

    Explain

    • initialization- Variable(वेरिएबल्स) को Initialize(इनिशियलाइज़ ) करता है और केवल एक बार Execute किया जाता है
    • condition- अगर conditiontrueहो तोforलूप की body Execute किया जाता है,
      andfalse होने पर for लूप  terminated(समाप्त) हो जाता है। 
    • update- आरंभिक Variable की value को update करता है और फिर से Condition check की जाती है

    Example #01:-


        // Printing Numbers From 1 to 5
        #include <iostream>
        using namespace std;
        int main() {
                for (int i = 1; i <= 5; ++i) {          
                cout << i << " ";
            }
            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
    2nd i = 3true 3 is printed and i is increased to 4
    3rdi = 4true4 is printed and i is increased to 5
    4thi = 5true5 is printed and i is increased to 6
    5thi = 6 falseThe loop is terminated

    Example #02:-


        // C++ program to find the sum of first n natural numbers
        // positive integers such as 1,2,3,...n are known as natural numbers    
        #include <iostream>
        using namespace std;
        int main()
        {
            int num, sum;
            sum = 0;
            cout << "Enter a positive integer: ";
            cin >> num;
            for (int i = 1; i <= num; ++i)
            {
                sum += i;
            }
            cout << "Sum = " << sum << endl;
            return 0;
        }

    Output:-

    Enter a positive integer: 10
    Sum = 55

    Example #03:-

    निम्न उदाहरणinitializationतथा increment/decrement का प्रयोग for में किए बिना 1से 5तक संख्याएं प्रिंट की गई है।


        // PROGRAM TO DEMONSTRATE FOR LOOP
        #include <iostream>
        #include <conio.h>
        using namespace std;
        int main()
        {
            int a = 1;
            for (; a <= 5;)
            {
                cout << "\n a = " << a;
                a++;
            }
            getch();
        }
       

    Output:-

    a = 1
    a = 2
    a = 3
    a = 4
    a = 5 

    Explain Example:-

    उपरोक्त उदाहरण भी वेरिएबल को प्रारंभिक वैल्यू तथा उसमें इंक्रीमेंट अथवा डिक्रीमेट स्टेटमेंट में नहीं दी गई है। यह दोनों ही कार्य क्रमशः forforके पहले तथा for स्टेटमेंट के ब्लॉक में किए गए है।

    Example #04:-

    निम्न उदाहरण में एक से अधिक variable(वेरिएबल्स) को initializeinitializeतथा increase/decrease करते हुए 1 से 5 एवं 5 से 1 तक संख्याएं साथ-साथ प्रिंट की गई है।


        // PROGRAM TO DEMONSTRATE FOR LOOP
        #include <iostream>
        #include <conio.h>
        using namespace std;
        int main()
        {
            int a, b;
            for (a = 1, b = 5; a <= 5; a++, b--)
            {
                cout << "a = " << a << "\t\t"
                    << "b = " << b << endl;
            }
            getch();
        }

    Output:-

    a = 1           b = 5
    a = 2           b = 4
    a = 3           b = 3
    a = 4           b = 2
    a = 5           b = 1

    Explain Example:- 

    उपरोक्त उदाहरण में दो variable(वेरिएबल) a व b  प्रयोग किए गए हैं। एक वेरिएबल का मान प्रत्येक लूप में बढ़ रहा है तथा दूसरे का मान प्रत्येक लूप में घट रहा है। ध्यान दें कि initialization सेक्शन में एक से अधिक वेरिएबलों को initialization(इनिशियलाइज) किया जा सकता है। इसी प्रकार दोनों वेरिएबलों के मान में परिवर्तन भी for स्टेटमेंट में एक साथ किया गया है। यह भी ध्यान रखें कि एक से अधिक वेरिएबलों को प्रयोग करने के लिए separator(सेपरेटर) के रूप में कोमा(,)का प्रयोग किया जाता है।

    Post a Comment

    0 Comments