Wednesday 6 December 2017

Kotlin: Logical Operators

Kotlin supports logical operators (also called as Boolean operators), to combine multiple conditions. 
a.   || – lazy disjunction
b.   && – lazy conjunction
c.   ! - negation

1. || – lazy disjunction
operand1 || operand2: It returns true, if any of the operand evaluates to true, otherwise returns false.

Operand1
Operand2
Evaluates To
False
false
false
False
true
true
True
false
true
True
true
true

BooleanOr.kt
fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = false
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = false
 operand2 = true
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = true
 operand2 = false
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")

 operand1 = true
 operand2 = true
 println("${operand1} || ${operand2}: ${(operand1 || operand2)}")
}

Output
false || false : false
false || true : true
true || false : true
true || true : true

Why || is called as Lazy Disjunciton?
If the first expression evaluates to true, then Kotlin won't evaluates the entire expression. So Logical OR is called short circuit OR (or) Lazy disjunction operator.


LazyDisjunciton.kt

fun main(args: Array<String>) {

 var a : Int = 10
 var b : Int = 21

 if ((a < b) || (a++ < b)) {
  System.out.println("This statement evaluated");
 }
 System.out.println("a is not incremented " + a);

 if ((a > b) || (a++ > b)) {
  System.out.println("This statement is evaluated");
 }
 System.out.println("a is incremented " + a);

}

Output

This statement evaluated
a is not incremented 10
a is incremented 11

&& – lazy conjunction
operand1 && operand2: It returns true, if both the operand evaluates to true, otherwise returns false.

Operand1
Operand2
Evaluates To
False
false
false
False
true
false
True
false
false
True
true
true


BooleanAnd.kt

fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = false
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = false
 operand2 = true
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = true
 operand2 = false
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")

 operand1 = true
 operand2 = true
 println("${operand1} && ${operand2}: ${(operand1 && operand2)}")
}


Output

false && false: false
false && true: false
true && false: false
true && true: true

Why && is called Lazy conjunction operator?
If the first statement in the expression evaluates to false, then kotlin won't evaluate the entire expression. So, Logical AND is called short circuit AND.


LazyConjunction.kt

fun main(args: Array<String>) {

 var a: Int = 10
 var b: Int = 21


 if ((a > b) && (a++ > b)) {
  System.out.println("This statement not evaluated");
 }
 System.out.println("a is not incremented " + a);

 if ((a < b) && (a++ < b)) {
  System.out.println("This statement is evaluated");
 }
 System.out.println("a is incremented " + a);

}

Output

a is not incremented 10
This statement is evaluated
a is incremented 11


3. Logical not (!) Operator
If the operand is false, ! Operator evaluates it to true
If the operand is true, ! Operator evaluates it to false
Operand
Evaluates To
False
true
True
false


LogicalNot.kt

fun main(args: Array<String>) {

 var operand1: Boolean = false
 var operand2: Boolean = true

 println("operand1 : ${operand1}")
 println("!operand1 : ${!operand1}")
 println("operand2 : ${operand2}")
 println("!operand2 : ${!operand2}")

}

Output

operand1 : false
!operand1 : true
operand2 : true
!operand2 : false



Previous                                                 Next                                                 Home

No comments:

Post a Comment