LOOPING IN C++(Организация циклов в языке С++)

Looping in C++(Организация циклов в языке С++)

Loop ဆိုသည္မွာ ပရိုဂမ္မ်ားတြင္ action(operator) တစ္ခ်ိဳ႕ အား အလိုအေလ်ာက္အၾကိမ္မ်ားစြာ လုပ္ေဆာင္ ေသာ လုပ္ေဆာက္ခ်က္ကို loop ဟုေခၚဆိုၿခင္းၿဖစ္သည္။ ထို ထပ္ခါထပ္ခါ ၿပဳလုပ္ေနေသာ အစိတ္ အပိုင္းအား loop ၏ body ၏ေခၚဆိုနိုင္သည္။
C++ language တြင္အသံုးၿပဳေနေသာ loop types သံုးမ်ိဳးမွာ ေအာက္ပါအတိုင္းၿဖစ္သည္။
•        while loop
•        do – while loop
•        for loop

while loop (Оператор цикла с предварительным условием)
while loop ၏ syntax သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
while (condition input into the cycle) operator;
အကယ္၍ loop body သည္ operator တစ္ခုထက္ပိုမိုပါ၀င္ခဲ့လွ်င္ ေအာက္ပါ syntax အားအသံုးၿပဳသည္။
while (condition input into the cycle) {statement1;
            statement2;
            . . .
            statementN;
                                                } / / End while



အကယ္၍ loop ထဲသို႕၀င္လာေသာ condition သည္ခ်က္ခ်င္းမွားယြင္းေနလွ်င္ မည္သည့္ ထို loop ၏ body အားတစ္ၾကိမ္တစ္ခါ operate ၿပဳလုပ္မည္မဟုတ္ပဲ ထို loop body အၿပင္သို႕ ထြက္ခြာသြားလိမ့္မည္ၿဖစ္သည္။ အဓိကအေနၿဖင့္ ထို loop body တြင္ ထို loop condition အားသက္ေရာက္ေစမည့္ operation တစ္ခုခု မၿဖစ္မေနပါ၀င္ရမည္ၿဖစ္သည္။အကယ္၍ ထိုသို႕ မပါ၀င္ပါက loop သည္ infinite ၿဖစ္သြားမည္ၿဖစ္သည္။

Example 4.1. Print the value of sym, varying from 'A' to 'Z', by using the cycle before the word.
# include <iostream>
using namespace std;
int main ()
{char sym;
cout << "Print out the alphabet on the screen" << endl;
 sym = 'A';
while (sym <= 'Z')
{cout << sym << '';
sym + +;    // အကယ္၍ while condition ကိုၿပန္လည္သက္ေရာက္ေစေသာ  sym++
                   မပါ၀င္ပါက loop သည္ infinite ၿဖစ္သြားမည္ၿဖစ္သည္။
}
return 0;
}

do-while loop(Оператор цикла с последующим условием)
do-while loop ၏ syntax  သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
do   {statement;} while (condition for the continuation of the cycle);
(သို႕)အကယ္၍ operation တစ္ခုထက္ပိုမို၍လုပ္ေဆာင္လိုလွ်င္ ေအာက္ပါ syntax အားအသံုးၿပဳနိဳင္သည္။
do   {   statement1;
           statement2;
           . . .
           statementN;
       } while (condition for the continuation of the cycle);

While-operator နွင့္ do-while operator တို႕၏ အဓိကၿခားနားခ်က္သည္ do-while operator သည္ အနည္းဆံုး loop body အားတစ္ၾကိမ္တစ္ခါ execute ၿပဳလုပ္မည္ၿဖစ္သည္။ထိုသို႕အနည္းဆံုးတစ္ၾကိမ္ၿပဳလုပ္ၿပီးမွသာ while condition test အားဆက္လက္ၿပဳလုပ္မည္ၿဖစ္သည္။
Loop body တြင္ condition test အားအက်ိဳးသက္ေရာက္ေစေသာ လုပ္ေဆာင္ခ်က္ တစ္ခုခုပါရွိရမည္ၿဖစ္သည္။ အကယ္၍ ထိုအက်ိဳးသက္ေရာက္ေစေသာ လုပ္ေဆာင္ခ်က္မပါပါက loop သည္ infinite ၿဖစ္သြားမည္ၿဖစ္သည္။

Example. Display the value of a variable, changing from 'A' to 'Z', by using the do-while loop.
# include <iostream>
using namespace std;
int main ()
{char sym;
 cout << "Print out the alphabet on the screen" << endl;
 sym = 'A'-1;    // condition test    ၿဖစ္ေသာ(sym>’Z’)အား သက္ေရာက္ေစေသာ
                        လုပ္ေဆာင္ခ်က္   
 do {sym + +;  
      cout << sym << '';
     } while (sym <'Z');
   return 0;
}


