In this tutorial, you shall learn about OR logical operator in Kotlin, its syntax, truth table, and how to use OR logical operator in programs, with examples.
Kotlin OR
Kotlin logical OR Operator takes two boolean values as operands and returns the output of Logical OR gate operation with the operands as inputs.
||
symbol is used for logical OR Operator.
Syntax
The syntax for logical OR Operator with the two operands is
operand1 || operand2
The operand must be a boolean value, boolean expression, or values that translate to boolean values.
Truth Table
The following is the truth table for Logical OR Gate operation.
operand1 | operand2 | operand1 || operand2 |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
OR Operator returns true if at least one of the two values is true, else it returns false.
For example, if there are two conditions, and if we need to check if at least one of these conditions is true, then we can use logical OR Operator.
Examples
1. Check if n is positive or even number
In the following example, we take an integer value in n
, and check if n
is positive or even number.
Here, there are two conditions. The first condition is to check if the number is positive. The second condition is to check if the number is an even number.
Since we have to check if the number is positive or an even number, we must use logical OR operator.
Main.kt
fun main() {
var n = 10
if (n > 0 || n % 2 == 0) {
println("$n is positive or even or both.")
}
}
Output
10 is positive or even or both.
Conclusion
In this Kotlin Tutorial, we learned how to use logical OR Operator to check if at least one of the two conditions is satisfied.