Beda for, while, dan do...while

Mau tau perbedaan perulangan menggunakan for, while, atau do…while? Cek aja deh ^^
Ketiga perulangan ini menghasilkan output yang sama, seperti screen shoot di bawah ini :


Contoh program menggunakan while
public class perulangan {
public static void main (String [] args) {
int i=0;
while(i<9)>
System.out.println(“Baris-“ +i);
i++;
}
}
}

Contoh program menggunakan do…while
public class perulangan1 {
public static void main (String [] args) {
int i=0;
do {
System.out.println(“Baris-“+i);
i++;
}
while(i<9);
}
}

Contoh program menggunakan for
public class perulangan2 {
public static void main (String [] args) {
for(int i=0; i<9;>
System.out.println(“Baris-“+i);
}
}
}

0 Response to "Beda for, while, dan do...while"