For loop(Оператор цикла с параметром)
For loop အားမ်ားေသာအားၿဖင့္ loop body အား မိမိဘယ္နွစ္ၾကိမ္ loop လုပ္မည္ဟုသိရွိေသာအခါမွ သာ သံုးေလ့ရွိသည္။
၎၏ေရးနည္း  syntax သည္ေအာက္ပါအတိုင္းၿဖစ္သည္။
for (выражение1; выражение2; выражение3) оператор;
အကယ္၍ operator တစ္ခုထက္ပိုမို၍ သံုးစြဲလုိပါက ေအာက္ပါအတိုင္းေရးသားသည္။
for (выражение1; выражение2; выражение3) оператор;
အကယ္၍ operator တစ္ခုထက္ပိုမို၍ သံုးစြဲလုိပါက ေအာက္ပါအတိုင္းေရးသားသည္။
for (выражение1; выражение2; выражение3)
{ оператор1;
  оператор2;
  . . . . . . . . . .
 операторN;
}

ဤတြင္ expression1 သည္ initialization expression(စတင္အလုပ္လုပ္မည့္) ၿဖစ္ၿပီး expression2 နွင့္ expression3 အား analysis မၿပဳလုပ္ခင္ တစ္ၾကိမ္သာ အလုပ္လုပ္လိမ့္မည္ၿဖစ္သည္။expression2 သည္ loop ၏ condition ၿဖစ္သည္။ expression2 သည္ loop အား အလုပ္မလုပ္ခင္တိုင္း analyze ၿပဳလုပ္မည္ၿဖစ္သည္ ။ expression3 သည္ ပံုမွန္အားၿဖင့္ parameter အားေၿပာင္းလဲရန္အတြက္ၿဖစ္ၿပီး မၿဖစ္မေနပါ၀င္ရလိမ့္မည္ဟု မဆို လိုေပ။  

Example Calculate and display the value of Y = X * sinX on the interval [0, Pi] with a step Pi/20.
#include <iostream>
using namespace std;
#include <iomanip.h>
#include <cmath.h>
#include <conio.h>
const double Pi=3.14159265;
int main()
 { double x, dx=Pi/20;
    clrscr();                                // <conio.h>
    cout << setw(10) << "Y=X*sinX" << endl;
    cout << setw(7) << "X" << setw(7) << "Y" << endl;
    for(x=0; x<=Pi; x+=dx) cout << setw(7) << x << setw(7) << x*sin(x) << endl;
    getch();
    return 0;
 }

Comma operator(Операция "запятая")
“comma” operator သည္ for loop အားေၿပာင္းလြယ္ၿပင္လြယ္မွဳ႕ၿပဳလုပ္ရာတြင္ အဆင္ေၿပေစ၇န္အသံုးၿပဳၿပီး ၎ for-loop တြင္လည္း initialize expression တစ္ခုထက္ပိုမို၍ ထည့္သြင္းအသံုးၿပဳနိဳင္ေစသည္။”comma” operator သည္ expression 2 ခုအားတစ္ခုအၿဖစ္သို႕ ေပါင္းစပ္ၿပီး ၎တို႕အား calculate ၿပဳလုပ္ရာတြင္ left မွ right သို႕အစဥ္လိုက္ၿဖစ္ေစသည္။

Example.Let rates are negotiating points: the first minute of conversation costs 2 rubles., And each following - 1.5 rubles. Determine how much one minute, two minutes. . . , ten minutes voices. (ဖုန္းေၿပာခတြက္ခ်က္ၿဖင္းၿဖစ္ၿပီး ပထမတစ္မိနစ္ေခၚဆိုခသည္ 2 ရုဘယ္ၿဖစ္ၿပီး ေနာက္ပိုင္းမိနစ္မ်ားေခၚဆိုခသည္ 1.5 ရူဘယ္ဆီသာ တိုးၿမွင့္သြားမည္သည္။တစ္မိနစ္.နွစ္မိနစ္ ..ထို႕အတူ 10 မိနစ္အထိေခၚဆိုခမ်ားအား တြက္ခ်က္ၿခင္းၿဖစ္သည္။ )

# include <iostream>
using namespace std;
# include <iomanip.h>
const int FIRST = 2;
const float NEXT = 1.5;
int main ()
{   int minutes;
    float cost;
   for (minutes = 1, cost = FIRST; minutes <= 10; minutes + +, cost + = NEXT)
   cout << setw (4) << minutes << setw (8) << cost << endl;
   return 0;
}


break operator
break operator အား current loop ၿပီးဆံုးသြားေသာအခါ program ၏ အလုပ္မ်ားနွင့္ အဆက္ ၿဖစ္ေစရန္ အသံုးၿပဳသည္။ထို႕အတူ loop မွထြက္လာေသာအခါ က်န္ရွိေနေသးေသာ ပရိုဂမ္အစိတ္အပိုင္း အား execute ၿပဳလုပ္ရန္အတြက္လည္း အသံုးၿပဳသည္။
for( ; ;) {   . . .
               if (выражение)  break;
             }
