' if ' expression doing the opposite

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.

Hi, I would like to help you, but I don’t really understand why you expected another behavior. You pass false, so you get into else which is "I just turned the light on.". Why would it be another way around?

2 Likes

Thank you so much for your reply. What’s confusing me is the tutorial website uses the green marked expressions that I just attached in an image. What’s point of those? I thought as in the first part it is declared if var is = false print “I just turned the light off” else go to second part.

I can’t say for sure what the author had in their mind, but it looks like a “bulb switch simulator”. It the bulb was off, it is now switched to on and it says so. And the opposite.

Thanks so much for your help broot . I just think thst the compiler doesn’t take into account those highlighted lines. And just use first value for true condition and value after else for false condition. I just started learning Kotlin. Thanks for your help again. Be happy, take care.

The compiler does take all lines into account. And the code does make sense.

Let’s take it line by line:

val reaction =

We’re declaring an immutable variable called reaction, which will be set to the result of the following expression.

    if (isLightbulbOn) {

This starts a conditional expression. (That may be a source of confusion. In many other languages, an ifelse construct is a statement; but in Kotlin, it’s an expression, and so can yield a value. If you know Java or C or similar, then it behaves just like the ?: operator.) If the variable is true, then it will evaluate the next bit:

        isLightbulbOn = false

A simple assignment, setting the variable to false. (The if ensures that we only reach this point if it was true, so this flips it.)

        "I just turned the light off."

Just a string expression, which doesn’t do anything on its own. But because it’s the last expression in the block, this is taken as the value of the block (and hence assigned to reaction).

    } else {

What follows is evaluated if the expression (‘isLightbulbOn’) was not true:

        isLightbulbOn = true

Again, a simple assignment, setting it to true. (And again, we only reach this point if it was false, so this flips it.)

        "I just turned the light on."

And again, a simple expression which will be taken as the value of the block (and assigned to reaction).

    }

The overall effect is that isLightbulbOn is flipped, and reaction gets set to a string describing that.

Is that making more sense now, or is anything still unclear?

2 Likes

Basically, changing the value of the variable inside the body of the if…else doesn’t change the code execution path.

Pretty much what you said here is correct. When it hits if (isLightbulbOn), it checks the value of isLightbulbOn at that point in time, and determines which branch to execute. Even if the value of isLightbulbOn changes inside the branch, it doesn’t matter, because the code is going to execute the branch and then finish.

If the light bulb is on, then inside the branch it turns it off and prints out that the light bulb has been turned off. If the light bulb is off, then inside the branch it turns it on and prints out that the light bulb has been turned on.

You would see the exact same behaviour in Java. If you were to write this code in Java, it’d look like this:

boolean isLightbulbOn = false;

String reaction;

if (isLightbulbOn) {
    isLightbulbOn = false;
    reaction = "I just turned the light off.";
} else {
    isLightbulbOn = true;
    reaction = "I just turned the light on.";
}
1 Like

Thank you so much gidds, for your very detailed information. Now I see everything clearly. :smiley: :tada: :confetti_ball: :stars: