C/C++ tilida takrorlanuvchi dasturlar Agar dastur bajarilishi davomida operator yoki operatorlar bir necha marta qayta-qayta bajarilsa, takrorlanuvchi (siklik) jarayon deyiladi. C/C++ tilida takrorlanishni 3 hil ko’rinishda tashkil qilish mumkin: 1. Sharti avval tekshiriladigan takrorlanish while ( shart ) operator(lar); Bu erda operator (lar) while da ko’rsatilgan shart yolg’on bo’lgunicha takrorlanadi. Agar takrorlanishda birdan ortiq operatorlar qatnashsa, ularni alohida { } ichiga olinadi. Masalan: b=2(a+5); a [ 1;10]; h=1 # include # include int main ( ) { int a=1, b; while (a<=10) { b=2*(a+5); cout << “ a=“<< a << “ b=“<< b << endl; a++; } return 0; } 2-misol. # include void main ( ) { int i=10; while (i++<=15) cout << “Salom yoshlar!”< 2. Sharti keyin tekshiriladigan takrorlanish do operator (lar); while (shart); Bu erda ham takrorlanish shart yolg’on bo’lgunicha davom etadi. Agar do ichiida operatorlar qatnashsa, ular alohida { } ichiga olinadi. Masalan: y=sin x; x € [1; 2]; h=0,1; # include #include void main ( ) { float x=1, y, h=0.1; do { y=sin(x); cout <<“x=“< 2-misol. # include void main ( ) { int n; do { cin >>n; cout <<“Sonni qayta kiriting!”< 3. Parametrli takrorlanish for (bosh qiymat; shart; o’zgarish qadami) operator (lar); Bu erda takrorlanish for ichidagi shart yolg’on bo’lgunicha davom etadi. Agar operatorlar qatnashsa ular alohida { } ichiga olinadi. Masalan: y=cos x; x € [2; 3]; h=0,2; #include #include int main ( ) { float x, y; for (x=2; x<=3; x=x+0.2) { y =cos(x); cout<<“x=“<< x <<“ y=“< for operatorining ko’rinishlariga misollar: for (n=10; n>0; n - -) for (k=2; k<=60; k+=12) for (d=1; d*d<216; d++) for (i=100; i<500; i*=1.1) for (x=1, y=1; y<=75; y+=5*(x++)+10) for (x=1, y=0; x<10; x++, y=y+x) Takrorlanishda for operatori tanasiga hech qanday narsa yozmasdan turib ham amal bajarsa bo’ladi. for operatorining bo’sh bo’lgan holidagi ko’rinishiga misol: # include void main ( ) { for ( int i=0; i<5; cout << “i=“< Takrorlanishda for operatori parametrlari sifatida bir nechta o’zgaruvchilarni – hisoblagich (schyotchik)larni ishlatish mumkin. Bunday holda ular orasiga vergul ( , ) qo’yib yoziladi. Masalan: # include void main ( ) { int i, j; for (i=0, j=0; i<10; i++, j++) cout <<“i=“<< i << “j=“<< j < for parametrlaridan ihtiyoriy biri tushib qolishi ham mumkin. Bunday holda for huddi while ga o’hshab ishlaydi. Masalan: # include void main ( ) { int a=0; for ( ; a<=5 ; ) { cout <<“\nSalom talabalar!”; a++; } } Takrorlanishlar ichma-ich tashkil etilishi ham mumkin. Masalan: # include void main ( ) { int a1, a2; char s; cout <<“Qatorlar sonini kiriting=“; cin >>a1; cout <<“\nUstunlar sonini kiriting=“; cin >> a2; cout <<“\n Belgini kiriting=“; cin >> s; for (int i=1; i<=a1; i++) { for (int j=1; j<=a2; j++) cout << s << “ ”; cout << “\n”; } } Misol. 100 gacha bo’lgan juft sonlarni ekranga chiqarish dasturini 3 hil usulda tuzing.
……..
void main ( )
{ int i=2;
while (i <=100)
{cout <<“\ni=“<
i=i+2;
}
}
|
……..
void main ( )
{ int i=2;
do
{cout <<“\ni=“<
i+= 2;
}
while (i <=100);
}
|
……….
void main ( )
{ int i;
for(i=2; i<=100; i+=2)
cout <<“\ni=“<< i; }
Yoki:
for(i=1;i<=100;i++)
if(i%2==0)
cout<< “i=“<
| Ba’zan takrorlatishlarni tashkil etishda break va continue operatorlari ham ishlatiladi. 1. break – shartni qanoatlantirmasa sikldan tashqariga chiqish; 2. continue - shartni qanoatlantirmasa keyingi qiymatga o’tish, lekin sikldan chiqib ketmaydi. Masalan: # include void main ( ) { int x, y; for ( x=1; x<=21; x+=2) { if ( x = = 5 || x == 13) continue; y = 2*x+45; cout <<“x=“<< x <<“ y=“<< y < 2-misol. 10ta ketma-ket kiritilgan butun sonlar yig’indisini hisoblash dasturi tuzilsin. Agar son manfiy bo’lsa yigindini hisoblash to’xtatilsin. # include int main ( ) { int x, k, s=0; for (k=1; k<=10; k++) {cin >> x; if (x<0) break; s = s + x; } cout << “s=“<< s << endl; return 0; }
Do'stlaringiz bilan baham: |