当前位置:网站首页>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)
边栏推荐
- View and modify the MySQL data storage directory under centos7
- How to recover the information server and how to recover the server data [easy to understand]
- struct MySQL
- Handwriting blocking queue: condition + lock
- Deep discussion on the decoding of sent protocol
- Interviewer: is acid fully guaranteed for redis transactions?
- Kotlin函数
- Distributed solution - Comprehensive decryption of distributed task scheduling platform - xxljob scheduling center cluster
- Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
- ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
猜你喜欢

Understand redis persistence mechanism in one article

Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification

About LDA model

Learn memory management of JVM 01 - first memory

SAP 自开发记录用户登录日志等信息

Get data from the database when using JMeter for database assertion

JDBC -- extract JDBC tool classes
[email protected] (using password"/>Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password

MySQL index (1)

Redis highly available sentinel mechanism
随机推荐
[HDU 2096] 小明A+B
MySQL index - extended data
Hexadecimal conversion summary
Solve the error 1045 of Navicat creating local connection -access denied for user [email protected] (using password
Instance + source code = see through 128 traps
GPS数据格式转换[通俗易懂]
Handwriting blocking queue: condition + lock
Conversion du format de données GPS [facile à comprendre]
MySQL view
Simply take stock reading notes (1/8)
Add a new cloud disk to Huawei virtual machine
Distributed solution - Comprehensive decryption of distributed task scheduling platform -xxljob
How to recover the information server and how to recover the server data [easy to understand]
Semantic segmentation experiment: UNET network /msrc2 dataset
Anaconda creates a virtual environment and installs pytorch
Distributed solution - Comprehensive decryption of distributed task scheduling platform - xxljob scheduling center cluster
Migrate data from Mysql to neo4j database
GPON technical standard analysis I
[hdu 2096] Xiaoming a+b
Redis highly available sentinel mechanism