'Strange behavior in Kotlin For-Loops

Today I found a strange behavior when using For-Loops:

daysToPrepend = 6 daysToAppend = 5

For these two for loops I get different outputs:

Case 1:

for (day in 0..daysToPrepend) {
    Log.i("APP_TEST", day.toString())
}

for (day in 0..daysToAppend) {
    Log.i("APP_TEST", day.toString())
}

Output:

0
1
2
3
4
5
6
0
1
2
3
4
5

Case 2:

for (day in 0..daysToPrepend) {
    Log.i("APP_TEST", "Loop1")
}

for (day in 0..daysToAppend) {
    Log.i("APP_TEST", "Loop2")
}

Output:

Loop1
Loop1
Loop2
Loop2

What is going on here? Am I missing something obvious?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source