Kotlin AND Operator
Kotlin AND Operator &&
returns the result of the logical AND gate operation for the given two operands.
Syntax
The syntax to use logical AND operator is
x && y
where
x
is the left operand&&
is the symbol used for AND logical operatory
is the right operand
Return Value
The above expression returns true if both x
and y
are true.
Chaining of AND Operator
We can also chain the AND operator to perform the logical AND gate operation with more than two inputs.
x && y && z
You can add as many inputs (boolean values) in a single expression as you would like by chaining the logical AND operator as shown in the above expression.
AND Truth Table
The output of logic AND gate operation with two operands x and y is given in the following table.
x | y | x && y |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
Examples
AND of true and false
In the following program, we take two variables: x
, and y
with the values true
and false
respectively; give them as inputs to logical AND operator, and find out the result.
Kotlin Program
fun main() {
val x = true
val y = false
val result = x && y
println("x && y = $result")
}
Output
x && y = false
AND of true and true
In the following program, we take two variables: x
, and y
with the values true
and true
respectively; give them as inputs to logical AND operator, and find out the result.
Kotlin Program
fun main() {
val x = true
val y = true
val result = x && y
println("x && y = $result")
}
Output
x && y = true