6-Lekciya
.pdfCikllar. while, do-while hám for cikl operatorları. Ishpe-ish cikllar. break hám continue operatorları
Joba:
1.Cikllar;
2.for cikl operatorı;
3.while cikl operatorı;
4.do-while cikl operatorı;
5.Ishpe-ish cikllar;
6.break hám continue operatorları.
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
Cikllar
Programma orınlanıwın basqarıwdıń jáne bir kúshli qurallarınıń biritákirarlaw operatorları esaplanadı.
Tákirarlaw operatorları programma kodınıń ózine tiyisli bolǵan bólegin qandayda bir tákirarlaw shártine baylanıslı bir neshe márte tákirarlap orınlaydı.
Tákirar- |
true |
|
|
|
operator1 |
|
operator2 |
||
law |
|
|||
|
|
|||
shárti |
|
|
|
|
|
|
|
|
false
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
for cikl operatorı
0 den 99 ǵa shekemgi sanlardıń hámmesin ekranǵa shıǵarıw kerek bolsın. Programma kodı tómendegishe boladı:
Cikl operatorısız |
for cikl operatorı menen |
System.out.println(0); |
for(int i = 0;i < 100;i++){ |
System.out.println(1); |
System.out.println(i); |
System.out.println(2); |
} |
System.out.println(3); |
|
System.out.println(4); |
|
// hám t.b. |
|
System.out.println(99); |
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
for cikl operatorı
for cikl operatorınıń jazılıw strukturası tómendegishe boladı:
for(<baslanǵısh mánis>;<tákirarlaw shárti>;<esaplaǵısh operatorı>) {
// Cikl denesi
}
0 den 5 ke shekemgi sanlardı nátiyjege shıǵarıw:
i ózgeriwshi for ciklı ishinde |
i ózgeriwshi for ciklı sırtında |
for(int i=0;i<5;i++){ |
int i; |
System.out.println(i); |
for(i=0;i<5;i++){ |
} |
System.out.println(i); |
|
} |
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
for cikl operatorı
Baslanǵısh mánis – cikldaǵı ózgeriwshi ańlatpanıń dáslepki mánisi;
Tákirarlaw shárti – logikalıq ańlatpa bolıp, egerde true bolsa cikl isleydi, false bolsa toqtaydı;
Esaplaǵısh operator – ózgeriwshi ańlatpa mánisi artıp yamasa kemeyip baradı.
|
1-mısal |
2-mısal |
for(int |
i=2;i<10;i=i+2) |
for(i=8;i>0;i=i-2){ |
System.out.print(i+" "); |
System.out.print(i+" "); |
|
// 2 4 6 |
8 |
// 8 6 4 2 |
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
Qosındını hám kóbeymeni esaplaw
n pútin sanı berilgen. 1 den n ge shekemgi sanlardıń qosındısın tabıń.
int n = new Scanner(System.in).nextInt(); |
// 5 |
int s=0; |
|
for(int i=1;i<=n;i++) |
|
s+=i; |
|
System.out.println("summa: " + s); |
// 15 |
|
|
n pútin sanı berilgen. 1 den n ge shekemgi sanlardıń kóbeymesin tabıń.
int n = new Scanner(System.in).nextInt(); // |
5 |
|
int p=1; |
|
|
for(int i=1;i<=n;i++) |
|
|
p*=i; |
|
|
System.out.println("n!= " + p); |
// |
120 |
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
Sheksiz tákirarlaw
Tómendegi jaǵdaylarda for operatorı sheksiz tákirarlanıwı múmkin: a) Tákirarlaw shárti nadurıs qoyılǵanda:
int j=5; |
|
for(long i=0;j==5;i++) |
// j diń mánisi ózgermeydi |
System.out.print(i); |
// 01234 … |
|
|
b) Tákirarlaw shárti true bolǵanda: |
|
|
|
for(int i=0;true;i++) |
|
System.out.println(i); |
// 01234 … |
|
|
c) Bos operatordan paydalanǵanda: |
|
|
|
int i=0; |
|
for(;;) |
|
System.out.println(i); |
// 00000 … |
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
for operatorında birneshe ózgeriwshilerden paydalanıw
for operatorında bir waqıtta birneshe ózgeriwshilerden paydalanıw
múmkin:
for(int j=0,i=5; i>0; i--,j++){ System.out.print(j+"-adım:"); System.out.println(i);
}
for(int i=5,j=11; i!=j; i++,j--){ System.out.println("i="+i+" j="+j);
}
run: 0-adım: 5 1-adım: 4 2-adım: 3 3-adım: 2 4-adım: 1
run: i=5 j=11 i=6 j=10 i=7 j=9
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
Ishpe-iz jaylasqan for operatorı
Birneshe for operatorların ishpe-iz jaylastırıp paydalanıwǵa boladı.
//... |
|
|
for(int i = 0; i < 5; i++) { |
run: |
|
0 | 0 1 2 3 4 |
||
System.out.print(i + " | "); |
||
1 | 0 1 2 3 4 |
||
for(int j = 0; j < 5; j++) { |
||
2 | 0 1 2 3 4 |
||
|
||
System.out.print(j + " "); |
3 | 0 1 2 3 4 |
|
} |
4 | 0 1 2 3 4 |
|
System.out.println(); |
|
|
} |
|
|
//... |
|
|
|
|
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>
while cikl operatorı
while cikl operatorıda for operatorına uqsap birneshe márte tákirarlawlardı ámelge asırıwda qollanıladı. Biraq jazılıw sintaksisi pútkilley parıq qıladı.
while(<shárt>){
operatorlar; //cikl denesi
}
Eger shárt true bolsa, cikl denesi orınlawda dawam etedi, qashan shárt
false bolsa cikl orınlawdı toqtatadı. int i=0;
while(i<4){ System.out.println(i); i++;
}
run:
0
1
2
3
</TITUNF></PI></PROGRAMMALASTIRIW></A.B.ORINBAEV>