package javaapplication1;
class TestBreak {
public static void main(String args[]) {
for(int x=0;x<10;x++)
{
if(x==4)
break;
System.out.println("Nilai x :"+x);
}
}
OUTPUT :
Nilai x : 1Nilai x : 2
Nilai x : 3
Nilai x : 5
Nilai x : 6
Nilai x : 7
Nilai x : 8
Nilai x : 9
2. contoh penggunaan keyword continue :
package javaapplication2;
class TestContinue {
public static void main(String args[]) {
int x=10;
while(x<=20) {
x++;
if(x%2==0)
continue;
System.out.println("Nilai x :"+x);
}
}
OUTPUT :
Nilai x : 12
Nilai x : 14
Nilai x : 16
Nilai x : 18
Nilai x : 20
Nilai x : 14
Nilai x : 16
Nilai x : 18
Nilai x : 20
3. Penggunaan keyword return , method setter , method getter
package javaapplication3;
class Manusia
{
String nama;
boolean isKenyang;
int jmlpiring;
void setNama(String _nama) // penggunaan method setter
{
nama = _nama;
}
String getNama() // penggunaan method getter
{
return nama; // penggunaan keyword return
}
void makan()
{
cekjmlpiring();
if(isKenyang==false)
{
System.out.println(nama+ "Lapar makan ahh ...");
System.out.println(nama+ "Sudah makan tadi ..");
}
}
void cekjmlpiring()
{
jmlpiring = jmlpiring +1;
if(this.jmlpiring>3)
isKenyang = true;
}
}
class DemoManusia
{
public static void main(String []args)
{
Manusia m1=new Manusia();
m1.setNama("Prima"); // penggunaan method setter
m1.makan();
m1.makan();
m1.makan();
m1.makan();
}
}
OUTPUT :
Prima Sudah makan tadi ..