当前位置:网站首页>Kotlin process control and circulation
Kotlin process control and circulation
2022-07-05 12:42:00 【Curious rookie】
One 、 Process control
1.if
val i = 1
if (i > 0) {
print("Big")
} else {
print("Small")
}
Use if, assignment .
val i = 1
val message = if (i > 0) "Big" else "Small"
print(message)
fun getLength(text: String?): Int {
return if (text != null) text.length else 0
}
Abbreviation above ,Elvis expression ;
fun getLength(text: String?): Int {
return text?.length ?: 0
}
2.when
val i: Int = 1
when(i) {
1 -> print(" One ")
2 -> print(" Two ")
else -> print("i Not one nor two ")
}
val i: Int = 1
val message = when(i) {
1 -> " One "
2 -> " Two "
else -> "i Not one nor two " // If you remove this line , Will report a mistake
}
print(message)
Two 、 loop
1、for
val oneToThree = 1..3
for (i in oneToThree) {
println(i)
}
for (i in 6 downTo 0 step 3) {
println(i)
}
for (i in 0..10) {
println(i) //012345678910
}
for (i in 0 until 10) {
println(i) //0123456789
}
for (i in 0..10 step 2) {
print(i) //0 2 4 6 8 10
}
// Get elements
var abc = listOf("a", "b", "c")
for (i in abc) {
print(i)
}
// Get subscript
for (index in abc.indices) {
print("$index") // 0 1 2
}
// Get elements and Subscripts
for ((index, value) in abc.withIndex()) {
print("$index:$value ") //0:a 1:b 2:c
}
2、while
var i = 0
while (i <= 2) {
println(i)
i++
}
var j = 0
do {
println(j)
j++
} while (j <= 2)
边栏推荐
- Redis highly available sentinel cluster
- Learn the memory management of JVM 02 - memory allocation of JVM
- Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
- Summary of C language learning problems (VS)
- [figure neural network] GNN from entry to mastery
- Yum only downloads the RPM package of the software to the specified directory without installing it
- Language model
- Introduction to GNN
- How to recover the information server and how to recover the server data [easy to understand]
- Pytoch loads the initialization V3 pre training model and reports an error
猜你喜欢
Yum only downloads the RPM package of the software to the specified directory without installing it
上午面了个腾讯拿 38K 出来的,让我见识到了基础的天花
About LDA model
The evolution of mobile cross platform technology
Take you hand in hand to develop a service monitoring component
Pytorch two-layer loop to realize the segmentation of large pictures
Add a new cloud disk to Huawei virtual machine
Understand redis persistence mechanism in one article
NPM install reports an error
Pytoch implements tf Functions of the gather() function
随机推荐
Why learn harmonyos and how to get started quickly?
Distributed solution - distributed session consistency problem
Interviewer: is acid fully guaranteed for redis transactions?
Redis highly available sentinel cluster
MySQL stored procedure
Kotlin变量
Implementing Yang Hui triangle with cyclic queue C language
Understand kotlin from the perspective of an architect
PXE startup configuration and principle
Conversion du format de données GPS [facile à comprendre]
MySQL trigger
Anaconda creates a virtual environment and installs pytorch
Yum only downloads the RPM package of the software to the specified directory without installing it
OPPO小布推出预训练大模型OBERT,晋升KgCLUE榜首
Neural network of PRML reading notes (1)
Redis clean cache
Solution to order timeout unpaid
The evolution of mobile cross platform technology
Distributed solution - distributed lock solution - redis based distributed lock implementation
MySQL storage engine