I was doing a lesson from Kotlin Conditionals: When and If - Dave Leeds on Kotlin
and the ‘if’ conditional is doing opposite of what i expected. Can someone please explain what’s going on here
fun main() {
var isLightbulbOn = false
val reaction =
if (isLightbulbOn) {
isLightbulbOn = false
"I just turned the light off."
} else {
isLightbulbOn = true
"I just turned the light on."
}
println(reaction)
}
This code gives following result => I just turned the light on.
and if i give true value to var. it gives the other result which should be for false
fun main() {
var isLightbulbOn = true
val reaction =
if (isLightbulbOn) {
isLightbulbOn = false
"I just turned the light off."
} else {
isLightbulbOn = true
"I just turned the light on."
}
println(reaction)
}
this code gives the following result=> I just turned the light off.