コトリンリターン、ブレーク、コンティニューキーワード
1. 概要
このチュートリアルでは、Kotlinでの構造ジャンプ式の使用法について説明します。
簡単に言えば、Kotlin has three structural jump expressions: return, break, continue.次のセクションでは、ラベルがある場合とない場合の機能について説明します。
2. Kotlinのラベル
Kotlinの式には、ラベルを付けることができます。
We create a label by using an identifier followed by the “@” sign.たとえば、[email protected]、[email protected]は有効なラベルです。
式にラベルを付けるには、その前にラベルを追加するだけです。
[email protected] for (i in 1..10) {
// some code
}
3. Breakステートメント
ラベルがない場合、breakは最も近い囲みループを終了します。
例を見てみましょう。
@Test
fun givenLoop_whenBreak_thenComplete() {
var value = ""
for (i in "hello_world") {
if (i == '_') break
value += i.toString()
}
assertEquals("hello", value)
}
または、break with a label, which terminates the loop marked with that label:を使用することもできます
@Test
fun givenLoop_whenBreakWithLabel_thenComplete() {
var value = ""
[email protected] for (i in 'a'..'d') {
for (j in 1..3) {
value += "" + i + j
if (i == 'b' && j == 1)
[email protected]_loop
}
}
assertEquals("a1a2a3b1", value)
}
この場合、i変数とj変数がそれぞれ「b」と「1」に等しいときに、外側のループが終了します。
4. Continueステートメント
次に、continueキーワードを見てみましょう。これは、ラベルの有無にかかわらず使用できます。
ラベルがない場合、continueは囲んでいるループの次の反復に進みます。
@Test
fun givenLoop_whenContinue_thenComplete() {
var result = ""
for (i in "hello_world") {
if (i == '_') continue
result += i
}
assertEquals("helloworld", result)
}
一方、when we use continue with a label marking a loop, it will proceed to the next iteration of that loop:
@Test
fun givenLoop_whenContinueWithLabel_thenComplete() {
var result = ""
[email protected] for (i in 'a'..'c') {
for (j in 1..3) {
if (i == 'b') [email protected]_loop
result += "" + i + j
}
}
assertEquals("a1a2a3c1c2c3", result)
}
この例では、continueを使用して、outer_loop.というラベルの付いたループの1回の反復をスキップしました。
5. Returnステートメント
ラベルがない場合、returns to the nearest enclosing function or anonymous function:
@Test
fun givenLambda_whenReturn_thenComplete() {
var result = returnInLambda();
assertEquals("hello", result)
}
private fun returnInLambda(): String {
var result = ""
"hello_world".forEach {
if (it == '_') return result
result += it.toString()
}
//this line won't be reached
return result;
}
Returnは、apply continue logic on anonymousfunctions:が必要な場合にも役立ちます。
@Test
fun givenAnonymousFunction_return_thenComplete() {
var result = ""
"hello_world".forEach(fun(element) {
if (element == '_') return
result += element.toString()
})
assertEquals("helloworld", result)
}
この例では、returnステートメントは匿名の楽しみの呼び出し元に戻ります。 forEachループ。
同様の結果を達成するためのIn the case of a lambda expression, we can also use return with a label:
@Test
fun givenLambda_whenReturnWithExplicitLabel_thenComplete() {
var result = ""
"hello_world".forEach [email protected]{
if (it == '_') {
[email protected]
}
result += it.toString()
}
assertEquals("helloworld", result)
}
または、return using an implicit label:
@Test
fun givenLambda_whenReturnWithImplicitLabel_thenComplete() {
var result = ""
"hello_world".forEach {
if (it == '_') {
// local return to the caller of the lambda, i.e. the forEach loop
[email protected]
}
result += it.toString()
}
assertEquals("helloworld", result)
}
上記の例では、returnステートメントはラムダの呼び出し元であるforEachループにも戻ります。
最後に、returnは、apply break logic to lambda expressions byreturning to a label outsideへのラベルとともに使用できます。
@Test
fun givenAnonymousFunction_returnToLabel_thenComplete() {
var result = ""
run [email protected]{
"hello_world".forEach {
if (it == '_') [email protected]
result += it.toString()
}
}
assertEquals("hello", result)
}
6. 結論
この記事では、Kotlinでのreturn, break, continueのユースケースについて説明しました。
サンプルコードはover on Github.で見つかりました