0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Kotlin onEachでbreak / continueをおこなう

Posted at

はじめに

いつも忘れてしまうのでonEachでbreak / continueする方法を記載します

continue

@￰onEachでbreakできます。
具体例で説明します。2がスキップ(*1)できます。

Main.kt
fun main() {
    intArrayOf(1, 2, 3).onEach {
        if (it == 2) return@onEach
        print("$it ")
    }
    // 1 3 (*1)
}

break

run {} でくくって、bk@でbreakできます。
具体例で説明します。2でbreak(*2)できています。

Main.kt
fun main() {
    run bk@{
        intArrayOf(1, 2, 3).onEach {
            if (it == 2) return@bk
            print("$it ")
        }
    }
    // 1 (*2)
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?