Difference between revisions of "JAVA: Operator Logical dengan Contoh"

From OnnoWiki
Jump to navigation Jump to search
Line 1: Line 1:
 
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:
 
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:
 +
 +
Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi baik itu logika, aritmatika, relasional dll. Mereka diklasifikasikan berdasarkan fungsionalitas yang mereka sediakan. Berikut beberapa jenisnya:
 +
  
 
* Operator Arithmetic
 
* Operator Arithmetic
Line 11: Line 14:
  
 
This article explains all that one needs to know regarding the Logical Operators.
 
This article explains all that one needs to know regarding the Logical Operators.
 +
 +
Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Logika.
  
 
==Logical Operators==
 
==Logical Operators==
  
 
These operators are used to perform logical “AND”, “OR” and “NOT” operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Let’s look at each of the logical operators in a detailed manner:
 
These operators are used to perform logical “AND”, “OR” and “NOT” operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Let’s look at each of the logical operators in a detailed manner:
 +
 +
Operator-operator ini digunakan untuk melakukan operasi logika “AND”, “OR” dan “NOT”, yaitu fungsi yang mirip dengan gerbang AND dan gerbang OR dalam elektronika digital. Mereka digunakan untuk menggabungkan dua atau lebih kondisi / kendala atau untuk melengkapi evaluasi kondisi asli di bawah pertimbangan tertentu. Satu hal yang perlu diingat adalah kondisi kedua tidak dievaluasi jika yang pertama salah, yaitu memiliki efek hubungan arus pendek. Digunakan secara luas untuk menguji beberapa kondisi untuk membuat keputusan. Mari kita lihat masing-masing operator logika secara rinci:
 +
  
 
‘Logical AND’ Operator(&&): This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. For example, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero).
 
‘Logical AND’ Operator(&&): This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. For example, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero).
 +
 +
Operator 'Logis DAN' (&&): Operator ini mengembalikan nilai true ketika kedua kondisi yang dipertimbangkan terpenuhi atau benar. Jika salah satu dari keduanya menghasilkan false, operator menghasilkan false. Misalnya, cond1 && cond2 mengembalikan true ketika cond1 dan cond2 benar (yaitu bukan nol).
 +
  
 
Syntax:
 
Syntax:
Line 69: Line 80:
  
 
'Logical OR' Operator(||): This operator returns true when one of the two conditions under consideration are satisfied or are true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.
 
'Logical OR' Operator(||): This operator returns true when one of the two conditions under consideration are satisfied or are true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.
 +
 +
'Logical OR' Operator(||): Operator ini mengembalikan nilai true ketika salah satu dari dua kondisi yang dipertimbangkan terpenuhi atau benar. Jika salah satu dari keduanya menghasilkan true, operator menghasilkan true. Untuk membuat hasilnya salah, kedua kendala harus mengembalikan false.
 +
  
 
Syntax:
 
Syntax:
Line 124: Line 138:
  
 
'Logical NOT' Operator(!): Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.
 
'Logical NOT' Operator(!): Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.
 +
 +
Operator 'Logical NOT' (!): Tidak seperti dua operator sebelumnya, ini adalah operator unary dan mengembalikan nilai true ketika kondisi yang dipertimbangkan tidak terpenuhi atau merupakan kondisi yang salah. Pada dasarnya, jika kondisinya salah, operasinya mengembalikan nilai true dan ketika kondisinya benar, operasinya mengembalikan false.
 +
  
 
Syntax:
 
Syntax:

Revision as of 05:35, 7 May 2022

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:

Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi baik itu logika, aritmatika, relasional dll. Mereka diklasifikasikan berdasarkan fungsionalitas yang mereka sediakan. Berikut beberapa jenisnya:


  • Operator Arithmetic
  • Operator Unary
  • Operator Assignment
  • Operator Relational
  • Operator Logical
  • Operator Ternary
  • Operator Bitwise
  • Operator Shift

This article explains all that one needs to know regarding the Logical Operators.

Artikel ini menjelaskan semua yang perlu diketahui tentang Operator Logika.

Logical Operators

These operators are used to perform logical “AND”, “OR” and “NOT” operation, i.e. the function similar to AND gate and OR gate in digital electronics. They are used to combine two or more conditions/constraints or to complement the evaluation of the original condition under particular consideration. One thing to keep in mind is the second condition is not evaluated if the first one is false, i.e. it has a short-circuiting effect. Used extensively to test for several conditions for making a decision. Let’s look at each of the logical operators in a detailed manner:

