Tipe Booleans di Kotlin

Dhe997

apa itu Boolean? Boolean adalah sebuah tipe data yang hanya memiliki dua nilai, yaitu true dan false. Terdapat 3 (tiga) operator yang dapat digunakan pada Boolean.

Conjunction atau AND (&&)

Operator AND (&&) akan mengembalikan nilai true jika semua hasil evaluasi expression yang diberikan bernilai true.

  1. fun main() {

  2.     val officeOpen = 7

  3.     val officeClosed = 16

  4.     val now = 20

  5.  

  6.     val isOpen = if (now >= officeOpen && now <= officeClosed){

  7.         true

  8.     } else {

  9.         false

  10.     }

  11.  

  12.     print("Office is open : $isOpen")

  13.  

  14.     /*

  15.         Output : Office is open : false

  16.      */

  17. }


Fungsi di atas menguji apakah jam sekarang berada di antara jam waktu buka kantor dan jam tutup kantor. If expressions di atas bisa Anda sederhanakan jadi seperti berikut:

  1. fun main() {

  2.     val officeOpen = 7

  3.     val officeClosed = 16

  4.     val now = 20

  5.  

  6.     val isOpen = now >= officeOpen && now <= officeClosed

  7.  

  8.     print("Office is open : $isOpen")

  9.     /*

  10.         Output : Office is open : false

  11.      */

  12. }



Disjunction atau OR (||)

Berbeda dengan operator AND (&&), operator OR (||) akan mengembalikan nilai true jika hasil evaluasi dari salah satu expressions yang diberikan bernilai true.

  1. fun main() {

  2.     val officeOpen = 7

  3.     val officeClosed = 16

  4.     val now = 20

  5.  

  6.     val isOpen = now < officeOpen || now > officeClosed

  7.  

  8.     print("Office is closed : $isOpen")

  9.     /*

  10.         Output : Office is closed : true

  11.      */

  12. }


Variabel isOpen di atas bernilai true. Alasannya, hasil evaluasi salah satu expression yang diberikan, bernilai true, yaitu expression disebelah kanan.

Negation atau NOT (!) 

Berbeda dengan operator AND (&&) dan operator OR(||), operator NOT(!) digunakan untuk melakukan negasi pada hasil evaluasi expression yang diberikan. Contoh, Jika hasil expressions setelah dievaluasi bernilai true, maka operator NOT akan mengembalikan nilai false.

  1. fun main() {

  2.     val officeOpen = 7

  3.     val now = 10

  4.     val isOpen = now > officeOpen

  5.  

  6.     if (!isOpen) {

  7.         print("Office is closed")

  8.     } else {

  9.         print("Office is open")

  10.     }

  11.  

  12.     /*

  13.         Output : Office is open

  14.      */

  15. }


Hasil evaluasi expression di atas adalah true. Tapi ketika menggunakan operator NOT maka akan dinegasikan menjadi nilai false. Sehingga statement pada branch else-lah yang akan dijalankan

Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.