# I am a beginner for Kotlin

**URL:** <https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851>\
**Category:** Language Design\
**Created:** [November 10, 2025, 3:34pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851 "2025-11-10T15:34:33Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![balakrishnanultra01](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/balakrishnanultra01/32/11169_2.png) [@balakrishnanultra01](https://discuss.kotlinlang.org/u/balakrishnanultra01)\
**Post date:** [November 10, 2025, 3:34pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/1 "2025-11-10T15:34:33Z")

</div>

```auto
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

 */

```

---

<div class="post-metadata">

**Author:** ![Landei](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/landei/32/9407_2.png) [@Landei](https://discuss.kotlinlang.org/u/Landei)\
**Post date:** [November 10, 2025, 4:36pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/2 "2025-11-10T16:36:10Z")

</div>

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

```auto
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

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

```

---

<div class="post-metadata">

**Author:** ![balakrishnanultra01](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/balakrishnanultra01/32/11169_2.png) [@balakrishnanultra01](https://discuss.kotlinlang.org/u/balakrishnanultra01)\
**Post date:** [November 10, 2025, 4:52pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/3 "2025-11-10T16:52:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gildor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gildor/32/1891_2.png) [@gildor](https://discuss.kotlinlang.org/u/gildor)\
**Post date:** [November 11, 2025, 6:53am UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/4 "2025-11-11T06:53:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![balakrishnanultra01](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/balakrishnanultra01/32/11169_2.png) [@balakrishnanultra01](https://discuss.kotlinlang.org/u/balakrishnanultra01)\
**Post date:** [November 11, 2025, 5:43pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/5 "2025-11-11T17:43:01Z")

</div>

Ok 👍

---

<div class="post-metadata">

**Author:** ![Skater901](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/skater901/32/10163_2.png) [@Skater901](https://discuss.kotlinlang.org/u/Skater901)\
**Post date:** [November 14, 2025, 8:24am UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/6 "2025-11-14T08:24:03Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![balakrishnanultra01](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/balakrishnanultra01/32/11169_2.png) [@balakrishnanultra01](https://discuss.kotlinlang.org/u/balakrishnanultra01)\
**Post date:** [November 15, 2025, 4:38pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/7 "2025-11-15T16:38:37Z")

</div>

👍

---

<div class="post-metadata">

**Author:** ![MIha\_Markic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/miha_markic/32/11397_2.png) [@MIha\_Markic](https://discuss.kotlinlang.org/u/MIha_Markic)\
**Post date:** [July 18, 2026, 11:18am UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/8 "2026-07-18T11:18:59Z")

</div>

But shouldn’t compiler emit at least a warning in such cases - I mean where the values are a totally different type and result in Any type.

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [July 18, 2026, 12:12pm UTC](https://discuss.kotlinlang.org/t/i-am-a-beginner-for-kotlin/30851/9 "2026-07-18T12:12:16Z")

</div>

There’s certainly an argument for highlighting it in an IDE. (I’m fairly surprised it doesn’t already…)
