I am a beginner for Kotlin

fun main()
{
    var heightAlice = 160
    val heightBob = 175

    val taller = if (heightAlice < heightBob)
    {
        print("Choose Alice\n")
        heightAlice=10
    } else {
        print("Choose Bob\n")
        heightBob
    }

    println("Taller height is $taller")
    println("value changed heightAlice : $heightAlice")

}


/*
How to work : 
heightAlice value assigned 10, But -> println("Taller height is $taller") output is : kotlin.Unit

The heightAlice : 10 value is changed.

Choose Alice
Taller height is kotlin.Unit
value changed heightAlice : 10

 */

Your code is a little confusing, you check for the smaller of the two heights, but call it “taller”.

Unlike C, C++ and Java, assignments have no value in Kotlin. heightAlice = 10 will assign the value 10 to the variable, but as whole statement, it has no value, which is expressed with they type Unit.

If your if branch should return the new value 10, you can simply write

val taller = if (heightAlice < heightBob) {
    print("Choose Alice\n")
    heightAlice=10
    heightAlice
} else ...

If your if branch should return the old value 160, you can do something like

val taller = if (heightAlice < heightBob) {
    print("Choose Alice\n")
    heightAlice.also { heightAlice = 10 }
} else ...
3 Likes

Thank You. Sir, Your reply.
My doubt is (Taller height is kotlin.Unit) → kotlin.Unit , why it’s came, i was assigned the value heightAlice =10.

Because this line `heightAlice = 10` returns Unit, assignement doesn’t return result of assignment, looks that you confused how expressions work in kotlin.

Check again examle from Landei

2 Likes

Ok :+1:

Let’s break it down step by step.

First: val taller = if (heightAlice < heightBob) This code is assigning the RESULT of the if expression to the taller variable. What that means is that whatever the last line of each part of the if expression is, that’s what gets put into the taller variable.

Just to clarify, an if expression has the if block and the else block. The block is everything inside the curly brackets. So let’s take your code: the if block is:
print("Choose Alice\n")
heightAlice=10
The else block is:
print("Choose Bob\n")
heightBob

So, when your code runs, it checks if heightAlice < heightBob. If heightAlice is less than heightBob, then taller is given the value of heightAlice=10. If heightAlice is greater than heightBob, then taller is given the value of heightBob. Do you see the difference?

heightBob is a number. So in the else block, taller is given the value of heightBob, which is 175. However, in the if block, taller is given the value of heightAlice=10. heightAlice=10 is an assignment, where you are modifying the value of heightAlice to become 10. An assignment returns the type of Unit, because really, an assignment is a statement, and not an expression. Statements don’t return a value; expressions return a value. However, in Kotlin, technically everything is an expression. It’s just that expressions that we would normally think of as statements always return the special value Unit.

So heightAlice=10 gives back a Unit, heightBob gives back 175. I hope that makes it clearer. :slight_smile:

2 Likes

:+1: