0
0
Kotlinprogramming~10 mins

Range operator (..) and in operator in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Range operator (..) and in operator
Start
Create range with ..
Use in operator to check membership
If true -> Execute block
If false -> Skip block
End
Create a range using .., then check if a value is inside it using in operator, and run code accordingly.
Execution Sample
Kotlin
val range = 1..5
val x = 3
if (x in range) {
    println("x is in range")
} else {
    println("x is not in range")
}
Check if x is inside the range from 1 to 5 and print a message.
Execution Table
StepActionExpressionResultOutput
1Create range1..5Range from 1 to 5
2Assign xx = 3x = 3
3Check if x in range3 in 1..5true
4If true, printprintln("x is in range")x is in range
5End---
💡 Checked x in range, condition true, printed message, program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
range-1..51..51..51..5
x--333
Key Moments - 2 Insights
Why does 'x in range' return true when x equals 3?
Because the range 1..5 includes all numbers from 1 to 5, and 3 is inside that range as shown in execution_table step 3.
What happens if x is 6 instead of 3?
The condition 'x in range' would be false, so the else block would run, printing 'x is not in range'. This is similar to execution_table step 3 but with false result.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'range' after step 1?
A1..5
B3
Ctrue
Dx
💡 Hint
Check the 'Result' column in row with Step 1 in execution_table.
At which step does the program print 'x is in range'?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Output' column in execution_table to find when the message appears.
If x was 0, what would be the result of 'x in range' at step 3?
Atrue
Bfalse
Cerror
Dnull
💡 Hint
Refer to variable_tracker and execution_table step 3 logic about range membership.
Concept Snapshot
Range operator (..) creates a range from start to end inclusive.
Use 'in' operator to check if a value is inside the range.
Example: val r = 1..5; if (x in r) { ... }
'..' includes both ends.
'in' returns true if value is inside range, false otherwise.
Full Transcript
This example shows how to create a range using the .. operator in Kotlin, which includes all numbers from the start to the end value. We assign x the value 3 and then check if x is inside the range using the in operator. Since 3 is between 1 and 5, the condition is true and the program prints 'x is in range'. The execution table traces each step: creating the range, assigning x, checking membership, printing output, and ending. The variable tracker shows how 'range' and 'x' change during execution. Key moments clarify why the condition is true and what happens if x is outside the range. The quiz tests understanding of the range value, when output occurs, and behavior with different x values.