Open loop အား ‘infinitely’ (бесконечно) အားၿဖင့္ execute ၿပဳလုပ္ထားၿပီး သို႕ေသာ္ if statement  ၏ expression သည္ true ၿဖစ္ေပၚပါက operator break အား execute ၿပဳလုပ္လိမ့္မည္ၿဖစ္သည္။ ထိုသို႕အားၿဖင့္ 'infninitely’ loop အား break ၿပဳလုပ္ၿပီး ဆက္လက္၍ က်န္ရွိေသာ အပိုင္းအားဆက္လက္၍ လုပ္ကိုင္လိမ့္မည္ ၿဖစ္သည္။

Example  Find the first negative number in the sequence of ten number.
ရိုက္ထည့္လိုက္ေသာ ကိန္း၁၀ လံုးမွ ပထမဆံုး မကိန္းအားရွာေဖြပါ။
# include <iostream>
using namespace std;
int main ()
{  int i, a;
for (i = 0; i <10; i + +) {     cout << "Enter number ";
                                         cin >> a;
                                          if (a <0) break;
                                   }
if (i <10)       / / control is transferred here after the break statement
cout << "First negative number of sequence:" << a << endl;
else
cout << "In the sequence there are no negative number" << endl;
return 0;
}


goto statement
unconditional-jump operator ၿဖစ္ေသာ goto operator သည္ conditional operator မဟုတ္သကဲ့သို႕ loop operator မ်ားလည္းမဟုတ္ေပ။သို႕ေသာ္ ၎သည္ ပရိုဂမ္ထဲတြင္ operator မ်ားအစဥ္အလိုက္ ၿဖစ္ေနၿခင္းအား ေၿပာင္းလဲေပးနိဳင္သည္။ထို႕အၿပင္ ၎ operator မွ direction ၿပဳလုပ္ေပးေသာ tag ၿပဳလုပ္ထားသည့္ အမွတ္ အသား ညြန္းတန္းတစ္ခုခုၿဖင့္လည္း pass ၿပဳလုပ္နိဳင္သည္။
goto label;
operators of the program;
operators to which a translation;

operator goto အားအသံုးၿပဳၿခင္းေသာအက်ိဳးေက်းဇူးသည္ တစ္ခါတစ္ရံဒိြဟေတြေ၀စ၇ာရွိနိဳင္သည္။ အဘယ္ေၾကာင့္ဆိုေသာ္ ၎သည္ “spaghetti” –program မ်ားဖန္တီးရာတြင္လြန္စြာၿမန္ဆန္ေစၿပီး debug ၿပဳလုပ္ရာနွင့္ correct ၿပဳလုပ္ရာတြင္ အခက္အခဲၿဖစ္ေစတက္သည္။ ထို႕ေၾကာင့္ goto operator မ်ား ပရိုဂမ္ မ်ားတြင္ အသံုးၿပဳၿခင္းသည္ အလြန္အမင္းေကာင္းမြန္သည့္ စတိုင္တစ္ခုမဟုတ္ေပ။

Stop the program by using the exit(exit operator)
အကယ္၍ ပရိုဂမ္အား ရပ္စဲၿခင္းၿပဳလုပ္ရန္လိုအပ္ပါက exit operator = exit() အားအသံုးၿပဳနိဳင္သည္။၎၏ prototype သည္ STL- libeary ထဲတြင္ define ၿပဳလုပ္ၿပီးၿဖစ္သည္။
Operator exit(argument) အား execution ၿပဳလုပ္သည္နွင့္တစ္ၿပိဳင္နက္ ပရိုဂမ္သည္ ခ်က္ခ်င္း ၿပီးဆံုးသြားမည္ ၿဖစ္ၿပီး clean-up actions အခ်ိဳ႕အားလည္းၿပဳလုပ္လိမ့္မည္ၿဖစ္သည္။argument ၏တန္ဖိုးသည္ Program ၏ အေၿခအေနအား pass ၿပဳလုပ္နိဳင္ၿပီး analyze လည္းၿပဳလုပ္နိုင္သည္။ပံုမွန္ေရးသားသည့္ စည္းစနစ္ အရဆိုရလွ်င္ argument တန္ဖိုး 0 သည္ မွန္ကန္ေသာ output အားရည္ညြန္းသည္ၿဖစ္ၿပီး တစ္ၿခားေသာ တန္ဖိုးမ်ားသည္ အမွားသေကၤတမ်ား နွင့္ အမွား code မ်ားအတြက္ ရည္ညြန္းသည္ၿဖစ္သည္။



 translated by zmk@miet51

1 comment: