Contoh program perulangan menggunakan do … while.
class dowhile1
{
public static void main(String[] args)
{
int i=1;
{
public static void main(String[] args)
{
int i=1;
do
{
System.out.println(“Perulangan ke-”+i);
i++;
}
while (i<=10);
}
}
{
System.out.println(“Perulangan ke-”+i);
i++;
}
while (i<=10);
}
}
Contoh program dengan do … while, jumlah perulangan diinput melalui keyboard.
import java.io.*;
class doWhile2
{
public static void main(String[] arg) throws IOException
{
int i, ulang;
class doWhile2
{
public static void main(String[] arg) throws IOException
{
int i, ulang;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Masukkan Jumlah Perulangan : “);
ulang = Integer.parseInt(in.readLine());
ulang = Integer.parseInt(in.readLine());
i=1;
do
{
System.out.println(“Perulangan ke-”+i);
i=i+1;
}
while (i<=ulang);
}
}
do
{
System.out.println(“Perulangan ke-”+i);
i=i+1;
}
while (i<=ulang);
}
}
Contoh program dengan do … while, jumlah perulangan diinput melalui keyboard untuk menjumlahkan beberapa bilangan bulat pertama
import java.io.*;
class dohhile3
{
public static void main(String[] args) throws IOException
{
int i, hasil, ulang;
class dohhile3
{
public static void main(String[] args) throws IOException
{
int i, hasil, ulang;
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print(“Masukkan Jumlah Perulangan : “);
ulang = Integer.parseInt(in.readLine());
ulang = Integer.parseInt(in.readLine());
hasil=0;
i=1;
i=1;
do
{
System.out.print(i);
{
System.out.print(i);
if (i != ulang)
{
System.out.print(” + “);
}
else
{
System.out.print(” = “);
}
{
System.out.print(” + “);
}
else
{
System.out.print(” = “);
}
hasil = hasil+i;
i++;
}
while (i<=ulang);
System.out.println(hasil);
}
}
i++;
}
while (i<=ulang);
System.out.println(hasil);
}
}
Silahkan Berkomentar Pada Form Komentar dengan sopan dan santun .. No Spam .... !!! thank's @Admin
EmoticonEmoticon