当前位置:网站首页>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)
边栏推荐
- GNN(pytorch-geometric)
- Interviewer: is acid fully guaranteed for redis transactions?
- CVPR 2022 | 基于稀疏 Transformer 的单步三维目标识别器
- Using MySQL in docker
- Redis cluster configuration
- Redis highly available sentinel cluster
- GPON other manufacturers' configuration process analysis
- UNIX socket advanced learning diary - advanced i/o functions
- The evolution of mobile cross platform technology
- Resnet18 actual battle Baoke dream spirit
猜你喜欢
Learn the memory management of JVM 02 - memory allocation of JVM
VoneDAO破解组织发展效能难题
Iterator details in list... Interview pits
Volatile instruction rearrangement and why instruction rearrangement is prohibited
Pytoch loads the initialization V3 pre training model and reports an error
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
JSON parsing error special character processing (really speechless... Troubleshooting for a long time)
Pytoch uses torchnet Classerrormeter in meter
在家庭智能照明中应用的测距传感芯片4530A
Resnet+attention project complete code learning
随机推荐
10 minute fitness method reading notes (2/5)
Storage Basics
The evolution of mobile cross platform technology
A new WiFi option for smart home -- the application of simplewifi in wireless smart home
MySQL regular expression
Average lookup length when hash table lookup fails
IPv6与IPv4的区别 网信办等三部推进IPv6规模部署
C alarm design
Pytoch through datasets Imagefolder loads datasets directly from files
About cache exceptions: solutions for cache avalanche, breakdown, and penetration
C language structure is initialized as a function parameter
GPS數據格式轉換[通俗易懂]
MySQL storage engine
ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
Why learn harmonyos and how to get started quickly?
Just a coincidence? The mysterious technology of apple ios16 is actually the same as that of Chinese enterprises five years ago!
Kotlin函数
Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
Detailed structure and code of inception V3
MySQL function