Logical Operators in Kotlin
Definition
These operators are used mainly to check conditions. There are 3 Logical Operations in Kotlin. They are
- Logical OR (||)
- Logical AND (&&)
- Logical NOT (!)
Syntax
1. Logical OR Operator
In a situation where a same operation is to be performed for the satisfaction of condition1 or condition2, this operator is used. It is represented by '||'
if(condition1 || condition2){
//Block of Code
}
For example,
if(value.contains("#") || value.contains("*")){
//Block of Code
}
2. Logical AND Operator
In a situation where the same operation is to be performed for satisfaction all conditions compulsorily, this operator is used. It is reperesented by '&&'
if(condition1 && condition2){
//Block of Code
}
For example,
if(value.contains("#") && value.contains("*")){
//Block of Code
}
3. Logical NOT Operator
This operator performs inversion operation of boolean values.
var variable_one = !variable_two
For example,
var value1 = !value2
Logical Operator Example Program in Kotlin
//Logical Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
var num1 = 100;
var check = true
//Or Operation
if(num1>1000 || num1 <500){
println("$num1 satisfies any one of the conditions")
num1 = 2000
}
//And Operation
if(num1>1000 && num1 <5000){
println("$num1 satisfies both the conditions")
}
//Not Operation
if(check){
check = !check
println("Value of check after Inversion is : $check")
}
}
Sample Output
100 satisfies any one of the conditions
2000 satisfies both the conditions
Value of check after Inversion is : false
Kotlin Operators
- Kotlin Operators Overview
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Logical Operators in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Comparison Operators in Kotlin
- In Operator (in and !in) in Kotlin
- Is Operator (is and !is) in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Not Null Assertion Operator in Kotlin
- Safe Call Operator (?.) in Kotlin
- Elvis Operator (?:) in Kotlin
- Range Operator (..) in Kotlin
Read More Articles
- Declare Variables In Kotlin
- Read Data Input using Scanner in Kotlin
- Double Data type Usage and Type Conversion in Kotlin
- print and println Data Output in Kotlin
- Arithmetic Operators (Mathematical Operators) in Kotlin
- Unary Operators (Sign, Inverts, Increment, and Decrement) in Kotlin
- Equality Operators (==, !=) and Referential equality Operators (===, !==) in Kotlin
- Printing Variables and Values in Kotlin
- Float Data type Usage and Type Conversion in Kotlin
- Long Data type Usage and Type Conversion in Kotlin
- Comparison Operators in Kotlin
- Byte Data type Usage and Type Conversion in Kotlin
- Is Operator (is and !is) in Kotlin
- In Operator (in and !in) in Kotlin
- Char Data type Usage and Type Conversion in Kotlin
- Read Data Input from Command Line in Kotlin
- Assignment Operators and Augmented Assignment Operators in Kotlin
- Indexed Access Operator [, ] in Kotlin
- Read String Data Input in Kotlin
- Short Data type Usage and Type Conversion in Kotlin
- Boolean Data type Usage and Type Conversion in Kotlin
- Logical Operators in Kotlin
- Elvis Operator (?:) in Kotlin
- Repeat and Its Usage in Kotlin
- Safe Call Operator (?.) in Kotlin