5.1. seleksi
5.1.1. if
1. ketentuan:
if (kondisi)
pernyataan1;
else
pernyataan2;
2. testseleksi.Java
class testseleksi{
public static void man(String args[]){
int a,b;
a=10;
b=a/2;
if(b>5)
System.out.println(“pernyataan1 dieksekusi”);
else
System.out.println(“pernyataan2 dieksekusi”);
System.out.println(“pernyataan3 dieksekusi”);
}
}
3. nested if
class nestedif{
public static void main(String args[]){
int bulan=3;
5.1.2. switch
1. switch merupakan alternatif terbaik dibandingkan menggunakan
nested if.
2. testswitch.Java
class testswitch{
public static void main(String args[]){
int bulan=4;
switch (bulan){
case 1:
case 2:
case 3: System.out.println(“kuartal 1”); break;
case 4:
case 5:
case 6: System.out.println(“kuartal 2”);break;
case 7:
case 8:
case 9: System.out.println(“kuartal 3”);break;
default: System.out.println(“kuartal 4”);
}
System.out.println(“setelah switch”);
}
}
5.2. iterasi
a. While
b. Do-while
c. For
5.2.1. while
1. Bentuk umum:
while(kondisi) Pernyataan;
Dimana:
· Kondisi : dapat berupa ekspres boolean apa pun yang mengembalikan
nilai true atau false.
· Pernyataan : adalah perintah yang ngiin kita eksekusi jika kondisi
bernilai true.
2. testwhile.java
class testwhile{
public static void main(String args[]){
int n=10;
System.out.println(“sebelum while”);
while(n>0){
System.out.println(“nilai n : “ + n);
n--;
}
System.out.println(“setelah while”);
}
}
}
5.2.2. do-while
1. Bentuk umum:
do{
//blok perulangan
}while(kondisi);
5.2.3. for
1. Bentuk umum:
2. testfor.Java
class testfor{
public static void main(String args[]){
int n;
System.out.println(“sebelum for”);
for(n=0;n<5;n++) System.out.println(“nilai n: “ + n);
System.out.println(“setelah for”);
}
}
3. testfor2.Java
class testfor2{
public static void main(String args[]){
int x,y;
System.out.println(“sebelum for”0;
for(x=0,y=6;x<=3 && y>=3; x++, y--){
System.out.println(“nilai x : “ + x);
System.out.println(“nilai y : “ + y);
}
System.out.println(“setelah for”);
}
}
4. testfor3.Java
class testfor3{
public static void main(String args[]){
System.out.println(“sebelum for”);
for(int x=0,y=6;x<3 && y>=]]]]]]]]]]]]3;x++,y--){
System.out.println(“nilai x : “ + x);
System.out.println(“nilai y : “ + y);
}
System.out.println(“setelah for”);
}
}
5.3. jump
a. break
b. continue
c. return
5.3.1. break
1. testbreak.Java
class testbreak{
public static void main(Strng args[]){
for(int x=0;x<10;x++){
if(x==4)
break;
System.out.println(“nilai x :” + x);
}
}
}
2. testbreak2.Java
class testbreak2{
public static void main(String args[]){
for(int x=1;x<3;x++){
System.out.println(“perulangan ke “ + x);
for(int y=0;y<5;y++){
if (y==2) break;
System.out.println(“nilai y : “ + y);
}
}
}
}
break label;
4. testbreak3.Java
class testbreak3{
public static void man(String args[]){
boolean status=true;
label1:{
System.out.println(“di dalam blok kode label1”);
label2:{
if (status) break;
System.out.println(“dalam blok label2”);
}
}
}
}
5.3.2. continue
1. continue digunakan bila kita ingin melakukan terus suatu
perulangan, tetapi karena kondisi tertentu kita ingin sebagian kode
setelah posisi tertentu tersebut, tidak dieksekusi pada perulangan
yang sedang berjalan itu.
2. testcontinue.Java
class testcontinue{
public static void main(String args[]){
int x=0;
while(x -->1){
if(x % 2 ==0) continue;
System.out.println(“nilai x : + x);
}
}
}
3. testcontinue2.Java
class testcontinue2{
public static void main(String args[]){
int x=10,y;
label1: while(x -->0){
y=0;
while(y++<10){
if (y>x){
System.out.println();
continue label1;
}
System.out.println(“x*y + “ “);
}
}
}
}
5.3.3. return
*
**
***
****
*****
******
*******