Loop

Sintaksis Jawa
tingkat , wulangan
kasedhiya

A snippet kuliah karo mentor minangka bagéan saka kursus Universitas Codegym. Ndaftar kanggo kursus lengkap.


"Halo."

"Halo, Ellie!"

"Iku wektu kanggo sinau bab puteran. Loops minangka prasaja minangka yen / statements liyane, nanging malah luwih menarik. Sampeyan bisa nggunakake daur ulang kanggo nglakokaké sembarang printah utawa pamblokiran printah kaping pirang-pirang. Umumé, daur ulang katon kaya iki: "

Loop (conto 1)

while(boolean condition)               
    command;
Loop (conto 2)

while(boolean condition) 
    block of commands in curly brackets

"Kabeh gampang banget. Perintah utawa blok dieksekusi bola-bali anggere kondisi loop bener. Kaping pisanan, kondisi kasebut dicenthang. Yen kondisi kasebut bener, awak loop (blok perintah) dieksekusi. Dipriksa maneh. Yen kondisi bener, badan loop dieksekusi maneh. Iki bola-bali nganti kondisi kasebut ora bener."

"Apa yen tansah bener utawa tansah palsu?"

"Yen tansah bener, banjur program ora bakal mandheg mlaku: bakal mbaleni loop tanpa wates. Yen tansah palsu, banjur awak loop ora bakal dieksekusi."

Ing ngisor iki sawetara conto:

kode Jawa Katrangan
int i = 3;
while (i >= 0)
{
    System.out.println(i);
    i--;    //Decrease by 1
}
3
2
1
0
int i = 0;
while (i < 3)
{
    System.out.println(i);
    i++;   //Increase by 1
}
0
1
2
boolean isExit = false;
while (!isExit)
{
    String s = buffer.readLine();
    isExit = s.equals("exit");
}
Program bakal nyithak senar saka keyboard nganti string 'metu' dilebokake.
while (true) 
    System.out.println("C");
Program bakal bola-bali nampilake huruf C ing layar.
while (true) 
{
    String s = buffer.readLine();
    if (s.equals("exit")) 
        break;
}
Program bakal maca strings saka keyboard nganti string 'metu' input.

"Sawise pernyataan kondisional, iki ora katon rumit. Aku wis pengin nyoba."

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION