티스토리 뷰
for
for (item in collection) print(item)
for (item: Int in ints) {
// ...
}
for (i in 1..3) {
println(i)
}
for (i in 6 downTo 0 step 2) {
println(i)
}
for (i in array.indices) {
println(array[i])
}
for ((index, value) in array.withIndex()) {
println("the element at $index is $value")
}
while
while (x > 0) {
x--
}
do {
val y = retrieveData()
} while (y != null) // y is visible here!
Returns and jumps
//return by default returns from the nearest enclosing function or anonymous function.
val s = person.name ?: return
//break terminates the nearest enclosing loop.
//continue proceeds to the next step of the nearest enclosing loop.
loop@ for (i in 1..100) {
for (j in 1..100) {
if (...) break@loop
if (...) continue@loop
}
}
//----------------------
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return // non-local return directly to the caller of foo()
print(it)
}
println("this point is unreachable")
}
//명시적
fun foo() {
listOf(1, 2, 3, 4, 5).forEach lit@{
if (it == 3) return@lit // local return to the caller of the lambda - the forEach loop
print(it)
}
print(" done with explicit label")
}
// 암시적
fun foo() {
listOf(1, 2, 3, 4, 5).forEach {
if (it == 3) return@forEach // local return to the caller of the lambda - the forEach loop
print(it)
}
print(" done with implicit label")
}
// anonymous function
fun foo() {
listOf(1, 2, 3, 4, 5).forEach(fun(value: Int) {
if (value == 3) return // local return to the caller of the anonymous function - the forEach loop
print(value)
})
print(" done with anonymous function")
}
'Programming > Kotlin' 카테고리의 다른 글
Kotlin Generic (0) | 2022.04.30 |
---|---|
Kotlin Null Safety (0) | 2022.04.23 |
Kotlin Conditions (0) | 2022.04.23 |
Kotlin Operator (0) | 2022.04.23 |
Kotlin Class (0) | 2022.04.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Spring JDBC Template
- 복합키 Mapping
- java generic
- Charles proxy
- Property
- spring spel
- Spring Registrar
- @Primary
- Embedded Mapping
- Akka
- Registrar
- JPA
- docker
- Mapping
- Query DSL
- guava
- scikit-learn
- java Equals
- DI
- Discriminate Mapping
- java EqualsAndHashCode
- Criteria
- Sprint RetryTemplate
- Embeddable Mapping
- Join Table
- JPA Criteria
- Spring
- Typesafe Config
- RetryTemplate
- SmartLifecycle
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함