当前位置:网站首页>Kotlin流程控制、循环
Kotlin流程控制、循环
2022-07-05 12:32:00 【好奇的菜鸟】
一、流程控制
1.if
val i = 1
if (i > 0) {
print("Big")
} else {
print("Small")
}使用if,赋值。
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
}上边的简写,Elvis 表达式;
fun getLength(text: String?): Int {
return text?.length ?: 0
}2.when
val i: Int = 1
when(i) {
1 -> print("一")
2 -> print("二")
else -> print("i 不是一也不是二")
}
val i: Int = 1
val message = when(i) {
1 -> "一"
2 -> "二"
else -> "i 不是一也不是二" // 如果去掉这行,会报错
}
print(message)二、循环
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
}
//获取元素
var abc = listOf("a", "b", "c")
for (i in abc) {
print(i)
}
// 获取下标
for (index in abc.indices) {
print("$index") // 0 1 2
}
// 获取元素和下标
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)
边栏推荐
- Reinforcement learning - learning notes 3 | strategic learning
- 手机 CPU 架构类型了解
- MySQL installation, Windows version
- Acid transaction theory
- Keras implements verification code identification
- Hexadecimal conversion summary
- Recyclerview paging slide
- MySQL log module of InnoDB engine
- GPS数据格式转换[通俗易懂]
- Programming skills for optimizing program performance
猜你喜欢

Simple production of wechat applet cloud development authorization login
![[figure neural network] GNN from entry to mastery](/img/e2/bb045fb03bc255b8659f9ca7cdd26b.jpg)
[figure neural network] GNN from entry to mastery

16 channel water lamp experiment based on Proteus (assembly language)

Intern position selection and simplified career development planning in Internet companies
A guide to threaded and asynchronous UI development in the "quick start fluent Development Series tutorials"

Matlab label2idx function (convert the label matrix into a cell array with linear index)

Principle of universal gbase high availability synchronization tool in Nanjing University

Master the new features of fluent 2.10

Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification

Matlab superpixels function (2D super pixel over segmentation of image)
随机推荐
Leetcode-1. Sum of two numbers (Application of hash table)
MySQL view
16 channel water lamp experiment based on Proteus (assembly language)
MySQL transaction
Complete activity switching according to sliding
什么是数字化存在?数字化转型要先从数字化存在开始
Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
Simple production of wechat applet cloud development authorization login
Solution to order timeout unpaid
Hexadecimal conversion summary
[hdu 2096] Xiaoming a+b
Take you two minutes to quickly master the route and navigation of flutter
Time tools
How does MySQL execute an SQL statement?
Swift - enables textview to be highly adaptive
Learn JVM garbage collection 02 - a brief introduction to the reference and recycling method area
Summary of C language learning problems (VS)
End to end neural network
The relationship between the size change of characteristic graph and various parameters before and after DL convolution operation
Learn memory management of JVM 01 - first memory