Operator-operator ini digunakan untuk melakukan operasi logika “AND”, “OR” dan “NOT”, yaitu fungsi yang mirip dengan gerbang AND dan gerbang OR dalam elektronika digital. Mereka digunakan untuk menggabungkan dua atau lebih kondisi / kendala atau untuk melengkapi evaluasi kondisi asli di bawah pertimbangan tertentu. Satu hal yang perlu diingat adalah kondisi kedua tidak dievaluasi jika yang pertama salah, yaitu memiliki efek hubungan arus pendek. Digunakan secara luas untuk menguji beberapa kondisi untuk membuat keputusan. Mari kita lihat masing-masing operator logika secara rinci:


‘Logical AND’ Operator(&&): This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. For example, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero).

Operator 'Logis DAN' (&&): Operator ini mengembalikan nilai true ketika kedua kondisi yang dipertimbangkan terpenuhi atau benar. Jika salah satu dari keduanya menghasilkan false, operator menghasilkan false. Misalnya, cond1 && cond2 mengembalikan true ketika cond1 dan cond2 benar (yaitu bukan nol).


Syntax:

condition1 && condition2

Example:

a = 10, b = 20, c = 20
condition1: a < b
condition2: b == c
if(condition1 && condition2)
d = a+b+c
// Since both the conditions are true
d = 50.

// Java code to illustrate
// logical AND operator
  
import java.io.*;
  
class Logical {
    public static void main(String[] args)
    {
        // initializing variables
        int a = 10, b = 20, c = 20, d = 0;
  
        // Displaying a, b, c
        System.out.println("Var1 = " + a);
        System.out.println("Var2 = " + b);
        System.out.println("Var3 = " + c);
  
        // using logical AND to verify
        // two constraints
        if ((a < b) && (b == c)) {
            d = a + b + c;
            System.out.println("The sum is: " + d);
        }
        else
            System.out.println("False conditions");
    }
}

Output:

Var1 = 10
Var2 = 20
Var3 = 20
The sum is: 50

'Logical OR' Operator(||): This operator returns true when one of the two conditions under consideration are satisfied or are true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.

'Logical OR' Operator(||): Operator ini mengembalikan nilai true ketika salah satu dari dua kondisi yang dipertimbangkan terpenuhi atau benar. Jika salah satu dari keduanya menghasilkan true, operator menghasilkan true. Untuk membuat hasilnya salah, kedua kendala harus mengembalikan false.


Syntax:

condition1 || condition2

Example:

a = 10, b = 20, c = 20
condition1: a < b
condition2: b > c
if(condition1 || condition2)
d = a+b+c
// Since one of the condition is true
d = 50.
// Java code to illustrate
// logical OR operator
  
import java.io.*;
  
class Logical {
    public static void main(String[] args)
    {
        // initializing variables
        int a = 10, b = 1, c = 10, d = 30;
  
        // Displaying a, b, c
        System.out.println("Var1 = " + a);
        System.out.println("Var2 = " + b);
        System.out.println("Var3 = " + c);
        System.out.println("Var4 = " + d);
  
        // using logical OR to verify
        // two constraints
        if (a > b || c == d)
            System.out.println("One or both"
                               + " the conditions are true");
        else
            System.out.println("Both the"
                               + " conditions are false");
    }
} 

Output:

Var1 = 10
Var2 = 1
Var3 = 10
Var4 = 30

One or both the conditions are true

'Logical NOT' Operator(!): Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.

Operator 'Logical NOT' (!): Tidak seperti dua operator sebelumnya, ini adalah operator unary dan mengembalikan nilai true ketika kondisi yang dipertimbangkan tidak terpenuhi atau merupakan kondisi yang salah. Pada dasarnya, jika kondisinya salah, operasinya mengembalikan nilai true dan ketika kondisinya benar, operasinya mengembalikan false.


Syntax:

!(condition)

Example:

a = 10, b = 20

!(a<b) // returns false
!(a>b) // returns true

// Java code to illustrate
// logical NOT operator
import java.io.*;
  
class Logical {
    public static void main(String[] args)
    {
        // initializing variables
        int a = 10, b = 1;
  
        // Displaying a, b, c
        System.out.println("Var1 = " + a);
        System.out.println("Var2 = " + b);
  
        // Using logical NOT operator
        System.out.println("!(a < b) = " + !(a < b));
        System.out.println("!(a > b) = " + !(a > b));
    }
}

Output:

Var1 = 10
Var2 = 1
!(a < b) = true
!(a > b) = false


Referensi