Tugas Mandiri 05
April5
1. What are the differences between a while loop and a do-while loop? Convert the following while loop into a do-while loop.
int i = 1;
while(i < 10)
if(i % 2 == 0)
System.out.println(i);int i = 1;
while(i < 10)
if(i % 2 == 0)
System.out.println(i++);int i = 1;
while(i < 10)
if((i++) % 2 == 0)
System.out.println(i);
Perbedaan while dengan do-while loop :
While melakukan verifikasi atau pengecekan terlebih dahulu, baru dijalankan. Dengan minimal dijalankan 0 (nol) kali.
Do while menjalankan perintah atau logika terlebih dahulu, baru dilakukan verifikasi atau pengecekan. Dengan minimal dijalankan 1 (satu) kali.
Mengubah while menjadi do-while :
1. A Hasil Dari A
int i = 1;
while(i < 10)
if(i % 2 == 0)
System.out.println(i);
2. B Hasil Dari B
int i = 1;
while(i < 10)
if(i % 2 == 0)
System.out.println(i++);
3. C Hasil Dari C
int i = 1;
while(i < 10)
if((i++) % 2 == 0)
System.out.println(i);
2. Do the following two loops result in the same value in sum?
for(int i = 0; i < 10; ++i){
sum + = i;
}for(int i = 0; i < 10; i++){
sum + = i;
}
Hasilnya ialah 45, Hasil Dari Atas
3. What does the following statement do?
for( ; ; ){
do something;
}