And Or in Kotlin

Find this useful? Support us: Star on GitHub 6
Category: Conditional | Language: Kotlin

In Kotlin, the && operator is used to indicate a logical AND, while || is used for a logical OR.

Here are several examples to explain how to use || and && operators in Kotlin:

### Using && operator:

val x: Int = 5
val y: Int = 10

// using && to check if both statements are true
if (x > 0 && y < 15) {
println("Both conditions are true")
} else {
println("At least one condition is false")
}

In this example, the if statement uses && to check if both conditions are true. Since x is greater than 0 and y is less than 15, the message "Both conditions are true" will be printed.

### Using || operator:

// using || to check if at least one condition is true
if (x > 0 || y > 15) {
   println("At least one condition is true")
} else {
   println("Both conditions are false")
}

In this example, the if statement uses || to check if at least one condition is true. Since x is greater than 0 and y is greater than 15, the message "At least one condition is true" will be printed.

### Combining operators:

val z: Int = 7

// using && and || together
if ((x > 0 && y < 15) || z == 7) {
println("At least one condition is true")
} else {
println("Both conditions are false")
}

In this example, the if statement uses both && and || to check if either x is greater than 0 and y is less than 15, or if z equals 7. Since z equals 7, the message "At least one condition is true" will be printed.