Skip to main content

In Operator (in and !in) in Kotlin

2 min read Updated June 30, 2026
Share:
On this page (8sections)

Definition

  • In operators are mainly used in when statements or expressions to check if a value falls in a range or a collection.
  • This operator also has its use in for loop while iterating a collection.

Syntax

1. in Operator

if(value in collection){
	//Block of Code
}

For example,

if(value in collection){
	println("$value is available in collection")
}

1. !in Operator

if(value !in collection){
	//Block of Code
}

For example,

if(value !in collection){
	println("$value is not available in collection")
}

In and !in Operator Example Program in Kotlin

//In Operator Example Program in Kotlin
//Operator Kotlin Programs, Basic Kotlin Program
fun main(args: Array<String>) {
    val collection = 10..20
    val num2 = 5

    println("in operator in if condition")
    if (15 in collection) {
        println("15 is in $collection")
    }

    println("\nin operator in for loop")
    for(item in collection){
        println("$item is in $collection")
    }

    println("\nin operator in when statement")
    when{
        19 in collection -> println("19 in collection is true")
    }
}

Sample Output

in operator in if condition
15 is in 10..20

in operator in for loop
10 is in 10..20
11 is in 10..20
12 is in 10..20
13 is in 10..20
14 is in 10..20
15 is in 10..20
16 is in 10..20
17 is in 10..20
18 is in 10..20
19 is in 10..20
20 is in 10..20

in operator in when statement
19 in collection is true

How It Works

This Kotlin program demonstrates In Operator (in and !in). It first prepares the data it needs, then walks through the data with a loop to compute the result, and finally prints the output shown in the Sample Output above.

  1. Declare the variables that hold the program’s data.
  2. Iterate over the data using a loop to apply the logic.
  3. Use conditional statements to handle the different cases.
  4. Print the final result to the console so you can compare it with the sample output.

Try changing the input values and re-running the program to see how the output changes — this is the fastest way to understand how the logic behaves.

Frequently Asked Questions

What is In Operator (in and !in) in Kotlin?
In operators are mainly used in when statements or expressions to check if a value falls in a range or a collection.
How do I run this Kotlin example?
Run it in IntelliJ IDEA or Android Studio, or compile from the command line with `kotlinc file.kt -include-runtime -d file.jar` and run `java -jar file.jar`.
How can I practice In Operator (in and !in)?
Copy the example into IntelliJ IDEA or Android Studio, run it, then change the values or add print statements to see how the output changes.

Related Tutorials

Search tutorials