Operator dalam java


A.    operator pembanding dijava
==        equals
!=        
<=
>=
<
>
B.     operator logika
and      &&
or         ||
not       !
contoh latihan pake scanner:
import java.util.*;
public class latihanIfElse{
            public static void main(String [] args){
            int diskon=0;
            Scanner input=new Scanner(System.in);
            System.out.print("masukan total belanja : ");
                        int total=input.nextInt();
            if(total >= 10000){
                        diskon=total/10;
            }else{
                        diskon=0;
            }
            System.out.println("Diskon : "+diskon);        
            }         
}
rule if else if:
-mengatur pernyataan yang dijalankan sewaktu kondisi berupa pilihan
if(kondisi A) {
            //dieksekusi jika A benar
}else if (kondisi B){
            //DIEKSEKUSI jika B benar
}else if (kondisi C){
            //DIEKSEKUSI jika C benar
}else{
            // DIEKSEKUSI JIKA KONDISI SELAIN DARI DIATAS
}
contoh:
public class pernyataanIfElseIf{
            public static void main(String [] args){
            int nilai=86;
            char hm;
            if(nilai >= 90){
                        hm='A';
            }else if (nilai>=80){
                        hm='B';
            }else if (nilai>=70){
                        hm='c';
            }else{
                        hm='D';
}
            System.out.println("Huruf mutu : "+hm);      
            }         
}
contoh switch:
public class pernyataanSwitch{
//jika tidak dikasih break maka java akan mengeksekusi hingga ada
//perintah break dilakukan
            public static void main(String [] args){
            int pilih=2;
switch(pilih){
case 1:
            System.out.println("soto ayam");
            break; 
case 2:
            System.out.println("ayam goreng");
            break;
case 3:
            System.out.println("sate ayam");
            break;
default:
            System.out.println("harap pilih 1,2 atau 3");  
                        }         
            }
}
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment