# Why I can't apply value inside while loop?

**URL:** <https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762>\
**Category:** Support\
**Created:** [May 11, 2018, 1:05pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762 "2018-05-11T13:05:46Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![roiamiel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/roiamiel/32/5359_2.png) [@roiamiel](https://discuss.kotlinlang.org/u/roiamiel)\
**Post date:** [May 11, 2018, 1:05pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/1 "2018-05-11T13:05:46Z")

</div>

Hey, I’d like to know why kotlin doesn’t support this code:

 ![Screenshot%20from%202018-05-11%2016-00-26](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/a/a74b8cd0783a1ba5fec40fcc6c72c9d071b38585.png)

I’m using Android Studio 3.1.2 along with Kotlin 1.2.41  
Thanks.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 11, 2018, 1:18pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/2 "2018-05-11T13:18:06Z")

</div>

Because “assignments are not expression”, they are statements, like variable declarations or for/while loop.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 11, 2018, 1:20pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/3 "2018-05-11T13:20:05Z")

</div>

The Kotlin team decided against it for a few reasons I think. In most cases it is just considered bad style and it also makes code harder to read. Yes your example is the one valid place where this kind of feature would be pretty handy, but they decided against it still, because only a small numbers of libraries would actually use something like this and it’s not worth adding an new feature to the language just for that.  
Also there is a massive discussion about this here

> [@Assignment not allow in while expression?](https://discuss.kotlinlang.org/t/assignment-not-allow-in-while-expression/339):
>
> Hi, In Java, I can do thing like this: `` BufferedReader reader = new BufferedReader(reader); String line = null; while ((line = reader.readLine()) != null) { &nbsp;&nbsp;System.out.println(line); } However in Kotlin such thing is not allowed. I get error like “Assignments are not expressions, and only expressions are allowed in this context” So the best I can come up with is something like this, but I wonder is there a better to write this in Kotlin? I like the Java syntax better because it’s s…

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [May 11, 2018, 5:33pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/4 "2018-05-11T17:33:08Z")

</div>

I recently encountered the similar problem. The solution was rather elegant:

```kotlin
do{
  val bytesRead = stream.read(buffer)
} while(byteRead != -1)

```

By the way, please do not use screenshots to present the code.

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [May 12, 2018, 6:39pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/5 "2018-05-12T18:39:38Z")

</div>

An alternative option is to use `also`

```kotlin
var bytesRead:Int =0
while(stream.read(buffer).also { bytesRead = it } >=0) {
  out.write(buffer, 0, bytesRead)
}

```

I agree it isn’t very pretty, perhaps the best way is to make it into some sort of inline function that encapsulates the ugliness of whatever code you end up using.

---

<div class="post-metadata">

**Author:** ![Beholder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beholder/32/2078_2.png) [@Beholder](https://discuss.kotlinlang.org/u/Beholder)\
**Post date:** [May 14, 2018, 2:14pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/6 "2018-05-14T14:14:59Z")

</div>

Or even maybe custom loop function can be useful

```auto
inline fun InputStream.whileReadBytes(array: ByteArray, block: (Int) -> Unit) {
    while (true) {
        val count = read(array)
        if (count < 0) break
        block(count)
    }
}

fun copy(input: InputStream, output: OutputStream) {
    val bytes = ByteArray(512)
    input.whileReadBytes(bytes) { count ->
        output.write(bytes, 0, count)
    }
}

```

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [May 14, 2018, 5:58pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/7 "2018-05-14T17:58:30Z")

</div>

> [@Beholder](#):
>
> Or even maybe custom loop function can be useful
> 
> ```
> inline fun InputStream.whileReadBytes(array: ByteArray, block: (Int) -> Unit) {
> while (true) {
> val count = read(array)
> if (count < 0) break
> block(count)
> }
> }
> 
> fun copy(input: InputStream, output: OutputStream) {
> val bytes = ByteArray(512)
> input.whileReadBytes(bytes) { count ->
> output.write(bytes, 0, count)
> }
> }
> 
> ```

First off for the simple example of copying from an input stream to an OutputStream there is already the InputStream.copyTo extension method in the standard library.

But to address this code suggestion, I would reverse that because I always feel dirty creating local variables and always try to find ways to avoid them:

```
inline fun ByteArray.whileReadBytes(input: InputStream, block: ByteArray.(Int) -> Unit) {
    while (true) {
        val count = input.read(this)
        if (count < 0) break
        this.block(count)
     }
}

fun copy(input: InputStream, output: OutputStream) {
    ByteArray(512).whileReadBytes(input) { output.write(this, 0, it) }
}

```

---

<div class="post-metadata">

**Author:** ![roiamiel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/roiamiel/32/5359_2.png) [@roiamiel](https://discuss.kotlinlang.org/u/roiamiel)\
**Post date:** [May 14, 2018, 6:23pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/8 "2018-05-14T18:23:14Z")

</div>

I think this is the best solution in terms of length, I will use it, thanks.

---

<div class="post-metadata">

**Author:** ![yanex](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yanex/32/1489_2.png) [@yanex](https://discuss.kotlinlang.org/u/yanex)\
**Post date:** [May 17, 2018, 2:52am UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/9 "2018-05-17T02:52:33Z")

</div>

Kotlin stdlib already has a `copyTo` function:

```auto
input.copyTo(output, bufferSize = 512)

```

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [May 17, 2018, 12:44pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/10 "2018-05-17T12:44:28Z")

</div>

> [@yanex](#):
>
> Kotlin stdlib already has a `copyTo` function

**Which I said in the first sentence of my post**. But I assumed that it was just a simple example and the problem the OP was trying to solve was something more complicated.

But the point of my post was focusing on the design of the previously suggested whileReadBytes method which can be used for other things than copying to an output stream.

---

<div class="post-metadata">

**Author:** ![sudeep.sager1234](https://avatars.discourse-cdn.com/v4/letter/s/e9bcb4/32.png) [@sudeep.sager1234](https://discuss.kotlinlang.org/u/sudeep.sager1234)\
**Post date:** [October 17, 2019, 2:14pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/11 "2019-10-17T14:14:46Z")

</div>

in java  
while (pos1 \< len1 && (ch1 = str1.charAt(pos1++)) == ‘0’)  
{  
zeroes1++;  
}  
same i want to write in kotlin please help me

---

<div class="post-metadata">

**Author:** ![christofferberg79](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@christofferberg79](https://discuss.kotlinlang.org/u/christofferberg79)\
**Post date:** [October 17, 2019, 7:39pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/12 "2019-10-17T19:39:16Z")

</div>

If I interpreted your code correctly you are counting the number of leading zeroes. In that case try:  
`zeroes1 = str1.takeWhile { it == ’0’ }.length`

---

<div class="post-metadata">

**Author:** ![julian](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/julian/32/6858_2.png) [@julian](https://discuss.kotlinlang.org/u/julian)\
**Post date:** [October 17, 2019, 7:47pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/13 "2019-10-17T19:47:18Z")

</div>

You should probably make a new post for your question instead of attaching it to an unrelated post that hasn’t been updated in a year.

There are two problems in your Java, for Kotlin:

1. assignment is a statement in Kotlin and therefore cannot appear as a subexpression (because it’s not an expression at all)
2. Kotlin strings don’t answer to .charAt()

So, minimally changing and adding to your code to make it work:

```
fun main() {
    val str1 = "00010"
    val len1 = str1.length
    var zeroes1 = 0
    var pos1 = 0
    while (pos1 < len1) {
        val ch1 = str1[pos1++]
        if (ch1 != '0') break

        zeroes1++;
    }
    println(zeroes1)
}

```

You can’t assign ch1 in the test, so you need to do that in the body, and then following tests that rely on ch1 need to happen in the body as well, but they can still fulfill their role with a conditional break. You can use `.get(pos1++)` instead of `[pos1++]`

Looks like IntelliJ’s Java-\>Kotlin conversion doesn’t fix assignments in tests like this. Pity.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [October 25, 2019, 5:57pm UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/14 "2019-10-25T17:57:41Z")

</div>

> [@christofferberg79](#):
>
> zeroes1 = str1.takeWhile { it == ’0’ }.length

Slight micro-optimization on storage:

```
zeroes1 = str1.asSequence().takeWhile { it == '0' }.count()

```

---

<div class="post-metadata">

**Author:** ![vk](https://avatars.discourse-cdn.com/v4/letter/v/cab0a1/32.png) [@vk](https://discuss.kotlinlang.org/u/vk)\
**Post date:** [April 14, 2025, 11:14am UTC](https://discuss.kotlinlang.org/t/why-i-cant-apply-value-inside-while-loop/7762/15 "2025-04-14T11:14:17Z")

</div>

Yet another sad mistake in language design 😔
